Zeller's official (asking for the day of the week)

public class Main {

    public static void main(String[] args) {
        System.out.println(calc(2018, 3, 28)); //Wednesday
        System.out.println(calc(2018, 2, 28)); //Wednesday
        System.out.println(calc(2018, 1, 28)); //Sunday
    }

    public static String calc(
            double y,
            double m,
            double d) {

        double M = 0;
        if (m < 3) {
            M = m + 12;
            y = y - 1;
        } else {
            M = m;
        }

        int C = (int) (y / 100);
        int Y = (int) (y % 100);

        double R = 0;

        if (y >= 1582) {
            R = (C / 4) - C - C;
        } else if (y >= 4) {
            R = 5 - C;
        } else {
            throw new IllegalArgumentException("year >= 4");
        }

        Double h = (d + ((26 * (M + 1)) / 10) + Y + (Y / 4) + R) % 7;

        String[] weekday = new String[] {
                "soil",
                "Day",
                "Month",
                "fire",
                "water",
                "wood",
                "Money"
        };
        return weekday[h.intValue()];

    }

}

Recommended Posts

Zeller's official (asking for the day of the week)
[Java] Get the day of the specific day of the week
I want to output the day of the week
How to get today's day of the week
[Ruby] Code to display the day of the week
[Ubuntu 20.04] Display the day of the week on the date / clock
Output of the day of the week using Ruby's Date class
[Ruby] Display today's day of the week using Date class
Official logo list of the service
[Java1.8 +] Get the date of the next x day of the week with LocalDate
Change the conversation depending on which day of the week is today
Understand the official sample Coffee of Dagger2
[day: 5] I summarized the basics of Java
Retrieve the first day of week in current locale (what day of the week is it today?)
[Java] Calculate the day of the week from the date (Calendar class is not used)
About the official start guide of Spring Framework
Compare the speed of the for statement and the extended for statement.
The official name of Spring MVC is Spring Web MVC
[Ruby] I want to make a program that displays today's day of the week!
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
[For beginners] DI ~ The basics of DI and DI in Spring ~
[For beginners] Quickly understand the basics of Java 8 Lambda