LoginSignup
12
3

More than 5 years have passed since last update.

プレミアムフライデーを求めるメソッドを作った(Java8版)

Last updated at Posted at 2017-02-28

初稿

プレミアムフライデーを求めるメソッドを作った を読んで、Java8の勉強も兼ねてJava8版を書いてみた。

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;

    }

}

結果

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(金)

書いてみるとシンプルかつ分かりやすいけど、知識のアップデートが必要ということが分かって辛かった…。

2017/3/2 追記

Twitterで書き方のアドバイスを戴いたので書き直しました。

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;

    }

}

ポイント

  1. 月のループ条件に Month (Java Platform SE 8 ) を使うように変更した。
  2. 最終金曜日の判定にTemporalAdjusters (Java Platform SE 8 )を使うように変更した。
    1. 使い方については、LocalDate (Java Platform SE 8 )も参照。
  3. ラムダ式を使った書き方は他の方の記述(この記事は以下の記事からリンクされています)を参照。
    部分部分で何をしているか将来の自分がわかるように、あえてラムダは使っていない。

将来の自分へのメッセージ

仕事でJava8を使う機会があったら、絶対一度はこれやるからQiitaで検索するんだぞ!

12
3
0

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
12
3