Teja
Teja

Reputation: 341

Timezone conversion in java

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?

Upvotes: 2

Views: 2032

Answers (2)

x4u
x4u

Reputation: 14077

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

Upvotes: 1

Bala R
Bala R

Reputation: 109017

DateTime getDateTime(Date srcDate, String destTimeZone) {

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

}

Upvotes: 0

Related Questions