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

前端 未结 28 1780

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

    It's very simple, trying to explain in a simple word. get the today's date as below

    Calendar calendar = Calendar.getInstance();
    System.out.println(calendar.getTime());// print today's date
    calendar.add(Calendar.DATE, 1);
    

    Now set one day ahead with this date by calendar.add method which takes (constant, value). Here constant could be DATE, hours, min, sec etc. and value is the value of constant. Like for one day, ahead constant is Calendar.DATE and its value are 1 because we want one day ahead value.

    System.out.println(calendar.getTime());// print modified date which is tomorrow's date

    Thanks

提交回复
热议问题