根据输入日期检索星期几

时间:2017-07-03 07:08:11

标签: java

任何人都可以告诉我我的代码有什么问题吗?我的输出结果不正确。干杯

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String[] days={"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
    int month = in.nextInt();
    int day = in.nextInt();
    int year = in.nextInt();
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR,  year);
    c.set(Calendar.MONTH,  month);
    c.set(Calendar.DAY_OF_MONTH,  day);
    int day_of_week = c.get(Calendar.DAY_OF_WEEK)-1;
    System.out.println(days[day_of_week]);
}

1 个答案:

答案 0 :(得分:5)

阅读文档,然后转到

c.set(Calendar.MONTH,  month -1);

因为它接受基于零的索引