trung truong
trung truong

Reputation: 21

Java LocalDate parser does not take hours, minutes, seconds into account

I want to parse DateTime from a string. Here is my code:

String dateStr = "2016-08-18T14:44:56.225Z"
final String Pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
final DateTimeFormatter dtf = DateTimeFormat.forPattern(Pattern);
LocalDate dt = dtf.parseLocalDate(dateStr);

Then I only receive Year, Month and Day (2016-08-18) from the code above. I cannot get Hours, Minutes and Seconds.

Did anyone get this problem before? How did you solve that? I use Java 7 and Joda-Time.

Upvotes: -1

Views: 1337

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338564

tl;dr

Instant                               // Represents a moment, a point on the timeline, as seen with an offset of zero hours-minutes-seconds. 
.parse( "2016-08-18T14:44:56.225Z" )  // Returns an `Instant` object. 
.atZone( ZoneId.of( "Asia/Tokyo" ) )  // Returns a `ZonedDateTime` object. Same moment, different wall-clock/calendar. 

Instant

The Z on the end of your input "2016-08-18T14:44:56.225Z" is an abbreviation for +00:00, pronounced “Zulu”, meaning an offset of zero hours-minutes-seconds from the temporal meridian of UTC.

That input should be parsed as an Instant. The java.time.Instant class represents a moment as seen in UTC. This is the basic-building block of the java.time framework.

Instant instant = Instant.parse( "2016-08-18T14:44:56.225Z" ) ;

Representing a moment, a point on the timeline, means the Instant class contains a date with a time-of-day in the offset of zero. That time-of-day includes hours, minutes, seconds, and a fractional second as fine as nanoseconds. This meets the needs you stated in your Question.

Now adjust to the time zone through which you choose to view this moment. Remember that for any given moment, the time and the date vary around the globe by time zone.

ZoneId z = ZoneId.of( "America/Edmonton" ) ;

Apply that time zone to see the same moment through the wall-clock/calendar used by the people of that region. You produce a ZonedDateTime object.

ZonedDateTime zdt = instant.atZone( z ) ;

Now you have date with a time-of-day as seen in that zone.

LocalDate

The LocalDate class represents a date-only value, a year, a month, and a day. No time-of-day, no time zone, no offset.

We can extract a LocalDate from our ZonedDateTime above.

LocalDate ld = zdt.toLocalDate() ;

Parsing

Regarding your formatting pattern: `Pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'":

  • Never wrap the Z in single-quote marks. That means “expect this text, but ignore this text”. You should never ignore that text. The Z indicates vital information, the offset of zero as discussed above.
  • That particular format is defined in the ISO 8601 standard. The java.time classes use the standard formats by default. So no need for you to define that particular format. Simple call toString & parse on Instant class.
  • By convention, Java variables are named starting with a lowercase letter. So pattern, not Pattern.

Offset vs Time zone

An offset is merely a number of hours-minutes-seconds ahead or behind the temporal meridian of UTC. For example -08:00 and +05:30.

A time zone is a named history of the past, present, and future changes to the offset used by the people of a particular region as decided by their politicians. Named in format of Continent/Region such as Africa/Tunis and Pacific/Auckland.

Java 6 & 7

The java.time classes are built into Java 8 and later.

For Java 6 & 7, use the back-port library, ThreeTen-Backport. But, really, it is time to move on to Java 8, 11, 17, 21, or soon enough, 25 (the LTS versions).

Upvotes: 2

mszymborski
mszymborski

Reputation: 1654

Use LocalDateTime to get time, too:

String dateStr = "2016-08-18T14:44:56.225Z"
final String Pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
final DateTimeFormatter dtf = DateTimeFormat.forPattern(Pattern);
LocalDateTime dt = dtf.parseLocalDateTime(dateStr);

You are also ignoring the time zone (UTC/Zulu in this case - the Z at the end) - make sure if you really want to do this.

Upvotes: 4

Related Questions