I have a trouble with parsing date time in java, I have a strange date time format.
How can I parse 2013-04-03T17:04:39.9430000+03:00
date time in java to format dd.MM.yyyy HH:mm
in java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
The "strange" format in question is ISO-8601 - its very widely used. You can use SimpleDateFormat to reformat it in most way you please:
will give you the format you mentioned.
For serious work with date and time in java I would suggest using a better implementation than the Calendar stuff. I would use Joda, and therein you can use DateTimeFormatter
tl;dr
ISO 8601
As the others noted, your format is not strange at all. Indeed it is a standard format. That format is one of a collection defined by the ISO 8601 format.
Microseconds
Those seven digits of a decimal fraction of a second,
.9430000
, represents nanoseconds. The old date-time classes bundled with the earliest versions of Java (java.util.Date/.Calendar/java.text.SimpleDateFormat) are built only for milliseconds (three digits of decimal fraction). Such input values as your cannot be handled by the old classes.java.time
Fortunately Java now has newer date-time classes that supplant those old classes. The new ones are in the java.time framework. These new classes can handle nanoseconds (up to nine digits of decimal fraction), so no problem there.
The java.time framework is built into Java 8 and later. Defined in JSR 310. Much of the functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project, and further adapted for Android in the ThreeTenABP project.
OffsetDateTime
An
OffsetDateTime
represents a moment on the timeline with an offset-from-UTC. Your input string,2013-04-03T17:04:39.9430000+03:00
, has an offset that is three hours ahead of UTC.The java.time classes use ISO 8601 formats by default when parsing/generating strings. So no need to define a formatting pattern. We can directly parse that string.
Generating Strings
To generate a string representation in the same style, call its
toString
method.For a different format define a formatting pattern.
Time Zone
Note that your input has an offset-from-UTC but not a true time zone. A time zone is an offset plus rules for handling anomalies such as Daylight Saving Time (DST). For a true time zone use
ZoneId
to get aZonedDateTime
. Search Stack Overflow for many examples.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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.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.Please use this method for parse ISO8601 Date without any library. http://www.java2s.com/Code/Java/Data-Type/ISO8601dateparsingutility.htm