'Converting date string to time in millis returns negative number

I'm trying to convert a string date with the following pattern 20221231 into a Long (to get the time in millis). I've tried many ways to do it but I always get a negative number.

My last solution is using DateUtils from apache as follow: return DateUtils.parseDate(dateString, DATE_FORMAT).getTime();

with DATE_FORMAT equals yyyyMMdd I've also tried format YYYYMMDD but for some reason I always get a negative number

EX: for a string date 19330601, I get -1154566800000

Any idea ? thanks



Solution 1:[1]

String dateString = "20200101";
DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
LocalDate timeA = LocalDate.parse(dateString, formatter);
long epoch = timeA.toEpochDay();
LocalDate timeB = LocalDate.ofEpochDay(epoch);

System.out.println("Original Time: " + timeA);
System.out.println("From epoch: " + timeB);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan