Est*_*dão 0 java datetime utc jodatime
大家,早安.
我想帮助您了解如何使用1.2.1.1版本的Joda Time完成org.joda.time.DateTime到java.util.Date的转换.
为何选择Joda 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
Run Code Online (Sandbox Code Playgroud)
我的问题是在版本1.2.1.1中,日期在我的本地设置上,在这个版本中没有toLocalDateTime()方法.
我会请求您的帮助和经验,以便在JODA版本:1.2.1.1中发现执行此转换的最佳实践
如何执行此转换为小时:分钟:UTC这个旧版JODA的第二个?
我研究了很多,看到一些人这样说会是一个好习惯吗?
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);
}
Run Code Online (Sandbox Code Playgroud)
结果:
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
Run Code Online (Sandbox Code Playgroud)
编辑 为什么Joda 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));
}
}
Run Code Online (Sandbox Code Playgroud)
My Out Put:in in 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
Run Code Online (Sandbox Code Playgroud)
toDate()类中的方法DateTime,即使在旧的JodaTime中,也是正确的方法.这是一个解释:
java.util.Date工作的说明你似乎对此有误解java.util.Date.它不包含时区.它表示自1970年1月00:00 UTC以来的时间偏移.
打印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));
}
Run Code Online (Sandbox Code Playgroud)
该程序的输出是:
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的内部偏移量,因此它根据合同为您提供了正确的Date对象java.util.Date.
让我们通过将上述程序中的日期替换为:
DateTime jodaDateTime = new DateTime( 2015, 9, 19, 10, 45, 0, 0, DateTimeZone.UTC);
Date javaDate = jodaDateTime.toDate();
Run Code Online (Sandbox Code Playgroud)
现在,运行与以前相同的打印件,我们再次获得:
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();
Run Code Online (Sandbox Code Playgroud)
Joda DateTime和以前一样,我们只添加了toLocalDateTime()Joda 2中存在的那个.
假设系统上的默认时区是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时间结构内,这是好的,但是java.util.Date因为它设置了与UTC不正确的偏移而违反了合同.
你在旧的Joda中使用的旧方法是最好的方法来获得适当java.util.Date的org.joda.time.DateTime.但是你必须非常小心打印java.util.Date它,因为它将默认打印在你的默认时区.
最后一条建议:如果您想在公司开始升级过程,请不要打扰Joda时间.请他们开始将系统升级到Java 8,这是目前Oracle维护的唯一Java版本.Java 8包含一个正确的日期/时间库,Joda的创建者建议切换到使用它.
| 归档时间: |
|
| 查看次数: |
4711 次 |
| 最近记录: |