1

How do I add or subtract a date?

Share:

The java.util.Calendar allows us to do a date arithmetic function such as add or subtract a unit of time to the specified date field.

The method that done this process is the Calendar.add(int field, int amount). Where the value of the field can be Calendar.DATE, Calendar.MONTH, Calendar.YEAR. So this mean if you want to subtract in days, months or years use Calendar.DATE, Calendar.MONTH or Calendar.YEAR respectively.

In the code above we want to know what is the date back to 30 days ago.

The sample result of the code is shown below:

Filed in: Java Util Package

One Response to "How do I add or subtract a date?"

  1. logesh says:

    there is a error in line number 18 semicolon missing ;

Leave a Reply

Submit Comment

© 2014 Java Examples - Developed with