how to convert dateTime type to gregorianCalendar

10,528

Solution 1

Java has built in code to parse xml datetimes: use DatatypeConverter.parseDateTime(). that will return a Calendar in the parsed TimeZone. you can then set the Calendar TimeZone to your desired target TimeZone for whatever you need to do next.

Solution 2

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

parses everything, except the trailing TZ.

sdf.parse (sd);
res168: java.util.Date = Sun Feb 27 10:03:33 CET 2011

From the api docs, I would expect

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

to be used to read the -06:00 in the end. But I see, that there is either an offset in the form 0700 expected, or with a prefix of GMT for example "GMT-04:00". So you have to insert that GMT-thingy yourself:

sdf.parse (sd.replaceAll ("(......)$", "GMT$1"))

SDF.parse (str) returns a Date, which has to be converted into a GC:

GregorianCalendar calendar = new GregorianCalendar ();
calendar.setTime (date);

Solution 3

tl;dr

OffsetDateTime.parse( "2011-02-27T10:03:33.099-06:00" )

java.time

The modern approach uses the java.time classes that supplanted the troublesome old legacy date-time classes. Specifically, GregorianCalendar was replaced by ZonedDateTime for a time zone, and OffsetDateTime for a mere offset-from-UTC.

ISO 8601

Your input string happens to comply with the ISO 8601 standard format.

The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.

OffsetDateTime

Your input string contains an offset-from-UTC, but not a time zone. So parse as an OffsetDateTime.

OffsetDateTime odt = OffsetDateTime.parse( "2011-02-27T10:03:33.099-06:00" ) ;

ZonedDateTime

If you know for certain the intended time zone, apply a ZoneId to produce a ZonedDateTime.

A time zone is always preferable to a mere offset. A zone is a history of past, present, and future changes to the offset used by the people of a particular region.

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/Galapagos" ) ;  
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;

Converting legacy ↔ modern

If you must have a GregorianCalendar object to inter-operate with old code not yet updated to java.time, you can convert. Look to new methods added to the old classes.

GregorianCalendar myGregCal = GregorianCalendar.from( zdt ) ;

And going the other direction…

ZonedDateTime zdt = myGregCal.toZonedDateTime() ;

Table of date-time types in Java, both modern and legacy.png


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.

Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.

Where to obtain the java.time classes?

Table of which java.time library to use with which version of Java or Android

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.

Share:
10,528
vjk
Author by

vjk

Updated on June 04, 2022

Comments

  • vjk
    vjk about 2 years

    I am getting time in string like this "2011-02-27T10:03:33.099-06:00" which is of xml dateTime type. I also have timezone of TimeZone type. How should I convert the dateTime to GregorianCalendar java type in that timezone.