Difference between new Date() and Calendar date

后端 未结 2 1468
时光取名叫无心
时光取名叫无心 2021-02-06 04:38

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

  1. Date date = new Date();

  2. Date date = Calendar.getInstance().g

相关标签:
2条回答
  • 2021-02-06 04:51

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

    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!

    To try to explain in different terms, the java.util.Date for

    10:49 pm Dec 19, 2013 UTC

    And

    5:49 pm Dec 19, 2013 US Eastern Time

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

    0 讨论(0)
  • 2021-02-06 05:08

    Practical info about Java Calendar and Date

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

    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.

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

    What is difference between Java Date and Calendar classes?

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

    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.

    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.

    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?

    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.

    And probably last question: what is epoch? and why this Epoch since January 1, 1970 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.

    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?"

    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.

    0 讨论(0)
提交回复
热议问题