[JAVA] I made a method to ask for Premium Friday

Premium Friday started on Friday, February 24th, 2017. The pros and cons of the system are fierce, but it's good to have more holidays anyway. Did you all leave the office at 3:00 pm on your first Premium Friday? __ I couldn't (´ ・ ω ・ `) __ I couldn't even leave the office on time (´ ・ ω ・ `)

So (?) I made a method to calculate Premium Friday. Please use it.


import java.util.Calendar;

public class PremiumFridayUtils {
    
    /**
     *Find the Premium Friday day of the specified month. For months without Premium Friday-Returns 1.
     * @param year year(1..*)
     * @param month month(1..12)
     * @return Premium Friday day of the specified month(1..31)
     */
    public static int getPremiumFriday(int year, int month) {
        //year must be a positive number and month must be between 1 and 12 respectively.
        if (year < 1 || month < 1 || month > 12) {
            throw new IllegalArgumentException();
        }
        
        //Premium Friday will be held after February 2017.
        if (year < 2017 || (year == 2017 && month == 1)) {
            return -1;
        }
        
        //It is judged whether it is Friday one day from the last day of the target month, and the first day judged as Friday is
        //Last Friday of the month=It will be Premium Friday.
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY) {
            calendar.add(Calendar.DATE, -1);
        }
        
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
        
    public static void main(String[] args) {
        // 2017-Output for 2019 Premium Friday.
        for (int year : new int[]{2017, 2018, 2019}) {
            for (int month = 1; month <= 12; month++) {
                int day = PremiumFridayUtils.getPremiumFriday(year, month);
                if (day != -1) {
                    System.out.printf("%d-%02d-%02d\n", year, month, day);
                }
            }
        }
    }
}
/*
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
*/

Recommended Posts

I made a method to ask for Premium Friday
I made a method to ask for Premium Friday (Java 8 version)
Made a one-liner method 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 plugin for IntelliJ IDEA
I made a Diff tool for Java files
[Ruby] I want to do a method jump!
I made a Docker container to run Maven
I want to call a method of another class
I made a library for displaying tutorials on Android.
I want to create a generic annotation for a type
I made a chat app.
How to create a method
I made a plugin to execute jextract with Gradle task
[Beginner] I made a program to sell cakes in Java
I want to call a method and count the number
How to test a private method with RSpec for yourself
[Personal development] I made a site to recruit test users!
I made a Dockerfile to start Glassfish 5 using Oracle Java
I want to recursively search for files under a specific directory
Ruby: I made a FizzBuzz program!
I want to create a chat screen for the Swift chat app!
I made a shopify app @java
I made a GUI with Swing
I made a reply function for the Rails Tutorial extension (Part 1)
I tried to explain the method
I made a gem to post the text of org-mode to qiita
I made a question that can be used for a technical interview
I made a simple recommendation function.
I made a reply function for the Rails Tutorial extension (Part 5):
[Java] I tried to make a maze by the digging method ♪
[Introduction to JSP + Servlet] I played with it for a while ♬
Why do I have to do a "method"? ~ "Main only" would have worked ~
I made a tool to output the difference of CSV file
I made a matching app (Android app)
I made a package.xml generation tool.
[Android] I made a pedometer app.
[JDBC] I tried to make SQLite3 database access from Java into a method for each SQL statement.
I was in trouble at work, so I made a plugin for IntelliJ
I made an app to scribble with PencilKit on a PDF file
I made a virtual currency arbitrage bot and tried to make money
I made a reply function for Rails Tutorial extension (Part 2): Change model
I want to develop a web application!
I want to write a nice build.gradle
[Ruby] I made a simple Ping client
[For programming beginners] What is a method?
I want to write a unit test!
How to create a Maven repository for 2020
I wanted to add @VisibleForTesting to the method
I was addicted to the roll method
I made a risky die with Ruby
I made a rock-paper-scissors app with kotlin
I made a calculator app on Android
I made a new Java deployment tool
A simple and convenient method for HashMap
I made a rock-paper-scissors app with android
I made a bulletin board using Docker 1
A note that I gave up trying to make a custom annotation for Lombok
[Android] I want to create a ViewPager that can be used for tutorials
[Go To Travel] I searched for a plan with a quo card in Jalan
I made a THETA API client that can be used for plug-in development