开发者

Java Calendar timezone converting to GMT problem

I have one Calendar object which is as per the user's time zone which may be PST etc, now i want to convert the same to GMT and retain the time i.e. is the calendar initially was set @ 00:00:00 at PST it should be converted to 08开发者_运维技巧:00:00 after the conversion taking into consideration the time/date difference . Can someone provide me some help on this.

Appreciate the help in advance.

Thanks,

Vaibhav


Just create a new Calendar in GMT, set the time in that calendar to the same as the original calendar, and you're done:

gmtCalendar.setTime(userCalendar.getTime());

That should be fine, as the getTime() call returns the instant in time (i.e. a java.util.Date with no associated time zone).

As ever though, if you're doing any significant amount of date/time work in Java you should strongly consider using Joda Time instead.


tl;dr

( ( GregorianCalendar ) myCal )  // Cast from a general `Calendar`  to  specific subclass `GregorianCalendar`. 
    .toZonedDateTime()           // Convert from troublesome legacy class to modern java.time class, `ZonedDateTime`.
    .toInstant()                 // Extract a UTC-specific value, an `Instant` object.

java.time

The modern approach uses java.time classes.

Convert your legacy Calendar object (if GregorianCalendar) to a ZonedDateTime. Call new conversion methods added to the old classes.

GregorianCalendar gc = ( GregorianCalendar ) myCal ;
ZonedDateTime zdt = gc.toZonedDateTime() ;

Now extract an Instant, a value always in UTC. You can think of it this way conteptually: ZonedDateTime = ( Instant + ZoneId )

Instant instant = zdt.toInstant() ;

For more flexibility such as generating strings in various formats, convert to an OffsetDateTime object.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;

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?

  • 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜