public class Untitled1{
  public String truansDate(String date,int ds){
    Calendar cal = Calendar.getInstance();
    int year = Integer.parseInt(date.substring(0,4));
    int month = Integer.parseInt(date.substring(5,7));
    int day = Integer.parseInt(date.substring(8,10));
    cal.set(year,month,day,0,0,0);
    cal.add(Calendar.DATE,ds);
    return (cal.getTime().getYear()+1900)+"/"+cal.getTime().getMonth()+"/"+cal.getTime().getDate();
  }
  public static void main(java.lang.String[] args) {
    Untitled1 u = new Untitled1();
    System.out.println(u.truansDate("2003/02/12",-13));
  }
}

解决方案 »

  1.   

    String strBeginDate = "2003.08.21 12:23:33";
    String strDeadLine = "3";
    Calendar calendar = Calendar.getInstance();
    DateFormat f = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
    try
    {
        calendar.setTime(f.parse(strBeginDate));
        showInfo("差异时间:" + Integer.parseInt(strDeadLine));
        calendar.add(Calendar.DAY_OF_YEAR,Integer.parseInt(strDeadLine));
        showInfo("处理时限:" + f.format(calendar.getTime()).toString());
        showInfo(f.format(calendar.getTime()).toString());
    }
    catch(ParseException e)
    {
        e.printStackTrace();
    }    public void showInfo(String strInfo)
        {
            if(DEBUG)
            {
                System.out.println(strInfo);
            }
        }
      

  2.   

    楼上的楼上的程序有点小问题,如果:2003/02/12 加上19 运行结果为
    2003/2/31
    我这又个程序:
    public class testData { /**
     * 
     */
    public testData() {
    GregorianCalendar testData = new GregorianCalendar(2003,1, 18);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    testData.add(Calendar.DATE, 12);
    Date date = testData.getTime();
    String str = format.format(date);
    System.out.println("15 day testData amortized on " + str);

    } public static void main(String[] args) {
    testData dd = new testData();
    }
    }
    想把直接输入的日期 换成一个变量,从外面可以通过输入的变量来调用此方法,
    怎么改呢?()
      

  3.   

    如果我做我会用TimeMillis来加减,效率应该会比楼上的慢不过觉得比较通用。
    public calDate(Date date, long timeMillis){
      long tmpTime = date.getTime() + timeMillis;
      return new Date(tmptime);
    }
      

  4.   

    如果是日历格式
    public Calendar calDate(Calendar date, long timeMillis){
      long tmpTime = date.getTime().getTime();
      return date.clone().setTimeInMillis(tmpTime);
    }
    上面的回复漏了返回类型,不好意思。