java中日期加减计算(转)

时间:2021-07-27 18:11:14

 .NET里面日期的相关计算真是太简单了,直接有方法,java咋就这么麻烦呢!

public static void main(String[] args) {
        String strNow = "20060821010101";                 //当前日期 yyyyMMddhhmmss格式
        String strAddDays = "-100";                              //求前100天的日期

        Date dt = new Date(strNow .subString(0,4) + "/"
                    + strNow .subString(4,6) + "/"
                    + strNow .subString(6,8) + " "
                   + strNow .subString(8,10) + ":"
                   + strNow .subString(10,12) + ":"
                   + strNow .subString(12,14) );

     long now = dt.getTime();
     long next = now + Long.parselong(strAddDays) * 24 * 3600 * 1000;    //一天24小时*3600秒*1000毫秒
    
     Date dtNext = new Date(next);

    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
    String strNext = df.format(dtNext);
}