本文介紹了月份不是從日期打印的 - Java DateFormat的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
如何從java中的日期獲取月份:
How to get month from a date in java :
DateFormat inputDF = new SimpleDateFormat("mm/dd/yy");
Date date1 = inputDF.parse("9/30/11");
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int year = cal.get(Calendar.YEAR);
System.out.println(month+" - "+day+" - "+year);
此代碼返回日期和年份,但不返回月份.
This code return day and year but not month.
輸出:
0 - 30 - 2011
推薦答案
這是因為你的格式不正確:你需要"MM/dd/yy"
這個月份,因為"mm"
是分鐘:
This is because your format is incorrect: you need "MM/dd/yy"
for the month, because "mm"
is for minutes:
DateFormat inputDF = new SimpleDateFormat("MM/dd/yy");
Date date1 = inputDF.parse("9/30/11");
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int year = cal.get(Calendar.YEAR);
System.out.println(month+" - "+day+" - "+year);
打印 8 - 30 - 2011
(因為月份是從零開始的;demo)
Prints 8 - 30 - 2011
(because months are zero-based; demo)
這篇關于月份不是從日期打印的 - Java DateFormat的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!