offsetTimeZone方法正确地从PST / PDT时区转换为UTC时区,但是我们需要java.util.Date类, 但是当我调用方法dateTime.toDate()时。日期转换回PST / PDT。我们如何在java.util.Date类中获取UTC日期时间?
public static void main(String[] args) {
String date ="2014-06-28T05:00:00";
LocalDateTime localDateTime = LocalDateTime.parse(date);
DateTime dateTime = offsetTimeZone(localDateTime,DateTimeZone.getDefault().getID(),DateTimeZone.UTC.getID());
System.out.println(dateTime.toString()); // output : 2014-06-28T12:00:00.000Z
System.out.println(dateTime.toDate()) // output : Sat Jun 28 05:00:00 PDT 2014
}
public static DateTime offsetTimeZone(LocalDateTime date, String srcTz, String destTz) {
DateTime srcDateTime = date.toDateTime(DateTimeZone.forID(srcTz));
DateTime dstDateTime = srcDateTime.withZone(DateTimeZone.forID(destTz));
return dstDateTime;
}