我需要将日期从 Google App Engine 本地服务器时区转换为 Java 中的太平洋时间。
我尝试使用
Calendar calstart =
Calendar.getInstance();
calstart.setTimeZone(TimeZone.getTimeZone("PST"));
//calstart.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
Date startTime = calstart.getTime();
但这给了我不正确的时间(大约下午 4 点,而实际太平洋标准时间是晚上 10 点)。还尝试了注释行America/Los_Angeles
但在 GAE 服务器上给出的时间不正确。
有什么想法/建议吗?
Using 乔达时间 http://joda-time.sourceforge.net/,您所需要的只是日期时间.withZone http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#withZone%28org.joda.time.DateTimeZone%29方法。下面的例子:
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();
}
建议不要使用默认 API 进行与时间相关的计算。这太糟糕了。 Joda 似乎是最好的替代 API。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)