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

前端 未结 28 1817

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

    It's simple actually. One day contains 86400000 milliSeconds. So first you get the current time in millis from The System by usingSystem.currentTimeMillis() then add the 84000000 milliSeconds and use the Date Class to generate A date format for the milliseconds.

    Example

    String Today = new Date(System.currentTimeMillis()).toString();

    String Today will be 2019-05-9

    String Tommorow = new Date(System.currentTimeMillis() + 86400000).toString();

    String Tommorow will be 2019-05-10

    String DayAfterTommorow = new Date(System.currentTimeMillis() + (2 * 86400000)).toString();

    String DayAfterTommorow will be 2019-05-11

提交回复
热议问题