- java.util.Calendar
- java.text.SimpleDateFormat
が使えません。そこで、Dateクラスに頼らざるをえないのですが、DateクラスについてもgetYear() , getMonth() などは, deprecated (非推奨) になっています。このような制約のもと、任意の日付の週番号を取得するにはどうしたらよいか、、、ということですが、その解決策を下記に載せます。CalendarUtilクラスを利用するのがポイントですね。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int getWeekOfYear(Date nowTime){ | |
//コピーする(年始の日付1/1) | |
Date janTime = CalendarUtil.copyDate(nowTime); | |
//月初め | |
CalendarUtil.setToFirstDayOfMonth(janTime); | |
DateTimeFormat fmt = DateTimeFormat.getFormat("M"); | |
String nowMonth = fmt.format(nowTime); | |
CalendarUtil.addMonthsToDate(janTime, -Integer.parseInt(nowMonth)+1); | |
int daysBetween = CalendarUtil.getDaysBetween(janTime, nowTime); | |
DateTimeFormat fmtWeek = DateTimeFormat.getFormat("EEE"); | |
// 1.1の曜日番号を求める | |
int dayNum = getDayNumber(janTime); | |
int weekNum = (int)(daysBetween+dayNum)/7+1; | |
return weekNum; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* GWT では Calendarクラスが使えないので。。。。 | |
* @param day | |
* @return | |
*/ | |
private int getDayNumber(Date day){ | |
DateTimeFormat fmt = DateTimeFormat.getFormat("EEE"); | |
Map<String, String> map = new HashMap<String,String>(); | |
map.put("Sun", "0"); | |
map.put("Mon", "1"); | |
map.put("Tue", "2"); | |
map.put("Wed", "3"); | |
map.put("Thr", "4"); | |
map.put("Fri", "5"); | |
map.put("Sat", "6"); | |
String temp = fmt.format(day); | |
return Integer.parseInt(map.get(temp)); | |
} |
0 件のコメント:
コメントを投稿