strip time from timestamp

后端 未结 3 1893
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 18:50

Why cannot I clear the time from a timestamp this way:

one day == 24 * 3600 * 1000 == 86400000 milliseconds.

long ms = new Date().getTime();  //Mon Sep 03 10:06         


        
3条回答
  •  梦毁少年i
    2021-01-23 19:28

    Why cannot I clear time from timestamm this way

    You're correctly clearing the time part in UTC. The millisecond values in Date are always relative to January 1st 1970 midnight in UTC. However, you're not displaying it in UTC, because of the way Date.toString() works (it always uses the system local time zone). Note that a Date itself has no concept of a time zone. It's just a number of milliseconds since January 1st 1970 midnight UTC.

    The concept of "clearing a time from a timestamp" doesn't really make sense without specifying which time zone you're talking about, as the same timestamp will have different times of day (and even dates) in different time zones.

    To be honest, I would suggest using Joda Time for any significant date/time work. Then you can create a LocalDate which is obviously meant to represent "just a date" - and the translation from a Date (or Instant) to a LocalDate will make it easy for you to specify whichever time zone you want to use.

提交回复
热议问题