How convert LocalDateTime to Date in Java 8

后端 未结 2 1650
我在风中等你
我在风中等你 2021-01-02 03:05

I\'m using timezone Brazil by default, but when caught one LocalDateTime of New York and convert to java.tim.Instant the instant is filled

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 03:43

    It seems to me that you're confused about the difference between a LocalDateTime and an Instant (or a Date, which is essentially the same thing as an Instant). These are completely different objects.

    A LocalDateTime is a particular calendar date and a particular clock time. You can think of it like this picture.

    Or you can think of it as a year, month, day, hour, minute and second. But it has no time zone. It's just what the calendar says and what the clock says.

    An Instant is a moment in time. For example, the moment when Neil Armstrong first stepped on the moon could be represented as an Instant. So could the moment JFK was shot.

    Again, there's no time zone. But it's something different from a LocalDateTime. You can't write down what time an Instant is unless you know which time zone to write it down for. Oh, and a Date is the same thing as an Instant.

    Therefore, to convert between a LocalDateTime and an Instant, you need to reference a particular time zone. So to express the moment Neil Armstrong stepped on the moon as a year, month, date, hour, minute and second; you need to know what time zone to use. If you use UTC, it was 2:56am on 21 July 1969. If you use Pacific Standard Time, it was 6:56pm on 20 July 1969.

    Armed with that knowledge, let's analyse your code. You started with a couple of LocalDateTime objects.

    • ldtBrazil is the current time in Brazil - 22:11:52 on 21 September.
    • ldtNY is the current time in New York - 21:11:52 on 21 September.

    Now, you use UTC to convert these to Instant objects.

    • instantBrazil is the moment at which it was 22:11:52 in Timbuktu (which uses UTC all year round).
    • instantNY is the moment at which it was 21:11:52 in Timbuktu (an hour earlier than instantBrazil).

    Then you print these out. We need to know a timezone to be able to do this, but that's OK. An Instant gets printed in UTC, no matter what. That's what the Z means.

    Now, you convert the Instant objects to a number of milliseconds. Fine. This is the number of milliseconds since midnight on 1 January 1970, UTC. milliNY is obviously 3.6 million less than milliBrazil, because it corresponds to an Instant that's an hour earlier.

    Then you convert the Instant objects to Date objects. This doesn't really change anything, since a Date and an Instant represent the same thing, even though they get printed out differently.

    You print out those converted Date objects. They get printed in Brazilian time, because that's your locale. And it just so happens that dateNY is an hour earlier than dateBrazil; but they still both get printed in Brazilian time, which is three hours behind UTC. So you get 19:11:52 and 18:11:52 respectively.

    Lastly, you make a couple of more Date objects, from the numbers of milliseconds. But these new Date objects are exactly equal to the dateBrazil and dateNY that you already have, since you made them from the same number of milliseconds. And again, they get printed in Brazilian time.

提交回复
热议问题