如何找到给定年月日的日期?

How to find the day when year, month and date is given?

我需要 return 天(星期一、星期二...)的解决方案,当我输入 YEAR、MONTH、DATE 时。

public static String findDay(int month, int day, int year) { // code here }

    

使用参数,创建一个 LocalDate,您可以从中获取工作日。

import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {
    public static void main(String args[]) {
        // Test
        System.out.println(findDay(2021, 4, 30));
    }

    public static String findDay(int year, int month, int day) {
        LocalDate date = LocalDate.of(year, month, day);
        return date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
    }
}

输出:

Friday

详细了解 modern date-time API* from Trail: Date Time


* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and