Java new Date() 和日历日期的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20693131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between new Date() and Calendar date
提问by Leem.fin
What is the difference between the two dates below in practice?
以下两个日期在实践中有何不同?
Date date = new Date();
Date date = Calendar.getInstance().getTime();
Date date = new Date();
Date date = Calendar.getInstance().getTime();
What I understand is that new Date() is a UTC/GMT based date while calendar's getTime() is based on TimeZone & System time. Am I right? Do I miss something still?
我的理解是 new Date() 是基于 UTC/GMT 的日期,而日历的 getTime() 是基于 TimeZone 和系统时间。我对吗?我还想念什么吗?
Moreover, if my above understanding is correct, can I say that the end results of the following two functions are exactly the same ?
而且,如果我上面的理解是正确的,我能说下面两个函数的最终结果是完全一样的吗?
1.
1.
public String getDate1(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//I set the time zone & pass the new Date()
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(new Date());
}
2.
2.
public String getDate2(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//I didn't set the time zone because I think calendar instance will handle timezone change
return sdf.format(Calendar.getInstance().getTime());
}
I appreciate if you could point out where I understand wrongly & explain to me clearly. Because I feel this thing is confused to me. Thanks!
如果您能指出我理解错误的地方并清楚地向我解释,我将不胜感激。因为我觉得这件事让我很困惑。谢谢!
回答by Affe
There is no difference between at all between those two dates. (The second one is of course a bit wasteful in allocating a Calendar object that you don't use.)
这两个日期之间根本没有区别。(第二个当然在分配一个你不使用的 Calendar 对象时有点浪费。)
An instance of java.util.Date is an absolute point in time. It has no knowledge of time zones. Setting the Default timezone on the SimpleDateFormat similarly does nothing, it uses the default by.... default!
java.util.Date 的一个实例是一个绝对的时间点。它不知道时区。在 SimpleDateFormat 上设置默认时区同样没有任何作用,它使用默认....默认!
To try to explain in different terms, the java.util.Date for
为了尝试用不同的术语来解释,java.util.Date for
10:49 pm Dec 19, 2013 UTC
UTC 时间 2013 年 12 月 19 日晚上 10:49
And
和
5:49 pm Dec 19, 2013 US Eastern Time
美国东部时间 2013 年 12 月 19 日下午 5:49
Is exactly the same object. The exact same java.util.Date represents both of those human-readable representations of time. The human-readable considerations only come into play when you use the formatter to turn it back and forth. (Hence why you set the timezone on the formatter, not on the date, date has no knowledge of what a timezone means.)
是完全一样的对象。完全相同的 java.util.Date 表示这两种人类可读的时间表示。只有当您使用格式化程序来回切换时,人类可读的考虑才会发挥作用。(因此,为什么您在格式化程序上设置时区,而不是在日期上,日期不知道时区的含义。)
回答by jimagic
Practical info about Java Calendar and Date
关于 Java 日历和日期的实用信息
If you want to operate with different dates in your Java program you will use Java Calendar class.
如果你想在你的 Java 程序中操作不同的日期,你将使用 Java Calendar 类。
I will try to give you some overview of not widely known facts about Java Calendar and Date classes, working code examples, which you can try right away.
我将尝试为您提供一些关于 Java Calendar 和 Date 类的不广为人知的事实的概述,工作代码示例,您可以立即尝试。
The basic information about Calendar class is provided by Java API. The Calendar class is about days, months and years. One could ask: is not Date class about the same? Not exactly...
Calendar 类的基本信息由 Java API 提供。Calendar 类是关于天、月和年的。有人可能会问:Date 类是不是差不多?不完全是...
What is difference between Java Date and Calendar classes?
Java Date 和 Calendar 类有什么区别?
The difference between Date and Calendar is that Date class operates with specific instant in time and Calendar operates with difference between two dates. The Calendar class gives you possibility for converting between a specific instant in time and a set of calendar fields such as HOUR, YEAR, MONTH, DAY_OF_MONTH. You can also manipulate with the calendar fields, for example getting the date of your grandmother birthday :).
Date 和 Calendar 之间的区别在于 Date 类以特定的瞬间进行操作,而 Calendar 则以两个日期之间的差异进行操作。Calendar 类使您可以在特定时刻和一组日历字段(例如 HOUR、YEAR、MONTH、DAY_OF_MONTH)之间进行转换。您还可以使用日历字段进行操作,例如获取您祖母的生日日期 :)。
I would like to point some things about Calendar and Date which you should know and which are not obvious...
我想指出一些你应该知道的关于日历和日期的事情,哪些是不明显的......
Leap seconds.
闰秒。
Years, months, dates and hours are in "normal" range like:
年、月、日期和小时在“正常”范围内,例如:
A year y - 1900. A month from 0 to 11 A date (day of month) from 1 to 31 in the usual manner. calendar leap seconds An hour 0 to 23. A minute from 0 to 59 in the usual manner. But, attention!! A second is represented by an integer from 0 to 61. Looks strange - 61 second, but do not forget about leap second. About once every year or two there is an extra second, called a "leap second." The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second.
y - 1900 年。从 0 到 11 的月份 以通常的方式从 1 到 31 的日期(月份中的某天)。日历闰秒 从 0 到 23 的小时。以通常的方式从 0 到 59 的分钟。但是,注意!!秒由 0 到 61 的整数表示。看起来很奇怪 - 61 秒,但不要忘记闰秒。大约每隔一两年就会多出一秒,称为“闰秒”。闰秒总是添加为当天的最后一秒,并且总是在 12 月 31 日或 6 月 30 日。例如,由于添加了闰秒,1995 年的最后一分钟长 61 秒。
Lenient fields.
宽容的领域。
Another funny feature is lenient and non-lenient fields in calendar. What is that? Example:
另一个有趣的功能是日历中的宽松和非宽松字段。那是什么?例子:
32 January 2006. Actually if you set your calendar lenient it will be 1 February 2006 and no problem for your program :). If it is non-lenient ArrayIndexOutOfBoundsException exception will be thrown.
2006 年 1 月 32 日。实际上,如果您将日历设置为宽松,那么它将是 2006 年 2 月 1 日,您的程序没有问题:)。如果是非宽松的 ArrayIndexOutOfBoundsException 异常将被抛出。
Another question is 00:00 end or beginning of day? Is 00:00 A.M. or P.M.? Are midnight and noon A.M. or P.M?
另一个问题是 00:00 结束还是一天开始?是 00:00 AM 还是 PM?午夜和中午是 AM 还是 PM?
Answer: 23:59 is the last minute of the day and 00:00 is the first minute of the next day. Midnight belongs to "am", and noon belongs to "pm", so on the same day, 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm.
答:23:59 是当天的最后一分钟,00:00 是第二天的第一分钟。午夜属于“am”,中午属于“pm”,所以同一天,12:00 am(午夜)< 12:01 am,12:00 pm(中午)< 12:01 pm。
And probably last question: what is epoch? and why this Epoch since January 1, 1970 00:00:00.000 GMT.
可能还有最后一个问题:什么是纪元?以及为什么这个纪元自 1970 年 1 月 1 日 00:00:00.000 GMT 开始。
Actually it is Unix time, or POSIX time, is a system for describing points in time: it is the number of seconds after 00:00:00 UTC, January 1, 1970.
实际上它是 Unix 时间,或 POSIX 时间,是一种描述时间点的系统:它是 1970 年 1 月 1 日 00:00:00 UTC 之后的秒数。
Wait, one question more!
等等,还有一个问题!
"If we use the time which is counted since Epoch, how can I know which years had leap seconds and which not?"
“如果我们使用从 Epoch 开始计算的时间,我怎么知道哪些年份有闰秒,哪些没有?”
Answer: To make life easier leap seconds are not counted. Java Date class takes actual time from OS and most of modern computers can not use leap seconds, their's internal clocks are not so precised. That's why periodical time synchronization is required.
答:为了让生活更轻松,闰秒不计算在内。Java Date 类从操作系统获取实际时间,大多数现代计算机不能使用闰秒,它们的内部时钟不是那么精确。这就是为什么需要定期时间同步的原因。