如何比较 ISO8601 格式的日期?

How to compare dates in ISO8601 format?

有没有办法将 ISO8601 日期与 Java 进行比较?比如,要知道字符串日期是否是好的格式,要知道月份是否不为负且不大于 12,要知道天数是否不超过 31,如果 2 月不超过 29 天,则为 %4...

我也在找怎么知道网上有没有class比较两个ISO8601日期的?就像,如果我有:2000-12-12 和 1999-05-06。我怎样才能比较这两个日期以获得年、月和日之间的确切差异?

如果您使用的是 Java 8 或更高版本,请使用 java.time 包中的工具:

LocalDate date1 = LocalDate.parse("1999-05-06"), 
    date2 = LocalDate.parse("2000-12-12");
Period period = Period.between(date1, date2);
// Prints "1 years, 7 months and 6 days"
System.out.printf("%d years, %d months and %d days\n", period.getYears(),
            period.getMonths(), period.getDays());

您需要的所有信息都可以从 LocalDate class 中的方法中提取。如果字符串的格式无效,LocalDate::parse 方法将抛出异常以指示错误。

关于如何在 Java (8+) 中解析和比较日期的一些示例。您可以为此使用 java.time.LocalDate class。这里的例子:

try {
    LocalDate.of(2000, 13, 12);
} catch (DateTimeException e) {
    System.out.println("It seems the date 12/13/2000 is invalid?");
}

// If you have only a string...

LocalDate.parse("2000-03-10");
System.out.println("I managed to parse the date from a string");

try {
    LocalDate.of(2000, 2, 30);
} catch (DateTimeException e) {
    System.out.println("It seems the date 30/02/2000 is invalid?");
}

var date1 = LocalDate.of(2000, 12, 12);
var date2 = LocalDate.of(1999, 5, 6);

if (date1.isBefore(date2)) {
    System.out.println("Date 1 is before date 2");
} else if (date1.isEqual(date2)) {
    System.out.println("Date 1 is equal to date 2");
} else {
    System.out.println("Date 1 is after date 2");
}

var period = Period.between(date2, date1);

System.out.println("Difference between date 1 and date 2 is: ");
System.out.print(period.getYears() + " years, ");
System.out.print(period.getMonths() + " months, ");
System.out.print(period.getDays() + " days");

这将打印:

It seems the date 12/13/2000 is invalid?
I managed to parse the date from a string
It seems the date 30/02/2000 is invalid?
Date 1 is after date 2
Difference between date 1 and date 2 is: 
1 years, 7 months, 6 days

如果你也需要处理时间,你可以使用LocalDateTime class。如果您尝试手动操作,这两个 class 都会为您节省很多时间!

您还可以使用 ChronoUnit class 计算天数距离,只需这样做:

ChronoUnit.DAYS.between(date1, date2);
ChronoUnit.MONTHS.between(date1, date2);
ChronoUnit.YEARS.between(date1, date2);

在这里查看 ChronoUnit 方法:https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html

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...

获取 LocalDate 实例,将年、月和月中的日期作为参数传递给 try-catch 块内的 LocalDate.of。如果其中任何一个无效,将抛出 DateTimeException 指示相同。

演示

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());
        }
    }
}

样本运行:

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

另一个样本运行:

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?

使用LocalDate#until得到java.time.Period,从中可以得到年、月、日。

演示

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);
    }
}

输出:

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