0

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
  • 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 Answers2

1

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);
David Rauca
  • 1,583
  • 1
  • 14
  • 17
0

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?

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154