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

前端 未结 28 2177

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:18

    Just pass date in String and number of next days

     private String getNextDate(String givenDate,int noOfDays) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            String nextDaysDate = null;
        try {
            cal.setTime(dateFormat.parse(givenDate));
            cal.add(Calendar.DATE, noOfDays);
    
           nextDaysDate = dateFormat.format(cal.getTime());
    
        } catch (ParseException ex) {
            Logger.getLogger(GR_TravelRepublic.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
        dateFormat = null;
        cal = null;
        }
    
        return nextDaysDate;
    
    }
    

提交回复
热议问题