Adding days, hours, month or years to dates is a common task in Java. java.util.Calendar can be used to perform Date and Time arithmetic in Java. Calendar class not only provides date manipulation but it also support time manipulation i.e. you can add, subtract hours, minutes and seconds from current time. Calendar class automatically handles date transition or month transition for example if you ask date after 30 days it will return you date based on whether current month is 30 or 31 days long. Same is true in case of adding and subtracting years, Calendar takes care whether current or following year is a leap year or not. For example 2012 is a leap year and it has February with 29 days, if you ask Calendar day before 365 it will return 24th July (assuming current date 23rd July) which shows it take care of leap year. By the way there are couple of more date and time related articles e.g. How to find current date and time in Java and How to convert Date to String in Java. If you haven’t read them already, It’s worth checking to know more about Date and Time in Java.
How to add or subtract days, month and year from date in Java
Calendar.DATE field can be used to add or subtract dates in Java. Positive value passed into add() method will add days into date while negative values will subtract days from date in Java. Similarly Calendar.MONTH can be used to add and subtract months from date in Java. You can use this to get date after 2 months or before 2 months. Just keep in mind that Calendar.MONTH start from zero. Same add() method can be used to both add or subtract months in Java. Once again difference will only be on whether value is positive or negative. Calendar.YEAR can be used to add or subtract year from current date in the same fashion we added days and month into date.
How to add or subtract hour, minutes and seconds from Time in Java
Calendar class not only provides Date information but also gives Time related information. You can get hours, minutes and seconds from java.util.Calendar instance. Similarly you can use same add() method for adding or subtracting hours, minutes and seconds from current time in Java. Just need to careful whether you are using Calendar.HOUR or Calendar.HOUR_OF_DAY because former represent time in AM and PM while later represent time in 24 hours time. Calendar.MINUTES can be used for adding or subtracting minutes from Date.
Important points related to Calendar class in Java
1) Calendar has overloaded getInstance() method which can return Calendar instance in either default timezone and locale or specified timezone or locale.
2) Calendar has different fields to return specifics from Calendar like Calendar.DATE, Calendar.MONTH, Calendar.YEAR etc you can check Javadoc for full list.
3) Calendar.MONTH return zero for first month, keep in mind while using value returned by Calendar.get(Calendar.MONTH)
4) Calendar.HOUR_OF_DAY represent time in 24 hours format. Calendar also support AM or PM format.
That’s all on How to add days, month and year on Date in Java. We have also seen how to add hours, minutes and seconds into time using java.util.Calendar class. Two points which is worth remembering is that month starts from zero and time can be represented in either 24 hours format or AM-PM format.
Other Date and Time tutorials from Javarevisited Blog