AccountingDate (ChronoLocalDate) to Instant in java

325 Views Asked by At

I'm using the AccountingDate implemented into this project.

public final class AccountingDate extends AbstractDate implements ChronoLocalDate, Serializable {}

Do you know a way to convert an AccountingDate to Instant or LocalDate?

2

There are 2 best solutions below

0
Sweeper On BEST ANSWER

AccountingDate implements ChronoLocalDate, which supports all date based ChronoFields, so it supports ChronoFields.EPOCH_DAY, so LocalDate.from works:

LocalDate.from(accountingDate)

To convert a date to an Instant, you need two more pieces of information:

  • time
  • zone offset

If we assume the time is midnight, and the zone offset being UTC, we can do:

accountingDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneOffset.UTC).toInstant()
0
Indivon On

Since it's a chrono date, you need to go through the epoch days

This would be e.g. like:

AccountingDate accountingDate = ...
LocalDate date = LocalDate.ofEpochDay(accountingDate.toEpochDay())