How to convert LocalDateTime to Date in Java 8

I use the default time zone of Brazil, but when I caught one LocalDateTime in New York and converted it to java.tim.Instant , it instantly fills correctly. The problem is that I am trying to create a Date with Date.from (instantValue) , instead of generating a New York date date , I end up getting the current date from Brazil.

ZoneId nyZone = ZoneId.of("America/New_York");
ZoneId brazilZone = ZoneId.of("America/Recife");

LocalDateTime ldtBrazil = LocalDateTime.now(brazilZone);
LocalDateTime ldtNY = LocalDateTime.now(nyZone);

Instant instantBrazil = ldtBrazil.toInstant(ZoneOffset.UTC);
Instant instantNY = ldtNY.toInstant(ZoneOffset.UTC);

System.out.println("-------LocalDateTime-------");
System.out.println("ldtBrazil    : "+ldtBrazil);
System.out.println("ldtNY        : "+ldtNY);

System.out.println("\n-------Instant-------");
System.out.println("instantBrazil: "+instantBrazil);
System.out.println("instantNY    : "+instantNY);

long milliBrazil = instantBrazil.toEpochMilli();
long milliNY = instantNY.toEpochMilli();

System.out.println("\n----------Milli----------");
System.out.println("miliBrazil : "+milliBrazil);
System.out.println("miliNY     : "+milliNY);

Date dateBrazil = Date.from(instantBrazil);
Date dateNY = Date.from(instantNY);

System.out.println("\n---------Date From Instant---------");
System.out.println("dateBrazil: "+dateBrazil);
System.out.println("dateNY    : "+dateNY);

System.out.println("\n---------Date From Milli---------");
System.out.println("dateBrazil: "+new Date(milliBrazil));
System.out.println("dateNY    : "+new Date(milliNY));

Result

-------LocalDateTime-------
ldtBrazil    : 2016-09-21T22:11:52.118
ldtNY        : 2016-09-21T21:11:52.118

-------Instant-------
instantBrazil: 2016-09-21T22:11:52.118Z
instantNY    : 2016-09-21T21:11:52.118Z

----------Milli----------
miliBrazil : 1474495912118
miliNY     : 1474492312118

---------Date From Instant---------
dateBrazil: Wed Sep 21 19:11:52 BRT 2016
dateNY    : Wed Sep 21 18:11:52 BRT 2016 //this data must be related to   NY LocalDateTime, but reiceved a same date of Brazil.

---------Date From Milli---------
dateBrazil: Wed Sep 21 19:11:52 BRT 2016
dateNY    : Wed Sep 21 18:11:52 BRT 2016
+4
source share
2 answers

LocalDateTime means no zone

It seems you misunderstood the goal LocalDateTime.

offset-from-UTC. . . "Local..." -, - , .

, - 25 2016 2016-12-25T00:00. , , , , , , .

LocalDateTime, , . , .

UTC

-, , UTC. UTC ; UTC.

java.time , Instant - go-to-, -. Instant UTC .

Instant now = Instant.now();

ZonedDateTime

, - . ZoneId, ZonedDateTime.

ZoneId zNewYork = ZoneId.of("America/New_York");
ZoneId zRecife = ZoneId.of("America/Recife");

ZonedDateTime zdtNewYork = now.atZone( zNewYork );
ZonedDateTime zdtRecife = now.atZone( zRecife );

, now, zdtNewYork zdtRecife, , . --. - , .

, Java. java.util.Date java.util.Calendar. . java.time.

, java.time, / java.time. , . java.util.Date.from Instant. Instant a ZoneDateTime ( OffsetDateTime).

java.util.Date utilDate = java.util.Date.from( zdtNewYork.toInstant() );

.

Instant instant = utilDate.toInstant();

. , java.util.Date "java.time" ?

count-from-epoch, - -1970- -UTC. , (, , , ..). , , , , 1970 . , .

, . getEpochSecond getNano Instant, toEpochMilli.

+14

, LocalDateTime Instant ( Date, , Instant). .

A LocalDateTime - . , .

Localdatetime

, , , , . . , .

An Instant - . , , , Instant. , JFK .

, . LocalDateTime. , Instant, , . , Date - , Instant.

, a LocalDateTime Instant . , , , , , , ​​; , . UTC, 2:56 21 1969 . , 18:56 20 1969 .

, . LocalDateTime.

  • ldtBrazil - - 22:11:52 21 .
  • ldtNY - - - 21:11:52 21 .

UTC, Instant.

  • instantBrazil - , 22:11:52 Timbuktu ( UTC ).
  • instantNY - , 21:11:52 ( , instantBrazil).

. , , . Instant UTC, . , Z.

Instant . . 1 1970 , UTC. mmiNY, , 3,6 , , Instant, .

Instant Date. , Date Instant , -.

Date. , . , dateNY dateBrazil; , UTC. , 19:11:52 18:11:52 .

, Date, . Date dateBrazil dateNY, , . .

+5

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


All Articles