如何列出 2009 年和 2010 年周一至周四的所有日历日期?

发布于 2024-09-28 10:07:14 字数 189 浏览 0 评论 0原文

我需要向后循环并列出 2009 年和 2010 年每周一至周四的所有日历日期,并将它们记录为映射到一周中某一天的日-月-年字符串映射

"19-10-2010", "Tuesday"
"4-10-2010", "Monday"

: Java 中的库可以帮助解决这个问题,还是可以仅使用标准库来完成?

I need to loop backwards and make a list of all calendar dates in 2009 and 2010 that fall on Monday - Thursday of each week and record them as a map of day-month-year strings mapped to a day of the week:

"19-10-2010", "Tuesday"
"4-10-2010", "Monday"

Is there a library in Java that would help with this or can it be done with just the standard library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

折戟 2024-10-05 10:07:14

使用日历

  • YEAR 设置为2009
  • DAY_OF_YEAR 设置为1
  • 迭代2009、2010 年的所有日期,检查周一至周四。

代码:

Calendar cal = Calendar.getInstance();

// Start in 1 Jan 2009
cal.set(YEAR, 2009);
cal.set(DAY_OF_YEAR, 1);

// Iterate while in 2009 or 2010
while (cal.get(YEAR) <= 2010)
{
    int dow = cal.get(DAY_OF_WEEK);
    if (dow >= Calendar.MONDAY && dow <= Calendar.THURSDAY))
    {
        // add to your map
    }
    cal.add(Calendar.DATE, 1);
}

更新:

对此进行优化很简单,这样您就不需要迭代周五、周六、周日:只要看到周四就添加 4 天,否则添加 1 天:

while (cal.get(YEAR) <= 2010)
{
    int dow = cal.get(DAY_OF_WEEK);
    if (dow >= Calendar.MONDAY && dow <= Calendar.THURSDAY))
    {
        // add to your map
    }
    cal.add(Calendar.DATE, (dow == Calendar.THURSDAY)? 4 : 1);
}

Use a Calendar:

  • Set YEAR to 2009
  • Set DAY_OF_YEAR to 1
  • Iterate over all days in year 2009, 2010 checking for Mon-Thu.

Code:

Calendar cal = Calendar.getInstance();

// Start in 1 Jan 2009
cal.set(YEAR, 2009);
cal.set(DAY_OF_YEAR, 1);

// Iterate while in 2009 or 2010
while (cal.get(YEAR) <= 2010)
{
    int dow = cal.get(DAY_OF_WEEK);
    if (dow >= Calendar.MONDAY && dow <= Calendar.THURSDAY))
    {
        // add to your map
    }
    cal.add(Calendar.DATE, 1);
}

Update:

It is trivial to optimize this so that you don't need to iterate over Fri, Sat, Sun: Just add 4 days whenever you see a Thursday, 1 otherwise:

while (cal.get(YEAR) <= 2010)
{
    int dow = cal.get(DAY_OF_WEEK);
    if (dow >= Calendar.MONDAY && dow <= Calendar.THURSDAY))
    {
        // add to your map
    }
    cal.add(Calendar.DATE, (dow == Calendar.THURSDAY)? 4 : 1);
}
阿楠 2024-10-05 10:07:14

您可以利用 guava 的计算图。通过这样做,您不需要生成所有日期及其星期几并保存在内存中。

    ConcurrentMap<String, String> m = new MapMaker()
            .makeComputingMap(new Function<String, String>() {
                Calendar cal = Calendar.getInstance();
                SimpleDateFormat formatDay = new SimpleDateFormat("EEEE");
                SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
                final String notApplicable = "NA";

                @Override
                public String apply(String arg0) {

                    try {
                        cal.setTime(format.parse(arg0));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    int dow = cal.get(DAY_OF_WEEK);
                    int year=cal.get(YEAR);
                    if(year!=2009&&year!=2010)
                        return notApplicable;
                    if (dow >= Calendar.MONDAY && dow <= Calendar.THURSDAY) {
                        return formatDay.format(cal.getTime());
                    }
                    return notApplicable;
                }
            });

要获取星期几,您可以执行以下操作:

 m.get("01-01-2009");

You can make use of guava's computing map.By doing so you need not generate all the date's and their day of week and keep in memory.

    ConcurrentMap<String, String> m = new MapMaker()
            .makeComputingMap(new Function<String, String>() {
                Calendar cal = Calendar.getInstance();
                SimpleDateFormat formatDay = new SimpleDateFormat("EEEE");
                SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
                final String notApplicable = "NA";

                @Override
                public String apply(String arg0) {

                    try {
                        cal.setTime(format.parse(arg0));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    int dow = cal.get(DAY_OF_WEEK);
                    int year=cal.get(YEAR);
                    if(year!=2009&&year!=2010)
                        return notApplicable;
                    if (dow >= Calendar.MONDAY && dow <= Calendar.THURSDAY) {
                        return formatDay.format(cal.getTime());
                    }
                    return notApplicable;
                }
            });

To get the day of week you can do:

 m.get("01-01-2009");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文