Search code examples
javadateiso8601

How to compare dates in ISO8601 format?


Is there a way to compare ISO8601 dates with Java? Like, to know if the String date is the good format, to know if months are not negative and not >12, to know if days are not over 31, when %4 if february doesn't have over 29 days...

I'm also searching how to know if there is a class on the Internet to compare two ISO8601 dates? Like, if I have: 2000-12-12 and 1999-05-06. How can I compare those two dates to have the exact difference between years, months and days?


Solution

  • Is there a way to compare ISO8601 dates with Java? Like, to know if the String date is the good format, to know if months are not negative and not >12, to know if days are not over 31, when %4 if february doesn't have over 29 days...

    Get a LocalDate instance on passing the year, the month and the day of the month as parameters to LocalDate.of inside a try-catch block. If any of these will be invalid, DateTimeException will be thrown indicating the same.

    Demo

    import java.time.DateTimeException;
    import java.time.LocalDate;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int year, month, dayOfMonth;
            LocalDate date;
            System.out.print("Enter the year, month and day separated by space: ");
            year = scanner.nextInt();
            month = scanner.nextInt();
            dayOfMonth = scanner.nextInt();
    
            try {
                date = LocalDate.of(year, month, dayOfMonth);
                System.out.println("The date in ISO 8601 format is " + date);
            } catch (DateTimeException e) {
                System.out.println(e.getMessage());
            }
        }
    }
    

    A sample run:

    Enter the year, month and day separated by space: 2020 2 30
    Invalid date 'FEBRUARY 30'
    

    Another sample run:

    Enter the year, month and day separated by space: 2020 2 29
    The date in ISO 8601 format is 2020-02-29
    

    I'm also searching how to know if there is a class on the Internet to compare two ISO8601 dates? Like, if I have: 2000-12-12 and 1999-05-06. How can I compare those two dates to have the exact difference between years, months and days?

    Use LocalDate#until to get java.time.Period from which you can get years, months and days.

    Demo

    import java.time.LocalDate;
    import java.time.Period;
    
    public class Main {
        public static void main(String[] args) {
            String dateStr1 = "1999-05-06";
            String dateStr2 = "2000-12-12";
    
            LocalDate date1 = LocalDate.parse(dateStr1);
            LocalDate date2 = LocalDate.parse(dateStr2);
    
            Period period = date1.until(date2);
            System.out.printf("There are %d years, %d months and %d days between %s and %s", period.getYears(),
                    period.getMonths(), period.getDays(), dateStr1, dateStr2);
        }
    }
    

    Output:

    There are 1 years, 7 months and 6 days between 1999-05-06 and 2000-12-12