0

I converted an instant to LocalDate and here is my implementation:

theInstant.atZone(ZoneId.systemDefault()).toLocalDate();

However, I got the exception in Junit test: java.time.DateTimeException: Invalid value for Year (valid values -999999999 - 999999999): -1000000000

Can anyone help?

Thanks!

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    How did you create the `Instant`? Would be good to see the JUnit test here, too... And in addition, you have converted the `Instant` to a `ZonedDateTime` by `.atZone(...)` and then extracted the date part by `.toLocalDate()`. – deHaar Mar 05 '21 at 15:14
  • 1
    This kind of reproduces the error `Instant.MIN.atZone(ZoneId.systemDefault());` – the Hutt Mar 05 '21 at 15:21
  • 1
    atZone method description says `An exception will be thrown if the instant is toolarge to fit into a zoned date-time.` – the Hutt Mar 05 '21 at 15:28
  • 2
    Help us to help you. What does `theInstant` contain? – Arvind Kumar Avinash Mar 05 '21 at 16:01
  • Also what sense does it make to have an instant that is 1000 million years ago? – Ole V.V. Mar 07 '21 at 10:53

2 Answers2

1

The problem is that for whatever instant you provided it must be in the range of -999999999 to 999999999 where 0 is the epoch. You can create an instant several ways. One is to specify the current date.

LocalDate ld = Instant.now().atZone(ZoneId.systemDefault()).toLocalDate();      
System.out.println(ld);

prints

2021-03-05

Another is to give it a value in some unit of time. Here is one for seconds.

    LocalDate ld = Instant.ofEpochSecond(1229998889L)
           .atZone(ZoneId.systemDefault()).toLocalDate();

Prints

2008-12-22
            
WJS
  • 36,363
  • 4
  • 24
  • 39
0

Once you have the LocatDate

    LocalDate localDate = LocalDate.now();  //local date

    LocalDateTime localDateTime = localDate.atTime(10, 45, 56);  //Add time information

    ZoneId zoneId = ZoneId.of("Asia/Kolkata"); // Zone information you can present ZoneId.systemDefault() here

    ZonedDateTime zdtAtAsia = localDateTime.atZone(zoneId); // add zone information

    ZonedDateTime zdtAtET = zdtAtAsia
            .withZoneSameInstant(ZoneId.of("America/New_York")); // Same time in ET timezone
     

thats all!

Raghuveer
  • 2,859
  • 7
  • 34
  • 66