[英]Joda Time - Convert UTC DateTime to Date - Using a Version 1.2.1.1 of Joda
大家,早安。
我想帮助您了解如何使用1.2.1.1版本的Joda Time将org.joda.time.DateTime转换为java.util.Date。
为什么选择乔达1.2.1.1? 因为当前我只能“不幸地”使用此版本的Joda。
我的测试>
System.out.println("JODA Version : 2.8.2 - UTC TIME to Date " + new DateTime().withZone(DateTimeZone.UTC).toLocalDateTime().toDate());;
System.out.println("JODA Version : 1.2.1.1 - UTC TIME to Date " + new DateTime().withZone(DateTimeZone.UTC).toDate());;
JODA Version : 2.8.2 - UTC TIME to Date Fri Sep 18 17:34:36 BRT 2015
JODA Version : 1.2.1.1 - UTC TIME to Date Fri Sep 18 14:34:36 BRT 2015
我的问题是,在1.2.1.1版中,日期位于我的本地设置上,而在此版本中,没有toLocalDateTime()方法。
我想请您的帮助和经验来发现最佳实践,以便在JODA版本:1.2.1.1中执行此转换。
在旧版本的JODA中,如何在UTC中将时间转换为小时:分钟:秒?
我做了很多研究,发现有人说这样做会是一个好习惯吗?
public static Date converterDateTimeToUTCDate(final DateTime dateTime) throws ParseException {
DateTime dat = dateTime.withZone(DateTimeZone.UTC);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").parse(dat.toString("yyyy-MM-dd HH:mm:ss:SSS"));
}
public static void main(String[] args) throws ParseException {
System.out.println("TODO DATE UTC TIME : " + new DateTime().withZone(DateTimeZone.UTC));
System.out.println("TODO DATE Convertion Direct Date: " + new DateTime().withZone(DateTimeZone.UTC).toDate());
Date converterUTCDateTimeToDate = converterDateTimeToUTCDate(new DateTime());
System.out.println("TODO DATE UTC with Parse : " + converterUTCDateTimeToDate);
}
结果:
TODO DATE UTC TIME : 2015-09-18T22:33:57.353Z
TODO DATE Convertion Direct Date: Fri Sep 18 19:33:57 BRT 2015
TODO DATE UTC with Parse : Fri Sep 18 22:33:57 BRT 2015
编辑为什么选择乔达1.2.1.1? 因为当前我只能“不幸地”使用此版本的Joda。
我在一家公司中工作,该公司需要很长时间才能在项目中更改API版本,而我的项目没有此等待时间,因此无法使用新版本
更新:
我看了一下,java Date没有TimeZone,这个BRT是在类的toString()方法中从本地计算机捕获的,我可以认为转换是正确的吗?
更新2
我为示例编辑了答案示例:
看到:
package joda;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
public class ConverterDateTimeToDate {
public static final String BASE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS z";
public static void main(String[] args) {
// Different display time zones
SimpleDateFormat formatUTC = new SimpleDateFormat( BASE_FORMAT );
formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat formatBrazil = new SimpleDateFormat( BASE_FORMAT );
formatBrazil.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
SimpleDateFormat formatCentralEurope = new SimpleDateFormat( BASE_FORMAT );
formatCentralEurope.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam"));
// Get a date in UTC
String dateString = "2015-09-19 10:45:00.000 UTC";
Date javaDate = null;
try {
DateTime dateTime = new DateTime().withZone(DateTimeZone.forID("America/Mexico_City"));
System.out.println("MEX TIME IN JODA : " + dateTime); //new Test
System.out.println("MEX TIME IN JODA CONVERTER : " + dateTime.toDate()); // new Test
System.out.println("Now in MEX Time Zone DateTime : " + dateTime);
javaDate = formatUTC.parse(dateTime.toString(BASE_FORMAT));
} catch (ParseException e) {
e.printStackTrace(); // Shouldn't happen.
}
// Now let's print it in various time zones. It's the same date - 10:45 in UTC!
System.out.println("In UTC: " + formatUTC.format(javaDate));
System.out.println("In Brazil: " + formatBrazil.format(javaDate));
System.out.println("In the Netherlands: " + formatCentralEurope.format(javaDate));
}
}
我的输出结果:在UTC中输入:2015-09-19 12:10:56.731 CDT ,我的转换问题吗? 因为我在系统中的DateTime来了
我的输出:
MEX TIME IN JODA : 2015-09-21T21:17:46.781-05:00
MEX TIME IN JODA CONVERTER : Mon Sep 21 23:17:46 BRT 2015
Now in MEX Time Zone DateTime : 2015-09-21T21:17:46.781-05:00
In UTC: 1732-01-11 02:17:46.781 UTC
In Brazil: 1732-01-10 23:17:46.781 BRT
In the Netherlands: 1732-01-11 03:17:46.781 CET
toDate()
即使在较旧的JodaTime中, DateTime
类中的方法也是正确的。 这里是一个解释:
java.util.Date
如何工作的说明 您似乎对java.util.Date
有误解。 它不包含时区。 它表示自1970年1月以来的时间偏移量, UTC时间00:00。
当您打印Date
对象时,您的JVM将使用您的默认时区并向您显示该时区的Date
。 因此,在打印日期时,如果要在其他时区查看日期,则应始终使用DateFormat
对象。 例如,如果要查看UTC日期,则必须使用其时区设置为UTC的日期格式。 这是一个例子:
public static final String BASE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS z";
public static void main(String[] args) {
// Different display time zones
SimpleDateFormat formatUTC = new SimpleDateFormat( BASE_FORMAT );
formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat formatBrazil = new SimpleDateFormat( BASE_FORMAT );
formatBrazil.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
SimpleDateFormat formatCentralEurope = new SimpleDateFormat( BASE_FORMAT );
formatCentralEurope.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam"));
// Get a date in UTC
String dateString = "2015-09-19 10:45:00.000 UTC";
Date javaDate = null;
try {
javaDate = formatUTC.parse(dateString);
} catch (ParseException e) {
e.printStackTrace(); // Shouldn't happen.
}
// Now let's print it in various time zones. It's the same date - 10:45 in UTC!
System.out.println("In UTC: " + formatUTC.format(javaDate));
System.out.println("In Brazil: " + formatBrazil.format(javaDate));
System.out.println("In the Netherlands: " + formatCentralEurope.format(javaDate));
}
该程序的输出为:
In UTC: 2015-09-19 10:45:00.000 UTC In Brazil: 2015-09-19 07:45:00.000 BRT In the Netherlands: 2015-09-19 12:45:00.000 CEST
您可以看到我们打印了相同的日期 ,并且根据格式的时区显示的日期有所不同。
TimeStamp
正确转换为java.util.Date
对于Joda的DateTime
对象,逻辑相同,但更复杂,因为它还包括时区,尽管并非用于所有操作。 在内部,它也表示与UTC的偏移量。
当使用其toDate()
方法时,它依赖于UTC的内部偏移量,因此,它根据java.util.Date
协定为您提供了正确的Date
对象 。
让我们演示一下,通过将上述程序中获取日期的方式替换为:
DateTime jodaDateTime = new DateTime( 2015, 9, 19, 10, 45, 0, 0, DateTimeZone.UTC);
Date javaDate = jodaDateTime.toDate();
现在,运行与以前相同的打印,我们再次得到:
In UTC: 2015-09-19 10:45:00.000 UTC In Brazil: 2015-09-19 07:45:00.000 BRT In the Netherlands: 2015-09-19 12:45:00.000 CEST
因此,您可以看到,如果正确设置了Joda DateTime
,则使用其toDate
将为您提供正确的Date
对象。
toLocalDateTime()
是错误的 现在,如果我们使用第一种方法,那么您认为正确的方法(仅在Joda 2.0及更高版本中存在),我们将得到什么?
我们将代码更改为:
DateTime jodaDateTime = new DateTime( 2015, 9, 19, 10, 45, 0, 0, DateTimeZone.UTC);
Date javaDate = jodaDateTime.toLocalDateTime().toDate();
Joda DateTime
与以前相同,我们只是添加了仅在Joda 2中存在的toLocalDateTime()
。
假设系统上的默认时区是BRT,您将得到以下结果:
In UTC: 2015-09-19 13:45:00.000 UTC In Brazil: 2015-09-19 10:45:00.000 BRT In the Netherlands: 2015-09-19 15:45:00.000 CEST
这显然不是正确的日期! toLocalDateTime()
部分获取了您的本地时间偏移,并将其添加到日期中以形成“本地”日期。 只要您停留在Joda时间构造内,这就很好,但是因为它设置了与UTC不正确的偏移量,所以违反了java.util.Date
的约定。
在旧的Joda中使用的旧方法是从org.joda.time.DateTime
获取适当的java.util.Date
的org.joda.time.DateTime
。 但是您在打印java.util.Date
必须非常小心,因为默认情况下它将在您的默认时区打印。
最后一个建议:如果您想在公司中开始升级过程,请不要担心Joda的时间。 请他们开始将系统升级到Java 8,这是Oracle唯一维护的Java版本。 Java 8包括适当的日期/时间库,Joda的创建者建议改用该库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.