John John
John John

Reputation: 4575

Parsing timestamp Soap fied to Java Date

I received from a SOAP message the follow date: 2012-11-16T02:02:05Z

How to parse to Date? What means this T and the Z in the date according with the SOAP specification?

Upvotes: 0

Views: 3858

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 339737

tl;dr

Instant.parse( "2012-11-16T02:02:05Z" )

ISO 8601

Your input string is in standard ISO 8601 format. That standard defines many useful, practical, unambiguous textual formats for representing date-time values.

The ISO 8601 formats are commonly adopted by modern protocols, such as XML schema, SOAP, etc., supplanting the clumsy outmoded formats such as RFC 1123 / RFC 822.

Your particular format from ISO 8601 uses:

  • T to separate the year-month-day portion from the hour-minute-second portion.
  • Z, short for Zulu, to indicate UTC time (an offset-from-UTC of +00:00).

Using java.time

Use the modern classes in the java.time package. They use ISO 8601 formats by default when parsing/generating strings.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.parse( "2012-11-16T02:02:05Z" ) ;

Capture the current moment in UTC.

Instant instant = Instant.now() ;

Generate a string in standard format by simply calling toString.

String output = instant.toString() ;

2017-07-26T01:23:45.704781401Z


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Upvotes: 1

John John
John John

Reputation: 4575

I can parse correctly doing this way:

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(paymentDate);

Thanks @vikdor!

Upvotes: 1

Related Questions