Truncate date to start of the day in a given timeZone - date

I am looking to truncate the date time to start of the day for the given timeZone.
If the current time is Mon Aug 24 15:38:42 America/Los_Angeles, it should be truncated to start of the day Mon Aug 24 00:00:00 America/Los_Angeles which then later should be converted to equivalent UTC time.
I have explored the methods provided by Joda Time Library, Apache Commons Library and ZonedDateTime but all of them truncate the date time in UTC and not to specific timeZone.
Can someone help me with this? Thanks in advance.

You can use ZonedDateTime. Use toLocalDate() on ZonedDateTime to get LocalDate then atStartOfDay on LocalDate with zone of ZonedDateTime instance to get the start of day.
Example:
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
ZonedDateTime startOfDay = now.toLocalDate().atStartOfDay(now.getZone());
DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
System.out.println(now.format(formatter)); // Mon, 24 Aug 2020 10:41:41 -0700
System.out.println(startOfDay.format(formatter)); // Mon, 24 Aug 2020 00:00:00 -0700

ZonedDateTime truncates in its own time zone. So it can be done a bit simpler than in the currently accepted answer (which is also a good and correct answer).
ZonedDateTime given = ZonedDateTime.of(2020, 8, 24, 15, 38, 42, 0, ZoneId.of("America/Los_Angeles"));
ZonedDateTime startOfDay = given.truncatedTo(ChronoUnit.DAYS);
System.out.println("Truncated to start of day: " + startOfDay);
Instant inUtc = startOfDay.toInstant();
System.out.println("In UTC: " + inUtc);
Output is:
Truncated to start of day: 2020-08-24T00:00-07:00[America/Los_Angeles]
In UTC: 2020-08-24T07:00:00Z

Related

How to get current month from the selected date in GWT?

Date startDate = new Date(Long.valueOf(""05/07/2018")getValue().toString());
get last day of selected month is required
private Date lastDayOfMonth(Date month) {
Date lastDay = (Date) month.clone();
CalendarUtil.addMonthsToDate(lastDay, 1);
CalendarUtil.setToFirstDayOfMonth(lastDay);
CalendarUtil.addDaysToDate(lastDay, -1);
GWT.log("lastDay :: "+lastDay);
return lastDay;
}
I get lastDay :: Tue Jul 31 00:00:00 GMT+530 2018
But i get 30th instead of 31st
First, your code does not work
Date startDate = new Date(Long.valueOf(""05/07/2018")getValue().toString());
Second, your method private Date lastDayOfMonth(Date month) looks fine, I have a test and got result Tue Jul 31 00:00:00
I dont understand your problem "Tue Jul 31 00:00:00 GMT+530 2018 But i get 30th instead of 31st". How can you get 30th from Tue Jul 31 00:00:00 GMT+530?

Format Date String in momentjs

I am trying to format a timezone based
How can i convert a JS time into these formats?
"Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)"
"September 24th 2015, 2:00:00 pm UTC-07:00"
"2015-09-24 14:00:00 GMT-0700"
"Sept 24 2015 14:00:00 GMT-0700"
"Thu Sep 24 2015 14:00:00 GMT-0700 (Pacific Daylight Time)"
Converting into any of it would help.
You can use tokens listed in the format documentation, as shown in the following snippet.
Use square brackets [] to add characters that should be escaped (GMT and UTC in the example, if you need current zone abbreviation use the z token).
Note that as the moment-timezone docs says:
Moment.js also provides a hook for the long form time zone name. Because these strings are generally localized, Moment Timezone does not provide any long names for zones.
To provide long form names, you can override moment.fn.zoneName and use the zz token.
You can find in the snippet an example of providing long names for zones.
var time = "2016-11-09 15:38:00", zone = "America/Chicago";
var m = moment.tz(time,zone);
console.log(m.format('ddd MMM D YYYY HH:mm:ss [GMT]ZZ (z)'));
console.log(m.format('MMMM Do YYYY, h:mm:ss a [UTC]ZZ'));
console.log(m.format('YYYY-MM-DD HH:mm:ss [GMT]ZZ'));
console.log(m.format('MMM D YYYY HH:mm:ss [GMT]ZZ'));
// Add long names for sample zones
var abbrs = {
EST : 'Eastern Standard Time',
EDT : 'Eastern Daylight Time',
CST : 'Central Standard Time',
CDT : 'Central Daylight Time',
MST : 'Mountain Standard Time',
MDT : 'Mountain Daylight Time',
PST : 'Pacific Standard Time',
PDT : 'Pacific Daylight Time',
};
moment.fn.zoneName = function () {
var abbr = this.zoneAbbr();
return abbrs[abbr] || abbr;
};
console.log(m.format('ddd MMM D YYYY HH:mm:ss [GMT]ZZ (zz)'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.7/moment-timezone-with-data-2010-2020.min.js"></script>
I was able to make third one using this snippet
var time = "2016-11-09 15:38:00",
zone = "America/Chicago",
format = "YYYY-MM-DD HH:mm:ss zZZ";
moment.tz(time,zone).utc().format(format)

Issue saving a string as ISO date format

I'm trying to save a date to MongoDB from FullCalendar in my Grails application.
I'm trying to parse the string 2015-12-27T00:00:00.000Z into the below format:
def startDate = new Date().parse("YYYY-MM-dd'T'HH:mm:ss.SSSXXX",it.start)
def endDate = new Date().parse("YYYY-MM-dd'T'HH:mm:ss.SSSXXX",it.end)
But, weirdly when I print the formatted date, I get Sun Dec 28 05:30:00 IST 2014. I don't know what or how that particular date is picked.
You should use lowercase y for year. Uppercase Y is for "Week year".
new Date().parse("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", "2015-12-27T00:00:00.000Z")
===> Sat Dec 26 19:00:00 EST 2015
import java.text.SimpleDateFormat;
println new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX").parse("2018-07-30 09:57:15 +0800")

Go language time.Parse() for timestamps with no timezone

In Go I'm trying to use the time.Parse() function from the time package to convert a string timestamp into a Time object. I know Go has an uncommon way of representing the time format your timestamps are in by providing it with an example of how their reference time (Mon Jan 2 15:04:05 -0700 MST 2006) would be displayed in your format. I'm still having issues with errors however. Here is an example of one of my timestamps:
Tue Nov 27 09:09:29 UTC 2012
Here is what the call I'm making looks like:
t, err := time.Parse("Mon Jan 02 22:04:05 UTC 2006", "Tue Nov 27 09:09:29 UTC 2012")
So basically what I've done here is try and match the formatting for day name/month name/day number, the hour/minute/second format, the string literal "UTC" and the year format. Note that I've increased the hours field of the Go reference format by 7 (from 15 to 22) to account for the fact that their timestamp is in a negative 7 timezone and all my timestamps are in a UTC timezone.
The error I get is:
parsing time "Tue Nov 27 09:09:29 UTC 2012" as "Mon Jan 02 22:04:05 UTC 2006": cannot parse ":09:29 UTC 2012" as "2"
What am I doing wrong here? Am I misinterpreting how to use time.Parse() or is my use case not supported for some reason?
Your format string should be:
Mon Jan 02 15:04:05 MST 2006
playground
That is, use MST for the timezone and 15 for the hour, as documented in your linked Parse function.
In this case, you can use time.UnixDate:
package main
import (
"fmt"
"time"
)
func main() {
t, e := time.Parse(time.UnixDate, "Tue Nov 27 09:09:29 UTC 2012")
if e != nil {
panic(e)
}
fmt.Println(t)
}
https://golang.org/pkg/time#UnixDate

DateTime JodaTime conversion

Please imagine the following:
I've this date in the Azores (GMT -1)
23/10/2010 23:00:00
And i want to convert this date to the following (the GMT +1)
24/10/2010 01:00:00
I want this behavior for any date in any timezone and the Date function with timezone give me the GMT -1 for this case.
Please note that i'm using JodaTime.
Thanks.
DateTime dateTime = new DateTime(2010, 10, 23, 23, 0, 0, 0, DateTimeZone.forOffsetHours(-1)); // (GMT -1) 23/10/2010 23:00:00
DateTime inAnotheTimeZone = dateTime.withZone(DateTimeZone.forOffsetHours(1));