My julian date is in 7 digit format YYYYDDD. I want to make sure all the scenarios of leap years should be covered. Currently I have put a check on days. Is there any utility validating it ?
Advertisement
Answer
I want to make sure all the scenarios of leap years should be covered. Currently I have put a check on days. Is there any utility validating it ?
Yes, simply format the strings to LocalDate
using the pattern, uuuuDDD
and the leap years will be automatically be taken into consideration by the parsing API.
Demo:
JavaScript
x
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// The 100th day of the year, 2020
String strDate1 = "2020100";
// The 100th day of the year, 2019
String strDate2 = "2019100";
// Notice 'D' (day-of-year) instead of 'd' (day-of-month)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuDDD");
LocalDate date1 = LocalDate.parse(strDate1, dtf);
LocalDate date2 = LocalDate.parse(strDate2, dtf);
System.out.println(date1);
System.out.println(date2);
}
}
Output:
JavaScript
2020-04-09
2019-04-10