LoginSignup
56
18

More than 5 years have passed since last update.

プレミアムフライデーを求めるメソッドを作ろうと思ったらJava8の標準APIに既にあった

Posted at

元ネタ
プレミアムフライデーを求めるメソッドを作った - Qiita
プレミアムフライデーを求めるメソッドを作った(Java8版) - Qiita
プレミアムフライデーを求めるメソッドをワンライナーで作った - Qiita

Java 8 なら TemporalAdjuster 作った方が Date and Time API 使ってる感出ると思って作ろうとしたら既にあった。

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)
そのままずばり月の最終金曜日が取れる。

実行結果
2017/02/24(金)
2017/03/31(金)
2017/04/28(金)
2017/05/26(金)
2017/06/30(金)
2017/07/28(金)
2017/08/25(金)
2017/09/29(金)
2017/10/27(金)
2017/11/24(金)
2017/12/29(金)
2018/01/26(金)
2018/02/23(金)
2018/03/30(金)
2018/04/27(金)
2018/05/25(金)
2018/06/29(金)
2018/07/27(金)
2018/08/31(金)
2018/09/28(金)
2018/10/26(金)
2018/11/30(金)
2018/12/28(金)
2019/01/25(金)
2019/02/22(金)
2019/03/29(金)
2019/04/26(金)
2019/05/31(金)
2019/06/28(金)
2019/07/26(金)
2019/08/30(金)
2019/09/27(金)
2019/10/25(金)
2019/11/29(金)
2019/12/27(金)
56
18
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
56
18