I am just trying to convert "2016-01-28T12:08:47.676706-05:00" but getting exception. Can anyone suggest me DateFormat like "yyyy-MM-dd HH:mm:ss.SSSZ" to parse the string into Date.
-
See: http://stackoverflow.com/questions/19223171/java-util-date-format-ssssss-if-not-microseconds-what-are-the-last-3-digits – Morrison Chang Jan 28 '16 at 17:28
-
Please provide [a minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) demonstrating your problem. In this case, that would include the full Java stack trace for your exception, along with all code associated with that stack trace. – CommonsWare Jan 28 '16 at 17:29
2 Answers
I hope that the code below will help you
String dateString = "2016-01-28T12:08:47.676706-05:00";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS", Locale.getDefault());
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(dateString);

- 1,583
- 1
- 14
- 17
-
You're going to get an exception with that. You forgot the seconds. – Kristy Welsh Jan 28 '16 at 17:35
-
Not really. I get 00 instead of 47 seconds. Thanks for the remark. I'll update the answer. – David Rauca Jan 28 '16 at 17:39
-
tl;dr
OffsetDateTime.parse( "2016-01-28T12:08:47.676706-05:00" )
java.time
Use the modern java.time classes that supplant the troublesome legacy date-time classes.
The legacy classes wore limited to millisecond resolution while the java.time classes have much finer resolution, nanoseconds. That is more than enough for the microseconds in your input string.
Your input complies with standard ISO 8601 format. The java.time classes use standard formats by default when parsing/generating strings. So no need to specify a formatting pattern at all.
We parse your input as a OffsetDateTime
since it includes an offset-from-UTC but not a time zone.
OffsetDateTime odt = OffsetDateTime.parse( "2016-01-28T12:08:47.676706-05:00" ) ;
odt.toString(): 2016-01-28T12:08:47.676706-05:00
To see that same moment as UTC value, extract an Instant
.
Instant instant = odt.toInstant() ;
instant.toString(): 2016-01-28T17:08:47.676706Z
Or adjust into a time zone, to get a ZonedDateTime
object.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
zdt.toString(): 2016-01-29T06:08:47.676706+13:00[Pacific/Auckland]
To generate Strings in other formats, search Stack Overflow for DateTimeFormatter
class. Already covered many times.
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, Java 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 Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle 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 proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 303,325
- 100
- 852
- 1,154