本文介紹了calendar.getDisplayName 返回錯誤的日期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
以下是我的程序,當我輸入相關的月份、日期和年份時,它返回錯誤的日期名稱.
Below is my program and it is returning the wrong day name when I enter the related month, date and year.
我在這里缺少什么?
我的計劃
import java.util.Calendar;
import java.util.Locale;
import java.util.Scanner;
public class TimeTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String month = in.next();
String day = in.next();
String year = in.next();
System.out.println(getDay(day, month, year));
}
private static String getDay(String day, String month, String year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
return calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
}
}
輸出:
09
24
2018
Wednesday
它應該返回星期一",因為那是當天.
It should be returning "Monday", as that is the current day.
推薦答案
假設它是 9 月,但實際上在Calendar 類的術語是 August,因為 Calendar 類中月份從 00(Jan)、01(Feb)...
等開始.
You are passing 09 as Month assuming it's September but actually in term of Calendar class it's August because months start from 00(Jan), 01(Feb)...
and so on in Calendar Class.
因此,為了獲得正確的輸出,您需要通過
Hence in order to the get correct output you need to pass
08 // September Month not August
24
2018
這是您的運行代碼
這篇關于calendar.getDisplayName 返回錯誤的日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!