Java / convert ISO-8601 (2010-12-16T13:33:50.513852Z) to Date object

非 Y 不嫁゛ 提交于 2019-11-26 04:28:17

问题


How to parse a String in ISO 8601 format with Zulu time?

javax.xml.bind.DatatypeConverter.parseDateTime(\"2010-12-16T13:33:50.513852Z\")

returns

IllegalArgumentException: \'2010-12-16T13:33:50.513852Z\' weist ein falsches Format auf.

Which mean something like wrong format, anyone have a clue what iss wrong in here?


回答1:


tl;dr

Instant.parse( "2010-12-16T13:33:50.513852Z" )

java.time

The newer java.time classes can handle this string input.

The Z on the end is short for Zulu and means UTC, an offset of zero +00:00.

Instant

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( "2010-12-16T13:33:50.513852Z" );

Time zone

You may want to apply a time zone ZoneId to get a ZonedDateTime. Search Stack Overflow for those class names to learn more, as well as for classes OffsetDateTime and DateTimeFormatter.

Conversion

Best to avoid the troublesome old legacy class of java.util.Date. But if you insist, call the new conversion methods added to the old classes.

java.util.Date date = java.util.Date.from( instant );


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?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

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.




回答2:


It works for me try ide online

Output is :

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+00:00",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=11,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=16,DAY_OF_YEAR=1,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=13,MINUTE=33,SECOND=50,MILLISECOND=513,ZONE_OFFSET=0,DST_OFFSET=0]



回答3:


The code you posted works fine in my jre. Probably you defined your own DatatypeConverter (with german exception texts...!) and this specific DatatypeConverter cannot parse this date.

Do a codesearch for this code: DatatypeConverter.setDatatypeConverter( - there you will probably find your custom implementation of the "DatatypeConverterInterface" - which will then probably lead you to your bug.

Alternatively you can search for weist ein falsches Format auf. (because that exception text is not part of the jre)

Viel Erfolg ;)




回答4:


import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS'Z'");
Date localizatedAt = dateFormat.parse(nextLine[i - 5 + 2]);


来源:https://stackoverflow.com/questions/4525850/java-convert-iso-8601-2010-12-16t133350-513852z-to-date-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!