java - 不同日期加天数

标签 java calendar simpledateformat localdate

我正在将 Java 中的日期数据转换为 Adabas(天)。但是当我使用 LocalDate 和 Calendar 时,日期有差异。我正在使用 Java 8

public static void printNewLocalDateWithPlusDays(Long daysToAdd) {
        LocalDate initDateToCalculate = LocalDate.of(0, 1, 4);
        System.out.println("Initial Date To Calculate : " + initDateToCalculate);
        LocalDate dateCalculated = initDateToCalculate.plusDays(daysToAdd);
        System.out.println("Date calculated : " + dateCalculated);
}

public static void printSimpleDateFormatWithPlusDaysInCalendar(Long daysToAdd) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse("0000-01-04"));
            System.out.println("Initial Date To Calculate : " + sdf.format(calendar.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        calendar.add(Calendar.DAY_OF_MONTH, daysToAdd.intValue());
        Date time = calendar.getTime();
        String dateFormatted = sdf.format(time);
        System.out.println("Date calculated : " + dateFormatted);
}

public static void main(String[] args) {
        Long daysToAdd = 36580L;
        System.out.println("Print dates is less than or equal to " + daysToAdd + " days");
        System.out.println("Print Local Date");
        printNewLocalDateWithPlusDays(daysToAdd);
        System.out.println("----------");
        System.out.println("Print Calendar with SimpleDateFormat");
        printSimpleDateFormatWithPlusDaysInCalendar(daysToAdd);

        System.out.println("----------");
        System.out.println("Difference init here");
        System.out.println("----------");

        daysToAdd = 36581L;
        System.out.println("Print dates is greater than or equal to " + daysToAdd + " days");
        System.out.println("Print Local Date");
        printNewLocalDateWithPlusDays(daysToAdd);
        System.out.println("----------");
        System.out.println("Print Calendar with SimpleDateFormat");
        printSimpleDateFormatWithPlusDaysInCalendar(daysToAdd);
}

我期望输出相同,但实际输出不同。为什么?

最佳答案

因为(至少当我运行你的代码时),Calendar.getInstance() 返回 GregorianCalendar 的实例,并根据其 javadoc (强调我的):

Historically, in those countries which adopted the Gregorian calendar first, October 4, 1582 (Julian) was thus followed by October 15, 1582 (Gregorian). This calendar models this correctly. Before the Gregorian cutover, GregorianCalendar implements the Julian calendar. The only difference between the Gregorian and the Julian calendar is the leap year rule. The Julian calendar specifies leap years every four years, whereas the Gregorian calendar omits century years which are not divisible by 400.

GregorianCalendar implements proleptic Gregorian and Julian calendars. That is, dates are computed by extrapolating the current rules indefinitely far backward and forward in time. As a result, GregorianCalendar may be used for all years to generate meaningful and consistent results.

由于您要构造 100 年的日期,因此它使用儒略历规则,该规则表明这是闰年,因此会打印 2 月 29 日。

但是,LocalDate 使用 ISO-8601 日历系统,其 javadoc说(强调我的):

The ISO-8601 calendar system is the modern civil calendar system used today in most of the world. It is equivalent to the proleptic Gregorian calendar system, in which today's rules for leap years are applied for all time. For most applications written today, the ISO-8601 rules are entirely suitable. However, any application that makes use of historical dates, and requires them to be accurate will find the ISO-8601 approach unsuitable.

因此它错误地计算了闰年,并打印了 3 月 1 日。

关于java - 不同日期加天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58087907/

相关文章:

php - 如何存储和解析 RRULE 数据

java - UTC 中的 SimpleDateFormat 到给定格式的字符串

java - 使 SimpleDateFormat 线程安全

java - 一种存储用户访问权限的方法

java - 使用 SQLite 数据库保存 CheckBox 状态

java - 在具有重定向输出的脚本中运行 jar 文件(在循环内)

android - 获取特定月份的第一天

JAVA Stack Pop 错误,尝试创建撤消

c# - 从 ASP.NET 日历控件的单元格触发事件

java - 将 java.time.LocalDate 转换为 java.util.Date