I read I made a method to find Premium Friday and wrote a Java 8 version for studying Java 8.
import java.time.*;
import java.time.format.DateTimeFormatter;
public class PremiumFriday {
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 ym = YearMonth.of(year, month);
LocalDateTime pf = getMonthOfPremiumFriday(ym);
System.out.println(DateTimeFormatter.ofPattern("yyyy/MM/dd(E)").format(pf));
}
}
}
private static LocalDateTime getMonthOfPremiumFriday(YearMonth ym) {
LocalDate ls = ym.atEndOfMonth();
LocalDateTime ldt = ls.atStartOfDay();
while (true) {
if (ldt.getDayOfWeek() == DayOfWeek.FRIDAY) {
break;
} else {
ldt = ldt.plusDays(-1);
}
}
return ldt;
}
}
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)
It's simple and easy to understand when I write it, but it was hard to find out that I needed to update my knowledge ...
I received advice on how to write on Twitter, so I rewrote it.
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
public class PremiumFriday {
public static void main(String[] args) {
int[] years = {2017, 2018, 2019};
for (int year : years) {
for (Month month : Month.values()) {
if (year == 2017 && month == Month.JANUARY) {
continue;
}
YearMonth ym = YearMonth.of(year, month);
LocalDate pf = getMonthOfPremiumFriday(ym);
System.out.println(DateTimeFormatter.ofPattern("yyyy/MM/dd(E)").format(pf));
}
}
}
private static LocalDate getMonthOfPremiumFriday(YearMonth ym) {
LocalDate premiumFriday = ym.atEndOfMonth().with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY));
return premiumFriday;
}
}
If I have a chance to use Java 8 at work, I will definitely do this once, so I will search on Qiita!
Recommended Posts