0

I'm trying to convert a java.time.LocalDate to java.util.Date, but I lose some days in the process.

Here the code that shows the issue:

public static void main(String[] args) {
    var localDate = LocalDate.of(775, 7, 20);
    var date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    System.out.println("date = " + date);
}

As you can see, I have a LocalDate that's in year 775, month 7 and day 20.

But when I convert it to Date it becomes in year 775, month 7 and day 16.

So 4 days are missing.

Is there a way of converting without a loss in days?

PS: I know that I should not mix legacy Date API with the new Date API but I'm limited by a closed source ERP that uses java.util.Date

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    The result is not wrong. It's just that `Date.toString()` produces the wrong output for really old dates. – Sweeper Apr 27 '23 at 04:37
  • it's not the same, because at my case i'm loosing some days – Abadellatif Abazine Apr 27 '23 at 04:38
  • @Sweeper i need it to have to correct output otherwise it's considered wrong – Abadellatif Abazine Apr 27 '23 at 04:39
  • There is nothing to solve. The value in the `Date` is correct. Just give it to whatever API you are using. If you want it to display the correct date as a `String`, convert it back to a `LocalDate`. You can see that it is the same `LocalDate` you had. – Sweeper Apr 27 '23 at 04:41
  • it has to be persisted with correct value, currently the value in the ERP points to a date that misses 4 days. – Abadellatif Abazine Apr 27 '23 at 04:43
  • I suspect this might be an [XY Problem](http://xyproblem.info/). Is there some bigger problem that you are trying to solve, and you thought making `Date.toString` return the same date as `LocalDate.toString` does would solve the problem? i.e. **Why** does the correct year, month, day has to be persisted? `Date` represents an instant, not a year, month and day. – Sweeper Apr 27 '23 at 04:44
  • Yes it would solve my problem, It has to be persisted with correct value because i consume it from another system that tokenizes this value with random dates. – Abadellatif Abazine Apr 27 '23 at 04:50
  • @Sweeper The ERP uses java.util.Date to persist dates, so when it comes to show the date in the UI, it uses toString of java.util.Date to show it. – Abadellatif Abazine Apr 27 '23 at 05:15
  • 2
    What do you need to handle a 1250 years old date for? You need to beware of the difference between the *Julian* calendar in effect back then and the *proleptic Gregorian calendar* that `LocalDate` uses. So both days, 16 July and 20 July, are correct, they are just given in different calendar systems. If you read the date July 20 in a source from back then, then it’s the Julian calendar that you need, that is, `LocalDate` is not the right class to use, sorry. Other than that you are doing the conversion in the correct way. – Ole V.V. Apr 27 '23 at 08:39

1 Answers1

0

Not sure of the root cause for such difference in the days, but below work around might be helpful in your context:

    var localDate = LocalDate.of(775, 7, 20);
    var instant = Calendar.getInstance();
    instant.set(localDate.getYear(), localDate.getMonthValue() - 1 , localDate.getDayOfMonth());
    var date = instant.getTime();
    System.out.println("date = " + date);