I need to convert date from Google App Engine local server time zone to pacific time in Java.
I tried using
Calendar calstart =
Calendar.getInstance();
calstart.setTimeZone(TimeZone.getTimeZone("PST"));
//calstart.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
Date startTime = calstart.getTime();
but this gives me incorrect time (some 4pm when actual PST is 10pm). Also tried commented line America/Los_Angeles
but gives incorrect time on GAE server.
Any thoughts/advice?
Using Joda Time, all you need is the DateTime.withZone method. Example below:
public static Date convertJodaTimezone(LocalDateTime date, String srcTz, String destTz) {
DateTime srcDateTime = date.toDateTime(DateTimeZone.forID(srcTz));
DateTime dstDateTime = srcDateTime.withZone(DateTimeZone.forID(destTz));
return dstDateTime.toLocalDateTime().toDateTime().toDate();
}
As an advice, never use the default API for time-related calculations. It is just awful. Joda seems to be the best replacement API around.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With