乔达时间的DateTime转换为java.util.Date奇怪的问题 [英] Joda time's DateTime converted to java.util.Date strange issue
问题描述
我遇到了一个奇怪的问题.这是描述它的代码片段:
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屋!