How can I increment a date by one day in Java?

前端 未结 28 1828

I\'m working with a date in this format: yyyy-mm-dd.

How can I increment this date by one day?

28条回答
  •  一向
    一向 (楼主)
    2020-11-21 07:17

    Date today = new Date();               
    SimpleDateFormat formattedDate = new SimpleDateFormat("yyyyMMdd");            
    Calendar c = Calendar.getInstance();        
    c.add(Calendar.DATE, 1);  // number of days to add      
    String tomorrow = (String)(formattedDate.format(c.getTime()));
    System.out.println("Tomorrows date is " + tomorrow);
    

    This will give tomorrow's date. c.add(...) parameters could be changed from 1 to another number for appropriate increment.

提交回复
热议问题