WEEK给出错误的日期

WEEK给出错误的日期

本文介绍了Java Calendar.DAY_OF_WEEK给出错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码有什么问题?一年中的任何一天都会给出错误的日期.

What is wrong with the below code? It gives wrong day for any date of the year.

import java.util.Scanner;
import java.util.Calendar;
public class Solution {
    public static String getDay(String d, String m, String y) {

        String[] days = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
        Calendar c = Calendar.getInstance();
        c.set(Integer.parseInt(y), Integer.parseInt(m), Integer.parseInt(d));
        return days[c.get(Calendar.DAY_OF_WEEK) - 1];
    }
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String m = in.next();
        String d = in.next();
        String y = in.next();

        System.out.println(getDay(d, m, y));
    }
}

推荐答案

请参阅 Calendar 类的文档: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#set-int-int-int-

月份的值是从0开始的索引,因此,如果您提供 3 作为月份的值,它将被解释为四月".

The value for month is 0-indexed, so if you provide 3 as the month value, it is interpreted as "April".

这篇关于Java Calendar.DAY_OF_WEEK给出错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:39