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

前端 未结 28 1776

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

    If you are using Java 8, java.time.LocalDate and java.time.format.DateTimeFormatter can make this work quite simple.

    public String nextDate(String date){
          LocalDate parsedDate = LocalDate.parse(date);
          LocalDate addedDate = parsedDate.plusDays(1);
          DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd");
          return addedDate.format(formatter); 
    }
    

提交回复
热议问题