Date.toString() 显示什么时区? [英] What time zone does Date.toString() display?

查看:27
本文介绍了Date.toString() 显示什么时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,方法是什么时区 java.util.Date.toString() 显示?自 Java Date 将给定日期存储在 UTC 中,并且不包含任何明确的时区,Date.toString() 只显示主机的默认时区,或 TimeZone.getDefault()?

By default, what time zone does method java.util.Date.toString() display? Since a Java Date stores a given date in UTC and doesn't contain any explicit time zone, does Date.toString() simply display the default time zone for the host, or TimeZone.getDefault()?

推荐答案

它使用 TimeZone.getDefault() 显示,反过来,它将默认为其运行的操作系统的时区on(即主机).所以在实践中,它们是一样的

It displays using TimeZone.getDefault() which, in turn, will default to the time zone of the operating system it is running on (i.e. the host). So in practice, they will be the same thing

请注意,Java 日期并不是真正的日期!它是一个时间瞬间,由 纪元开始后的毫秒偏移量表示/em>.它仍然包含引用 yearmonth 等的方法,但这些方法都已弃用.您绝不应该像使用日期一样使用 Date 对象.

Note that a Java date is not really a date! It is an instant in time, represented by a millisecond offset since the beginning of the epoch. It still contains methods which reference year, month etc but these are all deprecated. You should on no account be using a Date object as if it were a date.

使用 Calendar(尽管这可能比 Date 更糟糕)或像 JODA 这样的体面的库.

Use a Calendar (although this is arguably even more broken than Date) or a decent library like JODA.

这篇关于Date.toString() 显示什么时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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