Does calling "new date (long)" result in "January 01, 01:00:00 CET 1970"?

A Java document describes how a constructor Date(long date)creates a Date object using a given time value in milliseconds from January 1, 1970, 00:00:00 GMT

When I did new Date(0), the date is January 01, 01:00:00 CET 1970

I don’t know why it starts from 01h

0
source share
3 answers

Shows 1AM, because you are an hour ahead of GMT. A date instance is simply a counter of the number of milliseconds from 00:00:00 to 1970. Since your hour in advance when an era occurred, it was actually 1AM your time.

Date toString() . , DateFormat.

+4

, (CET) unix (, Date), GMT.

+3

TL;DR

Instant.now()  // Current moment in UTC.

, .

  • , UTC , 12:00 1 .
  • java.time, Instant Date.

, java.time.

, toString JVMs, , . A Date UTC. . , .

Instant UTC

Instant UTC ( (9) ).

Instant instant = Instant.now() ;  // Current moment in UTC.

instant.toString(): 2018-02-11T21: 07: 02.315283Z

, java.time, 1970 UTC, : Instant.EPOCH.

Instant.EPOCH.toString(): 1970-01-01T00: 00: 00Z

OffsetDateTime

, , , Instant OffsetDateTime ZoneOffset.UTC.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;

ISO 8601

continent/region, America/Montreal, Africa/Casablanca Pacific/Auckland. 3-4 , EST IST CET, , (!).

ZoneId z = ZoneId.of( "Europe/Paris" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

zdt.toString(): 2018-02-11T22: 07: 02.315283 + 01: 00 [/]

java.time .

ZonedDateTime zdtEpochParis = Instant.EPOCH.atZone( z ) ;

zdtEpochParis.toString(): 1970-01-01T01: 00 + 01: 00 [/]

, .

ZonedDateTime zdtEpochMontreal = Instant.EPOCH.atZone( ZoneId.of( "America/Montreal" ) ) ;

zdtEpochMontreal.toString(): 1969-12-31T19: 00-05: 00 [/]


java.time

java.time Java 8 . legacy -, java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

JDBC-, JDBC 4.2 java.time- . java.sql. * Classes.

java.time?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes, such as Interval, YearWeek, YearQuarterand longer .

0
source

Source: https://habr.com/ru/post/1736699/


All Articles