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
Date.toString()produces the wrong output for really old dates.Dateis correct. Just give it to whatever API you are using. If you want it to display the correct date as aString, convert it back to aLocalDate. You can see that it is the sameLocalDateyou had.LocalDateuses. 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,LocalDateis not the right class to use, sorry. Other than that you are doing the conversion in the correct way.