I made a method to ask for Premium Friday (Java 8 version)

First draft

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 ...

2017/3/2 postscript

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;

    }

}

point

  1. Use Month \ (Java Platform SE 8 ) for the month loop condition Changed to.
  2. Use TemporalAdjusters \ (Java Platform SE 8 ) to judge the last Friday I changed it to use.
  3. See also LocalDate \ (Java Platform SE 8 ) for how to use it.
  4. For how to write using a lambda expression, refer to the description of another person (this article is linked from the following article). I don't dare to use lambda so that I can see what I'm doing in parts in the future.

Message to yourself in the future

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

I made a method to ask for Premium Friday (Java 8 version)
I made a method to ask for Premium Friday
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
I made a Diff tool for Java files
[Beginner] I made a program to sell cakes in Java
I made a Dockerfile to start Glassfish 5 using Oracle Java
I made a shopify app @java
[Java] I tried to make a maze by the digging method ♪
I made a plugin for IntelliJ IDEA
I made a new Java deployment tool
[JDBC] I tried to make SQLite3 database access from Java into a method for each SQL statement.
I made a primality test program in Java
java I tried to break a simple block
I did Java to make (a == 1 && a == 2 && a == 3) always true
I tried hitting a Java method from ABCL
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I made a rock-paper-scissors game in Java (CLI)
I tried to break a block with java (1)
[Azure] I tried to create a Java application for free-Web App creation- [Beginner]
I made a Docker image of SDAPS for Japanese
I made a simple calculation problem game in Java
I made a check tool for the release module
I want to call a method of another class
I tried to create a Clova skill in Java
I tried to make a login function in Java
A study method for inexperienced people to pass Java SE 8 Silver in one month
I made a library for displaying tutorials on Android.
I made a Wrapper that calls KNP from Java
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
I want to create a generic annotation for a type
[Paiza] I made my own utility for answering questions [Java]
I made a Japanese version of Rails / devise automatic email
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
I tried to create a java8 development environment with Chocolatey
[Java] I want to convert a byte array to a hexadecimal number
How to create a lightweight container image for Java apps
I made a plugin to execute jextract with Gradle task
I want to make a list with kotlin and java!
I want to call a method and count the number
I just wanted to make a Reactive Property in Java
I want to make a function with kotlin and java!
Create a method to return the tax rate in Java
How to test a private method with RSpec for yourself
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
I investigated Randoop, a JUnit test class generator for Java
I tried to make a client of RESAS-API in Java
A memorandum to reach the itchy place for Java Gold
A memo to start Java programming with VS Code (2020-04 version)
[Personal development] I made a site to recruit test users!
How to lower java version
Create a java method [Memo] [java11]
I made a chat app.
How to create a method
[Java] How to search for a value in an array (or list) with the contains method
How to deal with the type that I thought about writing a Java program for 2 years
I want to recursively search for files under a specific directory
A story that I struggled to challenge a competition professional with Java
A cheat sheet for Java experienced people to learn Ruby (rails)
I made a reply function for the Rails Tutorial extension (Part 1)
Connecting to a database with Java (Part 1) Maybe the basic method