是使用TimeZone的java.util.Date吗? [英] Is java.util.Date using TimeZone?

查看:141
本文介绍了是使用TimeZone的java.util.Date吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两台不同的电脑,每台都有不同的TimeZone。

I have 2 different computers, each with different TimeZone.

在一台电脑中,打印 System.currentTimeMillis(),然后在两台计算机中打印以下命令:
System.out.println(new Date(123456)); - > 123456代表数字来自计算机#1中的 currentTimeMillis

In one computer im printing System.currentTimeMillis(), and then prints the following command in both computers: System.out.println(new Date(123456)); --> 123456 stands for the number came in the currentTimeMillis in computer #1.

第二个打印(虽然键入的硬编码)导致不同的打印,在两台电脑上。
为什么是这样?

The second print (though typed hardcoded) result in different prints, in both computers. why is that?

推荐答案

一些细节如何。

java.util.Date 是与时区无关的。在javadoc中说得如此正确。

java.util.Date is timezone-independent. Says so right in the javadoc.

你想要一些特定的时区?这是 java.util.Calendar

You want something with respect to a particular timezone? That's java.util.Calendar.

棘手部分?当您打印此内容(使用 java.text.DateFormat 子类),那涉及日历(涉及时区)。请参阅 DateFormat.setTimeZone ()

The tricky part? When you print this stuff (with java.text.DateFormat or a subclass), that involves a Calendar (which involves a timezone). See DateFormat.setTimeZone().

它确实看起来(没有检查实现)像java.util.Date.toString()通过一个DateFormat。所以即使我们的(大多数)时区无关的类也被弄乱了时区。

It sure looks (haven't checked the implementation) like java.util.Date.toString() goes through a DateFormat. So even our (mostly) timezone-independent class gets messed up w/ timezones.

想要从我们的纯无名的Date对象中获取时区的东西?有 Date.toGMTString()。或者您可以创建自己的SimpleDateFormatter,并使用setTimeZone()来控制自己使用的区域。

Want to get that timezone stuff out of our pure zoneless Date objects? There's Date.toGMTString(). Or you can create your own SimpleDateFormatter and use setTimeZone() to control which zone is used yourself.

这篇关于是使用TimeZone的java.util.Date吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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