本文介紹了java獲取給定日期的一年中的一周的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
給定日期,我如何獲得一年中的一周?我嘗試了以下代碼:
how can I get a week of the year given a date? I tried the following code:
Calendar sDateCalendar = new GregorianCalendar();
sDateCalendar.set(Integer.parseInt(sDateYearAAAA), Integer.parseInt(sDateMonthMM)-1, Integer.parseInt(sDateDayDD));
System.out.format("sDateCalendar %tc
", sDateCalendar);
iStartWeek = sDateCalendar.getWeekYear();
System.out.println("iStartWeek "+iStartWeek+ " "+sDateCalendar.WEEK_OF_YEAR);
我得到:sDateCalendar lun 4 月 23 日 11:58:39 CEST 2012iStartWeek 2012 3
and i obtain: sDateCalendar lun apr 23 11:58:39 CEST 2012 iStartWeek 2012 3
一年中正確的星期是 17.有人可以幫我嗎?
while the correct week of year is 17. Can someone help me ?
推薦答案
你使用的是sDateCalendar.WEEK_OF_YEAR,即靜態整數WEEK_OF_YEAR,見java.util.Calendar類的源碼:
You are using sDateCalendar.WEEK_OF_YEAR, which is the static integer WEEK_OF_YEAR, see the source of the java.util.Calendar class:
public final static int WEEK_OF_YEAR = 3;
要獲取周數,您應該使用:
To get the week number, you should be using:
sDateCalendar.get(Calendar.WEEK_OF_YEAR);
這篇關于java獲取給定日期的一年中的一周的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!