The era is not an era if a new date (0L). What for?

My problem is explained in some detail:

if i do this:

public class Main {

public static void main(String[] args) throws Exception {
        Date d = new Date(0L );
        System.out.println(d);
}

}

I get the following result: Thu Jan 01 01:00:00 CET 1970

According to the document, I expected: Thu Jan 01 00:00:00 CET 1970

I wish everything was wrong ...

EDIT: Indeed, I read the document too quickly. I should have been Thu Jan 01 00:00:00 GMT 1970

So, how can I force to use GMT and ignore all local time?

Change, Solution:

public static void main(String[] args) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("H:m:s:S");
SimpleTimeZone tz = new SimpleTimeZone(0,"ID");
sdf.setTimeZone(tz) ;
Date d = new Date(0L );
System.out.println( sdf.format(d));
}
+3
source share
4 answers

Epoch is defined as 00:00:00 at 1970-1-1 UTC. Since CET is UTC + 1, it is 1AM of your time.

Date(long), , , , epa, UTC:

Date , "", 1 1970 , 00:00:00 GMT.

GMT : , Date GMT. , GMT, DateFormat , , setTimeZone().

+5

. , , -, , , Date , .

, .

: : CET!= UTC:/

, Locale.

Reedit: .

: 01 01:00:00 CET 1970

: 01 00:00:00 CET 1970

: 01 00:00:00 GMT 1970 (โ‰ก 01 01:00:00 CET 1970)

+3

CET - GMT, , .

+1

:

Instant.EPOCH
       .toString()

1970-01-01T00: 00: 00Z

Date::toString

java.util.Date/Calendar: Date , toString . , , , .

Date/Calendar

/ Joda-Time Java 8, java.time. * JSR 310.

java.time

Instant Date, UTC.

Instant.now()

.

Instant.EPOCH.toString()

1970-01-01T00: 00: 00Z

" " , , LocalDate.

LocalDate.ofEpochDay( 0L )

1970-01-01

Joda

. Joda-Time , java.time.

Joda-Time DateTime . , .

, Unix time Epoch, Joda-Time 2.3.

// ยฉ 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

DateTimeZone timeZone_Paris = DateTimeZone.forID( "Europe/Paris" );

DateTime epochParis = new DateTime( 0L, timeZone_Paris );
DateTime epochUtc = new DateTime( 0L, DateTimeZone.UTC );

...

System.out.println( "epochParis: " + epochParis );
System.out.println( "epochUtc: " + epochUtc );

...

epochParis: 1970-01-01T01:00:00.000+01:00
epochUtc: 1970-01-01T00:00:00.000Z

UTC/GMT

, GMT ?

UTC/GMT ( ):

  • Convert DateTime to another instance with a different time zone
    (Joda-Time makes things unchanged for thread safety, so we donโ€™t actually convert, we create new instances based on the old ones.)
  • Use the formatter to create lines displayed for a specific time zone.
// To use UTC/GMT instead of local time zone, create new instance of DateTime.
DateTime nowInParis = new DateTime( timeZone_Paris );
DateTime nowInUtcGmt = nowInParis.toDateTime( DateTimeZone.UTC );

Dump for console ...

System.out.println( "nowInParis: " + nowInParis );
System.out.println( "nowInUtcGmt: " + nowInUtcGmt );

At startup ...

nowInParis: 2013-12-22T08:40:01.443+01:00
nowInUtcGmt: 2013-12-22T07:40:01.443Z
+1
source

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


All Articles