乔达时间的DateTime转换为java.util.Date奇怪的问题 [英] Joda time's DateTime converted to java.util.Date strange issue

查看:51
本文介绍了乔达时间的DateTime转换为java.util.Date奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题.这是描述它的代码片段:

I ran into a strange issue. Here is a snippet of code that describes it:

DateTimeZone dtz = DateTimeZone.forOffsetHours(0);

DateTime dt = new DateTime(dtz);

System.out.println(dt);
System.out.println(dt.toDate());

输出为:

2012-02-29T17:24:39.055Z
Wed Feb 29 19:24:39 EET 2012

我位于UTC + 2,但是此操作应该创建一个初始化为UTC时间的java.util.Date对象.我想念什么?

I'm located UTC+2, but this action is supposed to create a java.util.Date object which is initialized for UTC time. What am I missing?

推荐答案

Date根本不了解某个时区-它仅表示一个时间点(例如Joda Time的Instant类型).自Unix时代以来仅几毫秒.调用Date.toString()时,它总是 使用系统本地时区将其转换为可读的文本格式.

Date doesn't know about a time zone at all - it only represents an instant in time (like Joda Time's Instant type). It's just a number of milliseconds since the Unix epoch. When you call Date.toString(), it always uses the system local time zone for converting that into a readable text form.

因此,这里没有什么问题-只是对 java.util.Date行为或toString()行为含义的预期失败.

So there's nothing wrong here - just an expectations failure over either the meaning of java.util.Date or its toString() behaviour, or both.

(顺便说一句,最好使用 ,而不是创建自己的.)

(As an aside, prefer DateTimeZone.UTC over creating your own.)

这篇关于乔达时间的DateTime转换为java.util.Date奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆