問題描述
讓我解釋一下.通過知道日期的周數和年份:
Let me explain myself. By knowing the week number and the year of a date:
Date curr = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(curr);
int nweek = cal.WEEK_OF_YEAR;
int year = cal.YEAR;
但現在我不知道如何獲取該周第一天的日期.我一直在尋找日歷、日期、日期格式,但沒有什么有用的...
But now I don't know how to get the date of the first day of that week. I've been looking in Calendar, Date, DateFormat but nothing that may be useful...
有什么建議嗎?(在 Java 中工作)
Any suggestion? (working in Java)
推薦答案
那些字段不返回值.這些是constants,用于標識Calendar
對象中您可以獲取/設置/添加的字段.要實現你想要的,你首先需要得到一個Calendar
,清除它并設置已知值.它會自動將日期設置為該周的第一天.
Those fields does not return the values. Those are constants which identifies the fields in the Calendar
object which you can get/set/add. To achieve what you want, you first need to get a Calendar
, clear it and set the known values. It will automatically set the date to first day of that week.
// We know week number and year.
int week = 3;
int year = 2010;
// Get calendar, clear it and set week number and year.
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.WEEK_OF_YEAR, week);
calendar.set(Calendar.YEAR, year);
// Now get the first day of week.
Date date = calendar.getTime();
請學習閱讀 javadocs 學習如何使用類/方法/字段,不要嘗試在你的 IDE 中隨意戳 ;)
Please learn to read the javadocs to learn how to use classes/methods/fields and do not try to poke random in your IDE ;)
也就是說,java.util.Date
和 java.util.Calendar
是 史詩般的失敗.如果可以,請考慮切換到 Joda Time.
That said, the java.util.Date
and java.util.Calendar
are epic failures. If you can, consider switching to Joda Time.
這篇關于如何在Java中獲取給定周數的第一天的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!