java获取时间增加年月日

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = new Date();

        String stringDate = sdf.format(date);//date-->String

        System.out.println(stringDate);

        Calendar calendar = new GregorianCalendar();

        calendar.setTime(date);


        calendar.add(Calendar.YEAR, 1); //把日期往后增加一年,整数往后推,负数往前移

        date = calendar.getTime();

        stringDate = sdf.format(date);//date-->String

        System.out.println(stringDate);


        calendar.add(Calendar.MONTH, 1); //把日期往后增加一个月,整数往后推,负数往前移

        date = calendar.getTime();

        stringDate = sdf.format(date);//date-->String

        System.out.println(stringDate);


         calendar.add(Calendar.WEEK_OF_MONTH, 1); //把日期往后增加一周,整数往后推,负数往前移

          date = calendar.getTime();

          stringDate = sdf.format(date);//date-->String

          System.out.println(stringDate);


          calendar.add(Calendar.DAY_OF_MONTH, 1); //把日期往后增加一天,整数往后推,负数往前移

          date = calendar.getTime();

          stringDate = sdf.format(date);//date-->String

          System.out.println(stringDate);

2018-11-0218:35:48

2019-11-0218:35:48

2019-12-0218:35:48

2019-12-0918:35:48

2019-12-1018:35:48

你可能感兴趣的:(java获取时间增加年月日)