获取java中的年,月和周列表

时间:2017-05-03 10:19:54

标签: java calendar java-8

我试图获得自1970年,1971年,1972年,1973年以来到2017年的年份列表,以及1月份的月份列表 - decemeber 在此之后根据月份和年份产生周数(基于星期一至星期五的工作日)

所以,如果一个月开始于星期四,即2-2-2016

然后应该返回2-2-2016,3-2-2016

如果月末是星期二,其日期是30-4-2016,则假设用例

然后我应该得到29-4-2016,30-4-2016

到目前为止,我只能从输入日期到同一周的最后一个工作日,代码如下:

String Date = "06-04-2012";
DateTimeFormatter f = DateTimeFormatter.ofPattern("YYYY-MM-DD");
LocalDate ld = LocalDate.parse(Date, f);

LocalDate nextOrSameFriday = ld.with(TemporalAdjusters
        .nextOrSame(DayOfWeek.FRIDAY));
String output = nextOrSameFriday.toString();
//String output = ld.format( f );

if (nextOrSameFriday.getMonthValue() != ld.getMonthValue()) {
    nextOrSameFriday = ld.with(TemporalAdjusters
            .nextOrSame(DayOfWeek.THURSDAY));
    if (nextOrSameFriday.getMonthValue() == ld.getMonthValue())
        return nextOrSameFriday.toString();

    else if (nextOrSameFriday.getMonthValue() != ld.getMonthValue()) {
        nextOrSameFriday = ld.with(TemporalAdjusters
                .nextOrSame(DayOfWeek.WEDNESDAY));

        if (nextOrSameFriday.getMonthValue() == ld.getMonthValue())
            return nextOrSameFriday.toString();

        else if (nextOrSameFriday.getMonthValue() != ld.getMonthValue()) {
            nextOrSameFriday = ld.with(TemporalAdjusters
                    .nextOrSame(DayOfWeek.TUESDAY));
            if (nextOrSameFriday.getMonthValue() == ld.getMonthValue())
                return nextOrSameFriday.toString();
            else
                return Date;

        }

    }

}

在选择月份和年份后,我想要做以下事情:

1)找到本月的第一天

2)找到MONTH的第一个星期五

3)检查星期六或星期日,在DATE添加1或2

4)在周一的第一天增加2天以获得第二周的第一天。

5)增加5天以获得第二周的周五

6)同样继续直到月底

对此有任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

Stack Overflow通常不是代码编写服务,我希望你已经知道了。这个问题可能有点复杂,而不是试图解释我认为编写代码片段更简单,它也提供更高的精度。与此同时,我不太确定你要追求的是什么,但我相信我很接近,你会做出你需要的修改。

private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("d-M-uuuu");

public static void printWorkWeeksOfMonth(String date) {
    LocalDate ld = LocalDate.parse(date, DATE_FORMAT);
    LocalDate lastDayOfMonth = ld.with(TemporalAdjusters.lastDayOfMonth());

    // find first working day of month; also first working day of first work week
    LocalDate firstWorkingDay = ld.withDayOfMonth(1);
    if (ld.getDayOfWeek() == DayOfWeek.SATURDAY || ld.getDayOfWeek() == DayOfWeek.SUNDAY) {
        firstWorkingDay = firstWorkingDay.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
    }
    while (! firstWorkingDay.isAfter(lastDayOfMonth)) {
        // find last working day of week beginning on firstWorkingDay
        LocalDate lastWorkingDay = firstWorkingDay.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
        if (lastWorkingDay.isAfter(lastDayOfMonth)) {
            // end work week at end of month
            lastWorkingDay = lastDayOfMonth;
        }
        System.out.println(firstWorkingDay.format(DATE_FORMAT)
                + " through " + lastWorkingDay.format(DATE_FORMAT));

        // find beginning of next work week
        firstWorkingDay = firstWorkingDay.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
    }
}

如果我将2-2-2016提供给上述方法,则会打印:

1-2-2016 through 5-2-2016
8-2-2016 through 12-2-2016
15-2-2016 through 19-2-2016
22-2-2016 through 26-2-2016
29-2-2016 through 29-2-2016

给出29-4-2016打印:

1-4-2016 through 1-4-2016
4-4-2016 through 8-4-2016
11-4-2016 through 15-4-2016
18-4-2016 through 22-4-2016
25-4-2016 through 29-4-2016

换句话说,该方法将date落入的月份的工作周打印到标准输出。您可能希望将方法更改为接受YearMonth而不是String,因为它不使用确切的日期。或者您可能想要更改它,以便以某种方式使用确切的日期。