开发者

How make a list of all calendar dates in 2009 and 2010 that fall on Monday to Thursday?

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 h开发者_StackOverflow中文版elp with this or can it be done with just the standard library?


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


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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜