私はyyyy-MM-ddで日付を設定できるJavaプログラムを持っており、その次と前の日付を取得できます。次のメソッドは、作業を行う,,Javaで日付を処理する方法
public void setDate(String date) {
StringTokenizer st = new StringTokenizer(date, "-");
year = Integer.parseInt(st.nextToken());
monthNo = Integer.parseInt(st.nextToken()) - 1;
day = Integer.parseInt(st.nextToken());
date = year + "-" + monthNo + "-" + day;
}
public String getPreviousMonth(boolean maxDate) {
Calendar calendar = Calendar.getInstance();
if (maxDate) {
calendar.set(year, monthNo, 1);
int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
calendar.set(year, monthNo, maxD);
} else {
calendar.set(year, monthNo, day);
}
if (monthNo == 0) {
calendar.add(calendar.MONTH, -1);
} else {
calendar.add(calendar.MONTH, -1);
}
String date = (calendar.get(calendar.YEAR)) + "-" + (calendar.get(calendar.MONTH) + 1) + "-" + calendar.get(calendar.DAY_OF_MONTH);
calendar.clear();
return date;
}
public String getNextMonth(boolean maxDate) {
Calendar calendar = Calendar.getInstance();
if (maxDate) {
calendar.set(year, monthNo, 1);
int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
calendar.set(year, monthNo, maxD);
} else {
calendar.set(year, monthNo, day);
}
if (monthNo == 11) {
calendar.add(calendar.MONTH, 1);
} else {
calendar.add(calendar.MONTH, 1);
}
public String getCurrentMonth(boolean maxDate){
Calendar calendar = Calendar.getInstance();
if (maxDate) {
calendar.set(year, monthNo, 1);
int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
calendar.set(year, monthNo, maxD);
} else {
calendar.set(year, monthNo, day);
}
String date = (calendar.get(calendar.YEAR)) + "-" + (calendar.get(calendar.MONTH) + 1) + "-" + calendar.get(calendar.DAY_OF_MONTH);
calendar.clear();
return date;
}
私は1月と2月に日付を設定すると、それは私に正しい出力を提供しますが、私は1月と2月以外の月を選択した場合、私は正しいの日得ることができません、 以下は
this is OK
2012-1-31 - current month
2011-12-31 - previous month
2012-2-29 - next month
2012-2-29 current month
2012-1-29 previous month ***day should be 31
2012-3-29 next month ***day should be 31
this is OK
2011-12-31 current month
2011-11-30 previous month
2012-1-31 next month
2011-11-30 current month
2011-10-30 previous month *** this should be 31
2011-12-30 next month *** this should be 31
this is OK
2011-12-31 current month
2011-11-30 previous month
2012-1-31 next month
はあなたのためにgetNextMonthを修正...
ところで、getNextmonth doesnt comiple。 –