如何从Java中的日期减去两天?

时间:2016-10-06 11:25:48

标签: java date

    String dateSample = "2016-09-30 21:59:22.2500000";

    String oldFormat = "yyyy-MM-dd HH:mm:ss";
    String newFormat = "MM-dd-yyyy";

    SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);
    SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat);

    sdf2.format(sdf1.parse(dateSample));

从此,我得到了09-30-2016 但是,我想要结果09-28-2016 怎么做?

1 个答案:

答案 0 :(得分:0)

Calendar cal = GregorianCalendar.getInstance();
cal.setTime( sdf1.parse(dateSample));
cal.add( GregorianCalendar.DAY_OF_MONTH, -2); // date manipulation
System.out.println(sdf2.format(cal.getTime())); 

希望我帮助