Java examples for java.util:Month
This method returns new date by adding (subtracting) months to the original date.
//package com.java2s; import java.util.Calendar; import java.util.Date; public class Main { public static void main(String[] argv) throws Exception { Date date = new Date(); int monthsToAdd = 2; System.out.println(getDateByAddMonths(date, monthsToAdd)); }/*from w ww . j a v a2 s . c o m*/ /** * This method returns new date by adding (subtracting) months to the original date. * * @param date * base date * @param monthsToAdd * amount of months to add or subtract (negative values) * @return the returned date is normalized */ public static Date getDateByAddMonths(Date date, int monthsToAdd) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 1); calendar.add(Calendar.MONTH, monthsToAdd); return calendar.getTime(); } }