现在碰到这样一个问题,写一个函数实现取这一天的上个月的那一天。说明:像有些月的前一个月是没有31号的,则默认用这个月的31号去与上个月的30;另外2月份也是一个特殊的月,闰年时为29天,非闰年为28天,所以闰年的3月份的30,31的上个月为29号,非闰年的29,30,31号的上一个月的同一天为28号。还有就是这个月是1月,这上个月是去年的12月。
通过传入一个yyyymmdd的数据,如何实现上述需求,成功取得上一个月的同一天,请各位JAVA高手相助!!
通过传入一个yyyymmdd的数据,如何实现上述需求,成功取得上一个月的同一天,请各位JAVA高手相助!!
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM//dd");
System.out.println(sdf.format(calendar.getTime()));
for (int i = 0; i < 100; i++) {
calendar.add(Calendar.MONTH, -1);
System.out.println(sdf.format(calendar.getTime()));
}
楼主可以查看一下代码。思路比较简单。public class Test { public Date getBefore(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date); long millis = getBefore(calendar);
calendar.setTimeInMillis(millis); return calendar.getTime();
} private long getBefore(Calendar c) {
int month = (c.get(Calendar.MONTH) + 1);
long monthMillis = 0;
switch (month) {
// 31 day
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthMillis = calculateMillis(31);
break;
// 30 day
case 4:
case 6:
case 9:
case 11:
monthMillis = calculateMillis(30);
break;
// 2 month
default:
if (isLeapYear(c)) {
monthMillis = calculateMillis(29);
} else {
monthMillis = calculateMillis(28);
}
break;
}
System.out.println(monthMillis);
return (c.getTimeInMillis() - monthMillis);
} private long calculateMillis(int month) {
return month * (long) 24 * 60 * 60 * 1000;
} private boolean isLeapYear(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);
return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
} public static void main(String[] args) throws Exception { String source = "20110922";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(source);
//
Test t = new Test();
Date beforeMonth = t.getBefore(date);
System.out.println(sdf.format(beforeMonth));
}
}
不用编写复杂的过程
顶一下
楼主 写Java 应该去看看API文档
public class Test { public Date getBefore(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date); long millis = getBefore(calendar);
calendar.setTimeInMillis(millis); return calendar.getTime();
} private long getBefore(Calendar c) {
int month = (c.get(Calendar.MONTH) + 1);
int day = c.get(Calendar.DATE);
long monthMillis = 0; switch (month) {
// 其它月份都减去31天
// 10月31对应9月30
// 11月30对应10对30
case 1:
case 2:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 11:
case 10:
case 12:
monthMillis = calculateMillis(31);
break;
default:
int febDay = 28;
if (isLeapYear(c)) {
febDay = 29;
}
// 大于2月的天数,则减去当前天数
if (day > febDay) {
monthMillis = calculateMillis(day);
} else {
// 否则减去2月的天数
monthMillis = calculateMillis(febDay);
}
break;
}
System.out.println(monthMillis);
return (c.getTimeInMillis() - monthMillis);
} private long calculateMillis(int month) {
return month * (long) 24 * 60 * 60 * 1000;
} private boolean isLeapYear(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);
return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
} public static void main(String[] args) throws Exception { String source = "20001031";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(source);
//
Test t = new Test();
Date beforeMonth = t.getBefore(date);
System.out.println(sdf.format(beforeMonth));
}
}
这个是完全按照楼主题目要求的意思来做的。