I got the problem with date format yyyy-MM-dd'T'hh:mm:ss.SSS'Z'
, always when I parse some date the time is set to 12:00:00.000
.
This is my date formater:
DateFormat xmlDateFormatWithMs = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
I got the problem with date format yyyy-MM-dd'T'hh:mm:ss.SSS'Z'
, always when I parse some date the time is set to 12:00:00.000
.
This is my date formater:
DateFormat xmlDateFormatWithMs = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
You seem to need 24-hours hour format. You need:
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
With capital HH
to specify you need a 24-hours format.
Instant.parse( "2016-09-27T18:33:39Z" )
Another solution is to avoid using the old SimoleDateFormat and java.util.Date classes as they are notoriously troublesome.
Pass your ISO 8601 compliant string directly to Joda-Time:
DateTime dateTime = new DateTime( myString );
Or, preferably in java.time. The Joda-Time project is now in maintenance mode and advises migration to java.time.
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds. That means up to nine (9) digits of a decimal fraction.
Instant instant = Instant.parse( "2016-09-27T18:33:39Z" );
To adjust into other offsets or time zones, search Stack Overflow for OffsetDateTime
and ZonedDateTime
classes.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.