Joda时间-使用Uda版本1.2.1.1将UTC日期时间转换为日期 [英] Joda Time - Convert UTC DateTime to Date - Using a Version 1.2.1.1 of Joda
问题描述
大家早上好。
我想帮助您了解如何完成org.joda.time.DateTime到java.util的转换。请使用1.2.1.1版本的Joda Time。
I'd like to help you to see how I can accomplish the conversion of a org.joda.time.DateTime to java.util.Date using the 1.2.1.1 version of Joda Time.
为什么使用Joda 1.2.1.1?因为目前我只能不幸地使用此版本的Joda。
Why Joda 1.2.1.1 ? Because currently I can only work with this version of Joda "unfortunately".
我的测试>
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()方法。
My problem being that in version 1.2.1.1 the Date is on my local settings and in this version there is no toLocalDateTime() method.
我会请您的帮助和经验来发现最佳实践,以执行此转换在JODA版本中:1.2.1.1
I would ask the help and experience of you to discover the best practices in order to perform this conversion in JODA Version: 1.2.1.1
如何将这种旧版本的JODA转换为小时:分钟:在UTC中是秒?
How can I perform this conversion to the hour: minute: second in UTC this older version of JODA?
我做了很多研究,发现有人说这样做会是一个好习惯吗?
I researched a lot and saw some people talking to do so would be a good practice?
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。
EDIT Why Joda 1.2.1.1 ? Because currently I can only work with this version of Joda "unfortunately".
我在一家公司工作,该公司需要很长时间才能在其中更改API版本该项目和我的项目没有此等待时间,请使用新版本
I work in a company where there is a very long process to change the version of an API within the project and my project does not have this waiting time, to use a new version
更新:
我看了看,java Date没有TimeZone,这个BRT是在类的toString()方法中从我的本地计算机捕获的,我可以认为然后进行转换是正确的吗?
I looked and java Date has no TimeZone, this BRT is caught from my local machine in the toString () method of the class, I can consider that it is right to convert then?
更新2
我为示例编辑了答案示例:
I edited the example of my answer for an example:
请参阅:
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在系统中已经消失了
My Out Put : in In UTC: 2015-09-19 12:10:56.731 CDT, Was the problem with my convertion ? Because my DateTime in System it's come this aways
我的输出:
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()
DateTime
类,即使在旧的JodaTime中也是正确的。这是一个解释:
The correct way is to use toDate()
The method in the DateTime
class, even in old JodaTime, is the correct one. Here is an explanation:
关于 java.util.Date的解释
有效
您似乎对 java.util.Date
有误解。它不包含时区。它表示自1970年1月以来的时间偏移量,时间为 UTC 。
You seem to have a misconception about java.util.Date
. It does not contain a time zone. It is a representation of a time offset since January 1970, 00:00 UTC.
打印日期时
对象,您的JVM将使用您的默认时区,并向您显示该时区的日期
。因此,在打印日期时,如果要在其他时区查看日期,则应始终使用 DateFormat
对象。例如,如果要查看UTC日期,则必须使用其时区设置为UTC的日期格式。下面是一个示例:
When you print a Date
object, your JVM takes your default time zone and shows you the Date
at that time zone. So when you print dates, you should always use a DateFormat
object if you want to look at them in a different time zone. For example, if you want to see what the date is in UTC, you have to use a date format with its time zone set to UTC. Here is an example:
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
您可以看到我们在同一日期打印了 >-并且根据格式的时区 显示不同。
You can see we printed the same date - and it shows up differently based on the time zone of the format.
对于Joda的 DateTime
对象来说,同样的逻辑是正确的,但是它更为复杂,因为它还包括一个时区,尽管并不是所有操作都使用它。在内部,它也表示相对于UTC的偏移量。
The same logic is true for Joda's DateTime
object, but it's more complex, because it also includes a time zone, though it is not used for all operations. Internally, it also represents an offset from UTC.
当使用其 toDate()
方法时,它依赖于相对于UTC的内部偏移量,因此它根据 java.util.Date <>为您提供了正确的
Date
对象 / code>合同。
When you use its toDate()
method, it relies on that internal offset from UTC, thus it is giving you the correct Date
object according to the java.util.Date
contract.
让我们演示一下,通过将上述程序中获取日期的方式替换为:
Let's demonstrate that by replacing the way we get the date in the above program to:
DateTime jodaDateTime = new DateTime( 2015, 9, 19, 10, 45, 0, 0, DateTimeZone.UTC);
Date javaDate = jodaDateTime.toDate();
现在,运行与以前相同的打印,再次得到:
Now, running the same prints as before, again we get:
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
对象。
So you see, if the Joda DateTime
was appropriately set, then using its toDate
gives you the correct Date
object.
现在,如果我们使用第一种方法,那么您认为正确的方法(仅在Joda 2.0及更高版本中存在),我们将得到什么?
Now, if we use your first method, the one you thought was correct, which only exists in Joda 2.0 and above, what will we get?
我们将代码更改为:
DateTime jodaDateTime = new DateTime( 2015, 9, 19, 10, 45, 0, 0, DateTimeZone.UTC);
Date javaDate = jodaDateTime.toLocalDateTime().toDate();
Joda DateTime
与以前相同,我们只添加了仅在Joda 2中存在的 toLocalDateTime()
。
The Joda DateTime
is the same as before, we just added the toLocalDateTime()
that exists only in Joda 2.
假定您的默认时区系统是BRT,您会得到以下结果:
Assuming the default time zone on your system is BRT, you get the result:
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的错误偏移量。
This is clearly not the correct date! The toLocalDateTime()
part took your local time offset and added it to the date to make a "local" date. Which is good as long as you stay within Joda time constructs, but breaks the contract for java.util.Date
because it sets the incorrect offset from UTC.
您在旧Joda中使用的旧方法是最好的方法从 org.joda.time.DateTime
获得适当的 java.util.Date
。但是,在打印 java.util.Date
时必须非常小心,因为它将默认在您的默认时区打印。
The old method you had in the old Joda is the best one to get an appropriate java.util.Date
from an org.joda.time.DateTime
. But you have to be very careful at how you print the java.util.Date
because it will be printed by default in your default time zone.
最后一条建议:如果您想在公司中开始升级过程,请不要担心Joda的时间。要求他们开始将系统升级到Java 8,这是Oracle唯一维护的Java版本。 Java 8包含适当的日期/时间库,Joda的创建者建议改用该日期/时间库。
One last advice: if you want to start the upgrade process in your company, don't bother with Joda time. Ask them to start upgrading the system to Java 8, which is currently the only Java version maintained by Oracle. Java 8 includes a proper Date/Time library and the creators of Joda recommend switching to using that.
这篇关于Joda时间-使用Uda版本1.2.1.1将UTC日期时间转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!