Java – set the hours, minutes and seconds to 00 in zoneddatetime or instant
I have a date string in UTC format –
String dateStr = "2017-03-03T13:14:28.666Z";
I want to convert it to the following format in the Java date representation of zoned datetime
When zoned datetime prints, it should display
String dateStr = "2017-03-03T00:00:00.000Z";
I tried the following code –
String timeZone = "America/Los_Angeles"; DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"); DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); ZoneId zoneId1 = ZoneId.of(timeZone); String dateStr = "2017-03-03T13:14:28.666Z"; Instant inst = Instant.parse(dateStr,dtf2); zoneddatetime dateTimeInTz = zoneddatetime.ofInstant(inst,zoneId1); zoneddatetime startTime = dateTimeInTz.with(LocalTime.of(0,0)); zoneddatetime endTime = dateTimeInTz.with(LocalTime.MAX); System.out.println("Start:"+startTime+",End:"+endTime); System.out.println("Start:"+startTime.toString()+",End:"+endTime.toString()); zoneddatetime nT = zoneddatetime.of ( LocalDate.parse(dateStr,dtf1),LocalTime.of (0,0),ZoneId.of ( timeZone ) ); System.out.println("Start:"+nT);
Output:
Start:2017-03-03T00:00-08:00[America/Los_Angeles],End:2017-03-03T23:59:59.999999999-08:00[America/Los_Angeles] Start:2017-03-03T00:00-08:00[America/Los_Angeles],End:2017-03-03T23:59:59.999999999-08:00[America/Los_Angeles] Start:2017-03-03T00:00-08:00[America/Los_Angeles]
I want to normalize the start time in zoned date time I want to implement it with Java libraries, not any third-party libraries
Solution
TL; doctor
You work too hard
Instant.parse( "2017-03-03T13:14:28.666Z" ) .truncatedTo( ChronoUnit.DAYS ) .toString()
details
What does "normalize in zoned datetime" mean? Please edit your question for clarification
You're asking about contradictions Zoneddatetime has a specified time zone, which is used to view the wall clock time of a specific region Therefore, it is meaningless to require zoneddatetime to generate strings in UTC format, such as "2017-03-03t00:00:00.000z" Z is the abbreviation of Zulu, which means UTC
Your input string is in the standard ISO 8601 format By default, Java The time class uses these standard formats Therefore, there is no need to specify the format mode and the datetimeformatter class
It is resolved to a point on the UTC time axis with a resolution of nanoseconds
Instant instant = Instant.parse( "2017-03-03T13:14:28.666Z" );
If you want midnight UTC, please cut it off
Instant instantMidnightUtc = instant.truncatedTo( ChronoUnit.DAYS );
The zoneddatetime class is not required
Use the localdate class if you want to use only dates and no time zones
By the way, don't assume that the first moment of the day is always 00:00:00 The same is true for UTC However, exceptions may occur in each time zone, such as daylight saving time (DST), where one day may start at another time point, such as 01:00:00
About Java time
java. The time framework is built into Java 8 and later These classes replace the cumbersome old legacy date time classes, such as Java util. Date, calendar and & simpledateformat
Joda time project, which is now maintenance mode, is recommended to migrate to Java Time class
For more information, see Oracle tutorial And search stack overflow for many examples and instructions The specification is JSR 310
Where to get Java Time class?
>Java se 8 and Se 9 and later
>Built in. > Some standard Java APIs with bundled implementations. > Java 9 has added some small functions and fixes
>Java se 6 and Se 7
>Most Java The time function was reverse ported to Java 6& July at threeten backup
> Android
>Threetenabp project is especially suitable for threeten backup of Android (as described above). > See how to use threetenabp
The threeten extra project extends Java. Net with other classes time. The project is likely to be added to Java in the future Time's proving ground You can find some useful classes here, such as interval, yearweek, yearquarter and more