我有一个java程序,我可以在yyyy-MM-dd中设置日期,我可以从中获取下一个和之前的日期。以下方法做了工作,
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
请告诉我哪里错了......
答案 0 :(得分:2)
为您修复getNextMonth。 getPreviousMonth也需要做同样的事情。请参阅以下代码内联的评论:
public String getNextMonth(boolean maxDate) {
Calendar calendar = Calendar.getInstance();
if (maxDate) {
calendar.set(year, monthNo, 1);
/* Move these two lines to end of method.
int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
calendar.set(year, monthNo, maxD);
*/
} else {
calendar.set(year, monthNo, day);
}
//Not sure why have you used if/else. Both are doing the same thing
if (monthNo == 11) {
calendar.add(calendar.MONTH, 1);
} else {
calendar.add(calendar.MONTH, 1);
}
//here i.e. After you have calculated your next month.
int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
calendar.set(year, monthNo, maxD);
}
答案 1 :(得分:1)
问题是您在实际更改月份之前检索了当月的最大天数 这应该怎么看。
public String getNextMonth(boolean maxDate) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthNo, 1);
calendar.add(Calendar.MONTH, 1);
if (maxDate) {
int maxD = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, maxD);
} else {
calendar.set(Calendar.DAY_OF_MONTH, day);
}
String date = (calendar.get(Calendar.YEAR)) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
calendar.clear();
return date;
}