Difference between new Date() and Calendar date

后端 未结 2 1467
时光取名叫无心
时光取名叫无心 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.)

提交回复
热议问题