IK.AM

@making's tech note


Joda-Timeで今月のカレンダーの内容を出力するSnippet

🗃 {Programming/Java/org/joda/time}
🗓 Updated at 2012-08-05T10:16:00Z  🗓 Created at 2012-08-05T10:16:00Z   🌎 English Page

カレンダーを実装するときに良く使うコード。週の始まりが月曜日であることに注意。

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;

public class Main {

    public static void main(String[] args) {
        DateTime today = new DateTime();
        DateTime firstDayOfMonth = today.dayOfMonth().withMinimumValue();
        DateTime lastDayOfMonth = firstDayOfMonth.dayOfMonth().withMaximumValue();

        String[] weeks = new String[8];
        weeks[DateTimeConstants.MONDAY] = "月";
        weeks[DateTimeConstants.TUESDAY] = "火";
        weeks[DateTimeConstants.WEDNESDAY] = "水";
        weeks[DateTimeConstants.THURSDAY] = "木";
        weeks[DateTimeConstants.FRIDAY] = "金";
        weeks[DateTimeConstants.SATURDAY] = "土";
        weeks[DateTimeConstants.SUNDAY] = "日";

        DateTime firstDayOfCalender = firstDayOfMonth.dayOfWeek()
                .withMinimumValue();
        DateTime lastDayOfCalender = lastDayOfMonth.dayOfWeek()
                .withMaximumValue();
        for (int i = 0; i < 100; i++) {
            DateTime d = firstDayOfCalender.plusDays(i);
            if (d.isAfter(lastDayOfCalender)) {
                break;
            }
            int week = d.getDayOfWeek();
            System.out.println(d.toString("yyyy年MM月dd日") + " (" + weeks[week]
                    + ")");
            if (week == DateTimeConstants.SUNDAY) {
                System.out.println("------------------------------");
            }
        }
    }
}

出力結果

2012年07月30日 (月)
2012年07月31日 (火)
2012年08月01日 (水)
2012年08月02日 (木)
2012年08月03日 (金)
2012年08月04日 (土)
2012年08月05日 (日)
------------------------------
2012年08月06日 (月)
2012年08月07日 (火)
2012年08月08日 (水)
2012年08月09日 (木)
2012年08月10日 (金)
2012年08月11日 (土)
2012年08月12日 (日)
------------------------------
2012年08月13日 (月)
2012年08月14日 (火)
2012年08月15日 (水)
2012年08月16日 (木)
2012年08月17日 (金)
2012年08月18日 (土)
2012年08月19日 (日)
------------------------------
2012年08月20日 (月)
2012年08月21日 (火)
2012年08月22日 (水)
2012年08月23日 (木)
2012年08月24日 (金)
2012年08月25日 (土)
2012年08月26日 (日)
------------------------------
2012年08月27日 (月)
2012年08月28日 (火)
2012年08月29日 (水)
2012年08月30日 (木)
2012年08月31日 (金)
2012年09月01日 (土)
2012年09月02日 (日)
------------------------------

ちょっとアレンジ

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;

public class Main2 {

    public static void main(String[] args) {
        DateTime today = new DateTime();
        DateTime firstDayOfMonth = today.dayOfMonth().withMinimumValue();
        DateTime lastDayOfMonth = firstDayOfMonth.dayOfMonth()
                .withMaximumValue();

        DateTime firstDayOfCalender = firstDayOfMonth.dayOfWeek()
                .withMinimumValue();
        DateTime lastDayOfCalender = lastDayOfMonth.dayOfWeek()
                .withMaximumValue();
        System.out.println(firstDayOfMonth.toString("yyyy-MM"));
        System.out.println("----------------------");
        for (int i = 0; i < 100; i++) {
            DateTime d = firstDayOfCalender.plusDays(i);
            if (d.isAfter(lastDayOfCalender)) {
                break;
            }
            int week = d.getDayOfWeek();
            if (d.isBefore(firstDayOfMonth) || d.isAfter(lastDayOfMonth)) {
                System.out.print("|  ");
            } else {
                System.out.printf("|%2d", d.getDayOfMonth());
            }
            if (week == DateTimeConstants.SUNDAY) {
                System.out.print("|");
                System.out.println();
                System.out.println("----------------------");
            }
        }
    }
}

出力結果

2012-08
----------------------
|  |  | 1| 2| 3| 4| 5|
----------------------
| 6| 7| 8| 9|10|11|12|
----------------------
|13|14|15|16|17|18|19|
----------------------
|20|21|22|23|24|25|26|
----------------------
|27|28|29|30|31|  |  |
----------------------

✒️️ Edit  ⏰ History  🗑 Delete