Incrementing a java.util.Date by one day

前端 未结 8 1034

What is the correct way to increment a java.util.Date by one day.

I\'m thinking something like

        Calendar cal = Calendar.getInstance();
        ca         


        
相关标签:
8条回答
  • 2021-02-07 01:59

    Yeah, that's right. Java Date APIs feel wrong quite often. I recommend you try Joda Time. It would be something like:

    DateTime startDate = ...
    DateTime endDate = startDate.plusDays(1);
    

    or:

    Instant start = ...
    Instant end = start.plus(Days.days(1).toStandardDuration());
    
    0 讨论(0)
  • 2021-02-07 02:00

    If Java 8 or Joda Time are not an option, you could always opt for Apache DateUtils:

    DateUtils.addDays(myDate, 1);
    
    0 讨论(0)
提交回复
热议问题