LoginSignup
4
1

More than 5 years have passed since last update.

プレミアムフライデーを求めるメソッドをワンライナーで作った

Last updated at Posted at 2017-02-28

可読性は最悪
2017年のとき1月が入ってしまうのは勘弁してください。


import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.util.Arrays;
import java.util.stream.IntStream;

public class PremiumFriday {
    public static void main(String[] args) {
        String year = "2017";
        Arrays.stream(Month.values()).map(m ->IntStream.range(1,YearMonth.of(Integer.parseInt(year), m).atEndOfMonth().lengthOfMonth()+1).mapToObj(d -> LocalDate.of(Integer.parseInt(year), m, d)).filter(d -> DayOfWeek.FRIDAY.equals(d.getDayOfWeek())).max(LocalDate::compareTo)).forEach(o -> System.out.println(o.get()));
    }
}

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

※※※
yearをStringにしたのは「argsから取ろうかなー?でも渡すのめんどくさいなー」とか考えてたなごりでした。あとJavaで入力値ってなんとなくStringな気がして。Intかjava.time.Yearが自然ですね。

4
1
3

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
4
1