新Date()和日历日期之间的差异 [英] Difference between new Date() and Calendar date

查看:247
本文介绍了新Date()和日历日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两个日期在实践中有什么区别?

What is the difference between the two dates below in practice?


  1. 日期日期=新日期();

日期日期= Calendar.getInstance()。getTime();

据我所知,新的Date()是基于UTC / GMT的日期,而日历的getTime()是基于TimeZone&系统时间。我对吗?我还会遗漏一些东西吗?

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?

此外,如果我的上述理解是正确的,我可以说以下两个函数的最终结果完全相同吗?

Moreover, if my above understanding is correct, can I say that the end results of the following two functions are exactly the same ?

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.

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!

推荐答案

关于Java日历和日期的实用信息

如果你想在Java程序中使用不同的日期,你将使用Java Calendar类。

If you want to operate with different dates in your Java program you will use Java Calendar class.

我将尝试给你一些概述关于Java Calendar和Date类的广为人知的事实,工作代码示例,您可以立即尝试。

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.

有关Calendar类的基本信息由Java API提供。 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...

Java日期和日历类有什么区别?

What is difference between Java Date and Calendar classes?

日期和日期之间的区别日历是Date类以特定的即时时间运行,Calendar以两个日期之间的差异运行。 Calendar类使您可以在特定时刻和一组日历字段之间进行转换,例如HOUR,YEAR,MONTH,DAY_OF_MONTH。您还可以使用日历字段进行操作,例如获取祖母生日的日期:)。

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 :).

我想指出一些关于日历和日期的事情你应该知道哪些不明显...

I would like to point some things about Calendar and Date which you should know and which are not obvious...

闰秒。

年,月,日和小时是在正常范围内如:

Years, months, dates and hours are in "normal" range like:

一年y - 1900.
一个月从0到11
一个日期(日期)来自通常的方式是1到31。日历闰秒
小时0到23.
以通常方式从0到59分钟。
但是,注意!!第二个是从0到61的整数表示。看起来很奇怪 - 61秒,但不要忘记闰秒。每年或每两年大约有一次,称为闰秒。闰秒总是作为当天的最后一秒添加,并且总是在12月31日或6月30日。例如,由于增加了闰秒,1995年的最后一分钟长度为61秒。

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.

宽松字段。

另一个有趣的功能是日历中的宽松和非宽松字段。那是什么?示例:

Another funny feature is lenient and non-lenient fields in calendar. What is that? Example:

2006年1月32日。实际上,如果您设置日历宽松,它将是2006年2月1日,对您的程序没问题:)。如果是非宽松的,则会抛出ArrayIndexOutOfBoundsException异常。

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.

另一个问题是00:00结束还是开始?是凌晨00点。还是P.M.?是午夜和中午A.M.或P.M?

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?

答案:23:59是当天的最后一分钟,00:00是第二天的第一分钟。午夜属于am,中午属于pm,因此在同一天,凌晨12:00(午夜)<上午12:01和中午12:00(中午)<下午12:01。

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.

可能最后一个问题:什么是时代?以及为什么这个纪元自1970年1月1日00:00:00.000 GMT。

And probably last question: what is epoch? and why this Epoch since January 1, 1970 00:00:00.000 GMT.

实际上它是Unix时间,或POSIX时间,是一个描述时间点的系统:它是1970年1月1日00:00:00 UTC之后的秒数。

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.

等等,还有一个问题!

如果我们使用自Epoch以来计算的时间,我怎么知道哪些年份有闰秒而哪些年份没有?

"If we use the time which is counted since Epoch, how can I know which years had leap seconds and which not?"

答案:制作生活更轻松的闰秒不计算在内。 Java Date类占用OS的实际时间,大多数现代计算机都不能使用闰秒,它们的内部时钟不是那么精确。这就是为什么需要定期进行时间同步的原因。

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.

这篇关于新Date()和日历日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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