Former story I made a method to find Premium Friday --Qiita I made a method to find Premium Friday (Java 8 version) --Qiita I made a method to find Premium Friday in one liner --Qiita
In Java 8, I thought that it would be better to make TemporalAdjuster
using the Date and Time API, and I already had it.
package premium.friday;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
int[] years = {2017, 2018, 2019};
for (int year : years) {
for (int month = 1; month < 13; month++) {
if (year == 2017 && month == 1) {
continue;
}
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate premiumFriday = yearMonth.atDay(1).with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY));
System.out.println(premiumFriday.format(DateTimeFormatter.ofPattern("yyyy/MM/dd(E)")));
}
}
}
}
TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)
You can get the last Friday of the month as it is.
Execution result
2017/02/24(Money)
2017/03/31(Money)
2017/04/28(Money)
2017/05/26(Money)
2017/06/30(Money)
2017/07/28(Money)
2017/08/25(Money)
2017/09/29(Money)
2017/10/27(Money)
2017/11/24(Money)
2017/12/29(Money)
2018/01/26(Money)
2018/02/23(Money)
2018/03/30(Money)
2018/04/27(Money)
2018/05/25(Money)
2018/06/29(Money)
2018/07/27(Money)
2018/08/31(Money)
2018/09/28(Money)
2018/10/26(Money)
2018/11/30(Money)
2018/12/28(Money)
2019/01/25(Money)
2019/02/22(Money)
2019/03/29(Money)
2019/04/26(Money)
2019/05/31(Money)
2019/06/28(Money)
2019/07/26(Money)
2019/08/30(Money)
2019/09/27(Money)
2019/10/25(Money)
2019/11/29(Money)
2019/12/27(Money)