2

I am implementing the following method-

DateTime getDateTime(Date srcDate, String destTimeZone) {
}

As the input is of Date object, I can safely assume the timezone of it as "UTC". I have to convert it to destTimeZone and return DateTime object.

Any pointers in an efficient way to solve this?

JodaStephen
  • 60,927
  • 15
  • 95
  • 117
Teja
  • 341
  • 2
  • 7
  • 18

2 Answers2

1

Such a method is not really hard to implement with Joda Time:

public DateTime getDateTime( Date srcDate, String destTimeZone )
{
    return new DateTime( srcDate, DateTimeZone.forID( destTimeZone) );
}

The standard Java way would be:

Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( destTimeZone ) );
cal.setTimeInMillis( srcDate.getTime() );
// now you have a Calendar object with time zone set
x4u
  • 13,877
  • 6
  • 48
  • 58
0
DateTime getDateTime(Date srcDate, String destTimeZone) {

    return new DateTime(new Date(srcDate.getTime() + 
                   TimeZone.getTimeZone(destTimeZone).getRawOffset()));

}
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • 1
    This doesn't give correct results for timezones that can switch to daylight saving time and back and it would confuse Joda since it expects a Date to be always in UTC/GMT as usual in Java. – x4u Jun 03 '11 at 20:09