我正在尝试使用Joda Time将本地日期转换为 UTC 。我使用的代码如下所示,效果很好。
Date localDate = new Date();
System.out.println("Local Date : " + localDate);
DateTimeZone tz = DateTimeZone.getDefault();
Date utcDate = new Date(tz.convertLocalToUTC(localDate.getTime(), false));
System.out.println("UTC Date : " + utcDate);
Output :
Local Date : Wed May 29 11:54:46 EEST 2013
UTC Date : Wed May 29 08:54:46 EEST 2013
但是,如果我将 UTC 日期作为参数发送到DateTimeZone.convertLocalToUTC()方法,它也会将小时减少 3。但是,由于它是 UTC 日期,我希望它不会再次转换日期。这是一个错误还是我错过了什么?
Date localDate = new Date();
System.out.println("Local Date : " + localDate);
DateTimeZone tz = DateTimeZone.getDefault();
Date utcDate = new Date(tz.convertLocalToUTC(localDate.getTime(), false));
System.out.println("UTC Date : " + utcDate);
Date utcDate2 = new Date(tz.convertLocalToUTC(utcDate.getTime(), false));
System.out.println("UTC Date 2 : " + utcDate2);
Output :
Local Date : Wed May 29 11:54:46 EEST 2013
UTC Date : Wed May 29 08:54:46 EEST 2013
UTC Date 2 : Wed May 29 05:54:46 EEST 2013