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);

解决方案 »

  1.   

    上边的办法虽说麻烦了些,但很保险!newDate就是输出的结果!
      

  2.   

    to kesney(兔爷):
       jdk1.1以上版本如何实现
       getMonth()之类的函数已经deprecated
      

  3.   

    如果按照楼主的意思,要实现200301的格式的话,楼上的做发最后还是要处理字符串。变来变去得不偿失。笨办法。恐怕楼主早作出来了。
    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);
     }
    }
      

  4.   

    我也是这样做的,但这样不通用啊。下面是我的代码:
        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"这样的日期,后面是要减去的月份,但后面的参数不能太大,呵呵~~~太麻烦了。
      

  5.   

    我自己试了一下,呵呵,这个可以
        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);
      

  6.   

    改一下:
    ......
    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);
      

  7.   

    to kesney(兔爷) CoolRays(一个人)你们的方法中如果是前一个月,并且当前日期是1月,就不行了,  出来的结果正确的是200212但你们的出来是20030   有待改进!