Question

This question already has an answer here:

I have a string date "2010-12-15T16:26:49.841-08:00" and I need to convert it to a GregorianCalendar in Java. How do you do this?


Solution from Jesper's answer

Code for the solution using joda time:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").withOffsetParsed();
DateTime date = formatter.parseDateTime("2010-12-15T16:26:49.841-08:00");
Was it helpful?

Solution

Unfortunately, the standard SimpleDateFormat class cannot handle ISO 8601 format very well. Specifically, it cannot handle the : that is in the timezone offset at the end.

What you can do is manually remove the : from the timezone offset, so that you get a string that looks like this:

2010-12-15T16:26:49.841-0800

(note that the timezone offset is -0800 instead of -08:00). Then you can parse it with SimpleDateFormat with the format yyyy-MM-dd'T'HH:mm:ss.SSSZ.

But it is better to use the popular Joda-Time library to handle times and dates; it is much better than the standard Java API date and calendar classes and handles ISO 8601 format properly.

OTHER TIPS

Use SimpleDateFormat, see javadocs here:

Then convert the Date to Calendar. Take a look at plenty of examples here:

You can try with this piece of code

DateTimeFormatter formatter =
    DateTimeFormat.forPattern("your pattern").withOffsetParsed();
DateTime dateTime = formatter.parseDateTime("your input");
GregorianCalendar cal = dateTime.toGregorianCalendar();

This will defnitely give you the Gregorian Calender Object

 String dateTimeString="2010-12-15T16:26:49.841-08:00"; 

 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

 DateTime dateTime = formatter.parseDateTime(dateTimeString);

 GregorianCalendar cal = dateTime.toGregorianCalendar();

The other Answers are correct but now outdated. They use troublesome old classes now supplanted by the java.time framework.

Using java.time

The input string happens to comply with ISO 8601 standard formatting. So no need to specify a formatting pattern as the java.time classes use ISO 8601 by default when parsing/generating strings.

The input string includes an offset-from-UTC, so we parse as an OffsetDateTime.

String input = "2010-12-15T16:26:49.841-08:00" ;
OffsetDateTime odt = OffsetDateTime.parse( input );

If you have a specific time zone in mind, rather than a mere offset-from-UTC, apply that.

ZoneId zoneId = ZoneId.of( "America/Los_Angele" );
ZonedDateTime zdt = odt.atZone( zoneId );

GregorianCalendar

You should avoid the old date-time classes such as GregorianCalendar. But if you must to interoperate with old code not yet updated for java.time, you can convert. Use new methods added to the old classes for conversion. For more info and a nifty diagram, see my Question and Answer.

Calendar cal = java.util.GregorianCalendar.from( zdt );  // Do such conversions out of java.time only if absolutely necessary.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome 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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top