在Java中将日期从UTC转换为PST

Dav*_*ave 5 java timezone google-app-engine calendar date

我需要将日期从Google App Engine本地服务器时区转换为Java中的太平洋时间.

我试过用

Calendar calstart =
Calendar.getInstance();

calstart.setTimeZone(TimeZone.getTimeZone("PST"));
//calstart.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

Date startTime = calstart.getTime();
Run Code Online (Sandbox Code Playgroud)

但这给了我不正确的时间(当实际PST是晚上10点时,大约下午4点).还尝试了注释行,America/Los_Angeles但在GAE服务器上给出了不正确的时间.

有什么想法/建议吗?

mdr*_*drg 6

使用Joda Time,您只需要DateTime.withZone方法.示例如下:

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();
}
Run Code Online (Sandbox Code Playgroud)

作为建议,永远不要使用默认API进行与时间相关的计算.这太可怕了.Joda似乎是最好的替代API.