java - 如何计算Java中特定两个日期之间的星期日

标签 java date date-format java-time

我正在努力计算每个月的第一天有多少个星期日。 如输入 1(测试用例)1900 1 1 1902 1 1(年、月、日),输出应为 4。包括开始日期。

解释:

1 April 1900
1 July 1900
1 September 1901
1 December 1901

但是,当我尝试这个时:

6 4699 12 12 4710 1 1 1988 3 25 1989 7 13 1924 6 6 1925 6 16 1000000000000 2 2 1000000001000 3 2 1925 6 16 1924 6 6 1905 1 1 1905 1 1

输出应该是:18 2 2 1720 0 1

我的代码输出是:18 2 2 **1714** 0 1

在测试中输入6表示6个测试用例。 4699 年 12 月 12 日,即 4699 年 12 月 12 日; 4710 结束日期等。

你能帮我解决这个问题吗?

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Calendar;

public class nineteen {
    
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        Scanner sc = new Scanner(System.in);
        int loop = sc.nextInt();
        for (int i = 0; i < loop; i++) {
            long year = sc.nextLong(), month = sc.nextLong(), day = sc.nextLong(), yearto = sc.nextLong(),
                    monthto = sc.nextLong(), dayto = sc.nextLong();
            String day1 = String.valueOf(day);
            String month1 = String.valueOf(month);
            String year1 = String.valueOf(year);
            String dayt = String.valueOf(dayto);
            String montht = String.valueOf(monthto);
            String yeart = String.valueOf(yearto);
            String input_date = month1 + "/" + day1 + "/" + year1; // month day year
            String out_date = montht + "/" + dayt + "/" + yeart; // month day year
            long count = 0;

            Date d1 = formatter.parse(input_date);
            Date d2 = formatter.parse(out_date);
            count = saturdayscount(d1, d2);

            // TODO Auto-generated method stub

        }
        sc.close();
    }

    public static long saturdayscount(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(d1);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(d2);

        long sundays = 0;

        while (!c1.after(c2)) {

            if (c1.get(Calendar.DAY_OF_MONTH) != 1) {
                c1.add(Calendar.MONTH, 1);
                c1.set(Calendar.DAY_OF_MONTH, 1);

            }

            if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {

                sundays++;
            }

            c1.add(Calendar.MONTH, 1);
            c1.set(Calendar.DAY_OF_MONTH, 1);
        }

        System.out.println(sundays);
        return sundays;
    }

}

最佳答案

解决方案:如何处理超出范围的日期

正如我在评论中所说,您的问题是 1000000000000(1 000 000 000 000 年,1 万亿年或经典英语中的 10 亿年)超出了 Java 日期库的范围。这与设计不佳且过时已久的 DateSimpleDateFormat 类相结合,不会给您任何错误通知,只是默认给您不正确的数据。 Arvind Kumar Avinash 的回答表明,现代 Java 日期和时间 API java.time 确实通知您错误。确切的范围在 Lino 的回答中。如果您需要应对这样的岁月,请按以下方式处理。

由于标准库不能为我们完成这项工作,我们至少需要自己完成一些工作,可以说是“手工”完成。您当然可以从头开始编写自己的日期代码,但没有人愿意这样做。而是观察:

  • 闰年以 400 年为周期。
  • 1900 年 1 月 1 日是星期一。 400 年后的 2300 年 1 月 1 日也将是星期一。这意味着星期几遵循相同的 400 年循环。

因此要处理 2300 年之后的年份,首先要计算整个 400 年周期中每个月的第一天的星期日数。 java.time,现代 Java 日期和时间 API,可以做到这一点,如其他答案所示。现在从开始年份减去 400 的整数倍,得到 1900 到 2300 之间的年份。计算从这个修改后的开始日期到 2300 年 1 月 1 日的星期日。类似地从结束年份减去 400 的倍数。然后从 1900 年开始计算星期日。将这两个数字相加并调整您从开始和结束年份中减去的 400 年的周期数。

请记住多年来一直使用 longBigInteger,因为 int 也不能容纳这么大的数字。

我假设年份总是 1900 年或更晚,我相信这也是最初的欧拉计划挑战(链接如下)的前提。当回到过去时,日历系统往往不一致,因此某个给定的星期日是否在一个月的 1 号不再明确定义。我不考虑这一点。

链接: Project Euler.net Counting Sundays Problem 19

关于java - 如何计算Java中特定两个日期之间的星期日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66583492/

相关文章:

java - Java FileChannel.tryLock 可以在 Mac OS X 上运行吗?

java.sql.SQLException : The Network Adapter could not establish the connection

ios - DateFormatter 不返回 "HH:mm:ss"的日期

java - 日期解析不会因格式无效而失败

datetime - 需要帮助将 Sybase 中的字符串转换为日期格式

java - 当需要格式宽度时,避免 MissingFormatWidthException

java - dialogflow权限错误: "Not allowed to ask for PII in apps designed for family."

java - 日期格式问题

javascript - 如何有效地计算给定原始日期的连续日期

javascript - 转换一年中的某一天至今