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
  • 57,570
  • 15
  • 91
  • 113
Teja
  • 331
  • 1
  • 6
  • 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,537
  • 5
  • 47
  • 57
0
DateTime getDateTime(Date srcDate, String destTimeZone) {

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

}
Bala R
  • 104,615
  • 23
  • 192
  • 207
  • 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