API help - convert LocalTime to new time zone

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

API help - convert LocalTime to new time zone

Ken Hausam
I have a LocalTime variable and a separate DateTimeZone variable and want to know what the corresponding local time will be in a different timezone. I read the user guide for how to do this for a DateTime class (an instant), but it seems like it should be a valid operation on a partial as well as long as the old and new time zones are specified.

I guess one option is to convert my LocalTime to a DateTime using LocalTime's toDateTimeToday() method: http://joda-time.sourceforge.net/api-release/org/joda/time/LocalTime.html#toDateTimeToday(org.joda.time.DateTimeZone)

then performing the conversion as specified in the user guide:

// translate to London local time
    DateTime dtLondon = dt.withZone(DateTimeZone.forID("Europe/London"));

And then converting back to a LocalTime using DateTime's toLocalTime() method: http://joda-time.sourceforge.net/api-release/org/joda/time/DateTime.html#toLocalTime()

But this all seems convoluted. Doesn't seem like I should have to specify arbitrary date fields just to get the time in a different timezone. Are there any other ways to do this?

Thanks,
Ken






------------------------------------------------------------------------------
EMC VNX: the world's simplest storage, starting under $10K
The only unified storage solution that offers unified management
Up to 160% more powerful than alternatives and 25% more efficient.
Guaranteed. http://p.sf.net/sfu/emc-vnx-dev2dev
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: API help - convert LocalTime to new time zone

Tauren Mills

The corresponding time in a different timezone is not always the same. The only way to accurately calculate the time is to know the date as well. Without a date, you cannot know if daylight savings time should be taken into account. Keep in mind that DST starts and stops on different dates in different places and different years, and many locations dont even have DST.

Furthermore, timezone rules change, so the corresponding local time in another zone might be different this year than it was 20 years ago.

Tauren

On Aug 25, 2011 11:10 PM, "Tauren Mills" <[hidden email]> wrote:
> The corresponding time in a different timezone is not always the same. The
> only way to accurately calculate the time is to know the date as well.
> Without a date, you cannot know if daylight savings time should be taken
> into account. Keep in mind that DST starts and stops on different dates in
> different places and different years, and many locations dont even have
> DST.
>
> Furthermore, timezone rules change, so the corresponding local time in
> another zone might be different this year than it was 20 years ago.
>
> Tauren
> On Aug 25, 2011 6:58 PM, "Ken Hausam" <[hidden email]> wrote:
>> I have a LocalTime variable and a separate DateTimeZone variable and want
> to know what the corresponding local time will be in a different timezone. I
> read the user guide for how to do this for a DateTime class (an instant),
> but it seems like it should be a valid operation on a partial as well as
> long as the old and new time zones are specified.
>>
>> I guess one option is to convert my LocalTime to a DateTime using
> LocalTime's toDateTimeToday() method:
> http://joda-time.sourceforge.net/api-release/org/joda/time/LocalTime.html#toDateTimeToday(org.joda.time.DateTimeZone)
>>
>> then performing the conversion as specified in the user guide:
>>
>> // translate to London local time
>> DateTime dtLondon = dt.withZone(DateTimeZone.forID("Europe/London"));
>> And then converting back to a LocalTime using DateTime's toLocalTime()
> method:
> http://joda-time.sourceforge.net/api-release/org/joda/time/DateTime.html#toLocalTime()
>>
>> But this all seems convoluted. Doesn't seem like I should have to specify
> arbitrary date fields just to get the time in a different timezone. Are
> there any other ways to do this?
>>
>> Thanks,
>> Ken
>>
>>
>>
>>
>>

------------------------------------------------------------------------------
EMC VNX: the world's simplest storage, starting under $10K
The only unified storage solution that offers unified management
Up to 160% more powerful than alternatives and 25% more efficient.
Guaranteed. http://p.sf.net/sfu/emc-vnx-dev2dev
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...