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

前端 未结 28 1766

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

    Since Java 1.5 TimeUnit.DAYS.toMillis(1) looks more clean to me.

    SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
    Date day = dateFormat.parse(string);
    // add the day
    Date dayAfter = new Date(day.getTime() + TimeUnit.DAYS.toMillis(1));
    

提交回复
热议问题