如何使用Jodatime列出给定月份中的所有日期

时间:2013-04-20 12:25:01

标签: java jodatime

我正在寻找一种方法,使用Jodatime列出给定月份的所有日子。 拥有具体的代码示例会很棒。

1 个答案:

答案 0 :(得分:3)

由于我找不到这个问题的答案,我在这里发布代码,以便有人可以使用它:

// set your own print format here
private static final DateTimeFormatter DF=DateTimeFormat.forPattern("dd/MM/yyyy");

    private List<String> createMonthLabels(LocalDate month) {
    // add labels
    List<String> daysInMonthLabels = new ArrayList<String>();
    LocalDate firstDay = month.withDayOfMonth(1);
    LocalDate nextMonthFirstDay = firstDay.plusMonths(1);
    while (firstDay.isBefore(nextMonthFirstDay)) {
        daysInMonthLabels.add(DF.print(firstDay));
        firstDay = firstDay.plusDays(1);
    }

    return daysInMonthLabels;
}