0

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?

John John Pichler
  • 4,427
  • 8
  • 43
  • 72
  • 3
    Take a look at the SimpleDateFormat class: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html to understand the meaning of those. They are not specific to SOAP, btw. – Vikdor Nov 16 '12 at 02:31
  • @Vikdor wow, not specific to SOAP but there is some example. It seems the chars T and Z does not meaning anything according the example I found there: "yyyy-MM-dd'T'HH:mm:ss.SSSZ" – John John Pichler Nov 16 '12 at 11:42
  • Just Z seems to be the timezone, but The value is comming from paypal webservice is not according the specification.. – John John Pichler Nov 16 '12 at 11:45
  • Similar: [Converting ISO 8601-compliant String to java.util.Date](http://stackoverflow.com/q/2201925/642706) – Basil Bourque Feb 18 '17 at 20:06

2 Answers2

1

I can parse correctly doing this way:

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

Thanks @vikdor!

John John Pichler
  • 4,427
  • 8
  • 43
  • 72
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Feb 18 '17 at 19:28
1

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154