String myDate = "200301";
int year = Integer.parseInt(myDate.substring(0,4));
int month= Integer.parseInt(myDate.substring(4,6));java.util.Date dt= new java.util.Date(year,month,1);dt.setMonth(dt.getMonth() - 3);
year = dt.getYear();
month = dt.getMonth();
String newDate = String.valueOf(year)+String.valueOf(month);
int year = Integer.parseInt(myDate.substring(0,4));
int month= Integer.parseInt(myDate.substring(4,6));java.util.Date dt= new java.util.Date(year,month,1);dt.setMonth(dt.getMonth() - 3);
year = dt.getYear();
month = dt.getMonth();
String newDate = String.valueOf(year)+String.valueOf(month);
jdk1.1以上版本如何实现
getMonth()之类的函数已经deprecated
class test
{
public static void main(String[] args)
{
String myDate = "200304";
int year = Integer.parseInt(myDate.substring(0,4));
int month= Integer.parseInt(myDate.substring(4,6));
if(month-3>=0)
month=month-3;
else {
month=month-3+12;
year-=1;
}
String result=""+year;
if(month<10) result=result+"0";
result=result+month;
System.out.println(result);
}
}
public String getBeforeDate(String strDate, int n) {
String strYear = strDate.substring(0, 4);
String strMouth = strDate.substring(4); int nMouth = Integer.parseInt(strMouth);
int nYear = Integer.parseInt(strYear);
if(nMouth <= n) {
nYear --;
nMouth += 12;
} nMouth -= n; strYear = String.valueOf(nYear); if(nMouth < 10) {
strMouth = "0" + nMouth;
} else {
strMouth = String.valueOf(nMouth);
} return strYear + strMouth;
}方法的前一个参数是形如"200304"这样的日期,后面是要减去的月份,但后面的参数不能太大,呵呵~~~太麻烦了。
String myDate = "200301";
int year = Integer.parseInt(myDate.substring(0,4));
int month= Integer.parseInt(myDate.substring(4,6)); Calendar cal = Calendar.getInstance();
cal.set(year,month-3,1);
year = cal.get(cal.YEAR);
month = cal.get(cal.MONTH); String newDate = String.valueOf(year)+String.valueOf(month);
......
month = cal.get(cal.MONTH);
String newDate="";
if(month>=10)
newDate=Integer.toString(year)+Integer.toString(month);
else
newDate=Integer.toString(year)+"0"+Integer.toString(month);