Java timezone when parsing DateFormat

I had code that parses a date like this:

String ALT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; SimpleDateFormat sdf = new SimpleDateFormat( ALT_DATE_TIME_FORMAT); Date date = sdf.parse(requiredTimeStamp); 

And it worked fine, suddenly it stopped working. It turns out that the administrator made some configuration changes to the server, and the date is currently being returned as "2010-12-27T10: 50: 44.000-08: 00", which cannot be analyzed by the template described above. I have two questions:

The first one will be, which template will analyze the date returned by the JVM in the format above (in particular, only “-08: 00” as the time zone)? And secondly, where exactly can such settings be changed on the RHEL 5 linux server so that we know about such changes in the future?

+6
java timezone datetime date-format iso8601
source share
7 answers

Another application uses the ISO 8601 date and time format. I assume that another application sends you an XML response that matches the DateTime type of XML Schema, which is ISO 8601. It is now known that DateFormat cannot parse this format. You should either use other libraries, such as joda-time (joda-time is the winner) or FastDateFormat, as indicated in other answers. Take a look at this post Converting an ISO 8601-compatible string to java.util.Date

+4
source share

TL; DR

 OffsetDateTime.parse( "2010-12-27T10:50:44.000-08:00" ) 

ISO 8601

The input string format is defined in ISO 8601 , a family of date and time formats.

Avoid Old Time Classes

Questions and other answers use old deprecated time classes associated with the earliest versions of Java. Avoid them. Now superseded by java.time classes.

Using java.time

Your input line ends with offset-from-UTC . So, we analyze as an object OffsetDateTime .

The java.time classes use ISO 8601 formats by default when parsing / generating strings. Therefore, there is no need to specify a formatting pattern.

 OffsetDateTime odt = OffsetDateTime.parse( "2010-12-27T10:50:44.000-08:00" ); 

If you want to view this date and time in the UTC timeline, retrieve Instant .

 Instant instant = odt.toInstant(); 

A time zone is an offset plus a set of rules for handling anomalies, such as Daylight Saving Time (DST). If you have a time zone, use ZoneId to get the ZonedDateTime object. At the same time on the timeline, but can be viewed after another time of the wall clock .

 ZoneId z = ZoneId.of( "America/Montreal" ); ZonedDateTime zdt = odt.atZoneSameInstant( z ); // Same moment on the timeline, but viewed through a different wall-clock time. 

About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

You can exchange java.time objects directly with your database. Use a JDBC driver that conforms to JDBC 4.2 or later. No strings needed, no java.sql.* Classes needed.

Where to get java.time classes?

  • Java SE 8 , Java SE 9 , and then
    • Built in.
    • Part of the standard Java API with integrated implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the functionality of java.time has been ported to Java 6 and 7 in ThreeTen-Backport .
  • Android
    • Later versions of the Android package implementations of the java.time classes.
    • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP ....

The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

+6
source share

If you want to parse it using the direct JDK, I believe that it should be analyzed using the JAXB utility, see DatatypeFactory.newXMLGregorianCalendar or DatatypeConverter.parseDateTime .

+1
source share

Use JodaTime

As a more specific example for using @Pangea to use JodaTime, this is what you could use:

 String timestamp = "2012-09-17T04:11:46Z"; DateTime date = ISODateTimeFormat.dateTimeParser().parseDateTime(timestamp); 

This correctly recognizes the UTC time zone. I have not tried this with milliseconds in a string label, but I am sure that it will work just as well.

Hope this helps others.

In JP

+1
source share

The question should be where the requiredTimeStamp comes from and in what format. Is it entered by the user or read from another program? Which component creates the date in the String view?

The format "2010-12-27T10: 50: 44.000-08: 00" looks like a standardized format ISO-8601 It should be processed with the template yyyy-MM-dd'T'HH:mm:ss.SSSZ

Not sure which settings affect this, but there is an Oracle FAQ about Java TimeZones . This can be the user.timezone system property or the /etc/localtime symbolic link in RHEL.

0
source share

Try changing it to lowercase z.

z handles most of the general general time zone syntax, while Z uses the more stringent 4-digit RFC 822 time zone.

Although his document states that both should analyze “General time zone settings”, this may make a difference in your case.

0
source share

SimpleDateFormat accepts only -0800 or GMT-08:00 as a time zone.

It seems that the ISO 8601 format cannot be parsed using SimpleDateFormat . Maybe you should take a look at the Apache Commons Lang FastDateFormat . It is compatible with SimpleDateFormat , but accepts the ZZ template for the time zone, which should analyze the time zone format you need. DateFormatUtils contains some sample constants that look like the template you need, just without milliseconds (like ISO_DATETIME_TIME_ZONE_FORMAT ).

0
source share

All Articles