Both System.currentTimeMillis()
and Instant.toEpochMilli()
return the number of milliseconds since the Unix epoch. That isn't "in" any particular time zone, although the Unix epoch is normally expressed as "midnight on January 1st 1970, UTC". But an instant is just an instant in time, and is the same whichever time zone you're in - but it will reflect a different local time.
The output of LocalDateTime.atZone(UTC)
differs because you're saying "Take the local date and time, and convert it to an instant as if it were in the UTC time zone" - even though when you created that LocalDateTime
you did so implicitly in the UTC+3 time zone... that's why it's "wrong".
LocalDateTime.now()
takes the local date and time in the system default time zone. So if your time zone is UTC+3, the current instant in time is 2015-10-06T16:57:00Z, then LocalDateTime.now()
will return .2015-10-06T19:57:00
. Let's call that localNow
...
So localNow.atZone(ZoneOffset.of("+3"))
will return a ZonedDateTime
representing 2015-10-06T19:57:00+03 - in other words, the same local date/time, but "knowing" that it's 3 hours ahead of UTC... so toInstant()
will return an Instant
representing 2015-10-06T16:57:00Z. Great - we still have the current date/time.
But localNow.atZone(ZoneOffset.UTC)
will return a ZonedDateTime
representing 2015-10-06T19:57:00Z - in other words, the same local date/time, but "thinking" that it's already in UTC... so toInstant()
will return an Instant
representing 2015-10-06T19:57:00Z.. which isn't the current time at all (it's in three hours).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…