Jodatime将joda DATETIME转换为java.util.Date

时间:2014-07-11 10:17:29

标签: java jodatime

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;
}

0 个答案:

没有答案