Java 8:在每月的特定日期之后找到第n个DayOfWeek [英] Java 8: Find nth DayOfWeek after specific day of month

查看:84
本文介绍了Java 8:在每月的特定日期之后找到第n个DayOfWeek的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 8中,我发现的是

In Java 8, what I found is

TemporalAdjuster temporal = dayOfWeekInMonth(1,DayOfWeek.MONDAY) 

在一个月的第一个星期一提供时间,并且

gives the temporal for the first Monday of a month, and

next(DayOfWeek.MONDAY)

在特定日期之后的下一个星期一.

gives the next Monday after a specific date.

但是我想在特定日期之后找到第n个星期一.

But I want to find nth MONDAY after specific date.

例如,我要在2017年6月6日之后的第二个星期一,应该是2017年6月19日

For example, I want 2nd MONDAY after 2017-06-06 and it should be 2017-06-19 where

dayOfWeekInMonth(2,DayOfWeek.MONDAY)

会给我2017-06-12和

will give me 2017-06-12 and

next(DayOfWeek.MONDAY) 

对于第n个DayOfWeek指标,当然没有任何参数.它将给出下一个第一个星期一,即2017年6月12日.

has of course no parameter for the nth DayOfWeek's indicator. It will give the next first MONDAY which is 2017-06-12.

如何计算而不循环?

推荐答案

编写找到下一个星期一,然后添加(N-1)周:

Find the next monday, then add (N - 1) weeks:

public static TemporalAdjuster nextNthDayOfWeek(int n, DayOfWeek dayOfWeek) {
    return temporal -> {
        Temporal next = temporal.with(TemporalAdjusters.next(dayOfWeek));
        return next.plus(n - 1, ChronoUnit.WEEKS);
    };
}

将您的 TemporalAdjuster 对象传递到 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆