0

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");
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Charlie
  • 41
  • 5
  • FYI that format is the format of the [XML dataTime type](http://www.w3.org/TR/xmlschema-2/#dateTime). Conversions should exist in any decent XML API (and elsewhere) –  Dec 16 '10 at 03:32
  • Actually, this is ISO 8601 I think... http://en.wikipedia.org/wiki/ISO_8601 – Andrew White Dec 16 '10 at 03:36
  • @Andrew White XML dateTime is mostly a specific subset of 8601 -- namely `'-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?`. –  Dec 16 '10 at 03:37
  • 1
    I think this question has been asked quite a number of times on SO. Hey, and look below -- our first "Use JodaTime" answer... – andersoj Dec 16 '10 at 04:41
  • @andersoj: Isn't it interesting that questions with easily memorized answers are rarely flagged as duplicates. Must have something to do with accumulating points. – Gilbert Le Blanc Dec 16 '10 at 20:29
  • @Gilbert Le Blanc: Agreed, and I'm sort of amazed that the community hasn't come up with a better way of focusing in and highlighting FAQ type questions in a single SO question. This topic (date/time handling in Java) is a great example -- just poke around at the related tags, and you'll see all sorts of overlapping questions just begging for a unifying question/answer (and closing/redirecting all the rest). Accumulating points by any means necessary? Perish the thought. – andersoj Dec 16 '10 at 21:00
  • @andersoj: hahaha, you're such a sissy getting all worked up over this. you don't even provide any help. your comments are worthless and you posted twice about it. so lame, try and get a life – Charlie Dec 17 '10 at 19:28
  • Duplicate: [What's the best way to parse an XML dateTime in Java?](http://stackoverflow.com/q/909022/642706) – Basil Bourque Aug 14 '16 at 22:45

5 Answers5

1

Use SimpleDateFormat, see javadocs here:

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

icyrock.com
  • 27,952
  • 4
  • 66
  • 85
1

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 1
    Note for future readers - with Java 7, you can use the "Z" specifier which corresponds to the ISO-8601 format. – Jon Skeet Oct 08 '12 at 09:32
0

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.

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

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

gmhk
  • 15,598
  • 27
  • 89
  • 112
  • You might want to mention that you are using Joda time classes here, not standard Java API classes. – Jesper Dec 16 '10 at 09:03
0
 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();
Ratna Dinakar
  • 1,573
  • 13
  • 16
  • Did you try this out? It doesn't even compile. You might want to mention that you are using Joda time classes. – Jesper Dec 16 '10 at 09:01