Calendar.add
public abstract void add(int field,
                         int amount)
Date Arithmetic function. Adds the specified (signed) amount of time to the given time field, based on the calendar's rules. For example, to subtract 5 days from the current time of the calendar, you can achieve it by calling: 
add(Calendar.DATE, -5). 
Parameters:
field - the time field.
amount - the amount of date or time to be added to the field.

解决方案 »

  1.   

    你的程序基本正确,有两处要改         Calendar nowTime = new GregorianCalendar();
             
             //第一处要改的
             //2004-11-10 0:20:20 我要算出该日期前12天的日期
             //如果要设置成11月10号,那么底下的设置日期应该是2004,10,11 也就是月份少一 nowTime.set(2004,10,11,8,20,20);
    long timeMillis=nowTime.getTimeInMillis();
    System.out.println(timeMillis);
    timeMillis=timeMillis - 11*60*60*24*1000;
    nowTime.setTimeInMillis(timeMillis); 
    Date result=nowTime.getTime();         //第二处要改的  格式串应该为 yyyy/MM/dd hh:mm:ss
    SimpleDateFormat fmtDte = new SimpleDateFormat ("yyyy/MM/dd hh:mm:ss");

             System.out.println (fmtDte.format(result)); 
             
    我这里的输出结果是:2004/10/31 08:20:20
      

  2.   

    import java.util.*;
    import java.text.SimpleDateFormat;class CalendarTest1 {public static void main(String []arg)throws Exception{
    int k=1;
             //java.util.Date dteTmp = new java.util.Date ("2004/05/01 10:10:10");
    SimpleDateFormat fmtDte = new SimpleDateFormat ("yyyy/MM/dd hh:mm:ss"); 
    java.util.Date dteTmp= fmtDte.parse("2004/05/01 10:10:10");
    System.out.println(fmtDte.format(dteTmp));
            Calendar d=Calendar.getInstance();
            d.setTime(dteTmp);
            d.add(Calendar.DAY_OF_MONTH,-12);
            System.out.println(fmtDte.format(d.getTime()));
       }
    }
    楼主看合不合要求???
      

  3.   

    vgvg(巩伟) 说的完全正确,12个月就是Calendar.MONTH,12年Calendar.YEAR,加就是正,见就是负
      

  4.   

    import java.util.*;
    import java.text.*;
    public class sss
    {
       public static void main(String[] args) {
    int strTo;
    String to="-10"; try {
          strTo = Integer.parseInt(to);
        }
        catch (Exception e) {
          System.out.println("日期标识转换出错! : \n:::" + to + "不能转为数字型");
          e.printStackTrace();
          strTo = 0;
        }
        Calendar strDate = Calendar.getInstance(); 
        strDate.add(strDate.DATE, strTo); 
       //生成 (年-月-日) 字符串
        String meStrDate = strDate.get(strDate.YEAR) + "-" +
            String.valueOf(strDate.get(strDate.MONTH)+1) + "-" + strDate.get(strDate.DATE);
    System.out.println(meStrDate);}
    }