Opposite of subtracting months from a java date , this example will show how to add months to a date using java Calendar.add, java 8 date time api , joda’s DateTime.plusMonths and apache commons DateUtils.addMonths. Assuming players get a vacation after the super bowl, we will set the date which it occurred and then add one month to represent a vacation of sipping fruity drinks in Mexico.
Straight up Java @Test
public void add_months_to_date_in_java () {
Calendar superBowlXLV = Calendar . getInstance ();
superBowlXLV . set ( 2011 , 1 , 6 , 0 , 0 );
Calendar sippinFruityDrinksInMexico = Calendar . getInstance ();
sippinFruityDrinksInMexico . setTimeInMillis ( superBowlXLV . getTimeInMillis ());
sippinFruityDrinksInMexico . add ( Calendar . MONTH , 1 );
SimpleDateFormat dateFormatter = new SimpleDateFormat ( "MM/dd/yyyy HH:mm:ss z" );
logger . info ( dateFormatter . format ( superBowlXLV . getTime ()));
logger . info ( dateFormatter . format ( sippinFruityDrinksInMexico . getTime ()));
assertTrue ( sippinFruityDrinksInMexico . after ( superBowlXLV ));
}
Output
02/06/2011 00:00:49 CST
03/06/2011 00:00:49 CST
Java 8 Date and Time API Java 8 LocalDateTime.plusMonths will return a copy of the LocalDateTime with the specified number of months added.
@Test
public void add_months_to_date_in_java8 () {
LocalDateTime superBowlXLV = LocalDateTime . of ( 2011 , Month . FEBRUARY , 6 ,
0 , 0 );
LocalDateTime sippinFruityDrinksInMexico = superBowlXLV . plusMonths ( 1 );
java . time . format . DateTimeFormatter formatter = java . time . format . DateTimeFormatter
. ofPattern ( "MM/dd/yyyy HH:mm:ss S" );
logger . info ( superBowlXLV . format ( formatter ));
logger . info ( sippinFruityDrinksInMexico . format ( formatter ));
assertTrue ( sippinFruityDrinksInMexico . isAfter ( superBowlXLV ));
}
Output
02/06/2011 00:00:00 0
03/06/2011 00:00:00 0
Joda Time Joda DateTime.plusMonths will return a copy the DateTime plus the specified number of months.
@Test
public void add_months_to_date_in_java_joda () {
DateTime superBowlXLV = new DateTime ( 2011 , 2 , 6 , 0 , 0 , 0 , 0 );
DateTime sippinFruityDrinksInMexico = superBowlXLV . plusMonths ( 1 );
DateTimeFormatter fmt = DateTimeFormat . forPattern ( "MM/dd/yyyy HH:mm:ss z" );
logger . info ( superBowlXLV . toString ( fmt ));
logger . info ( sippinFruityDrinksInMexico . toString ( fmt ));
assertTrue ( sippinFruityDrinksInMexico . isAfter ( superBowlXLV ));
}
Output
02/06/2011 00:00:00 CST
03/06/2011 00:00:00 CST
Apache Commons Apache commons DateUtils.addMonths will adds a number of months to the date returning a new object.
@Test
public void add_months_to_date_in_java_apachecommons () {
Calendar superBowlXLV = Calendar . getInstance ();
superBowlXLV . set ( 2011 , 1 , 6 , 0 , 0 );
Date sippinFruityDrinksInMexico = DateUtils . addMonths ( superBowlXLV . getTime (), 1 );
SimpleDateFormat dateFormatter = new SimpleDateFormat ( "MM/dd/yyyy HH:mm:ss z" );
logger . info ( dateFormatter . format ( superBowlXLV . getTime ()));
logger . info ( dateFormatter . format ( sippinFruityDrinksInMexico ));
assertTrue ( sippinFruityDrinksInMexico . after ( superBowlXLV . getTime ()));
}
Output
02/06/2011 00:00:11 CST
03/06/2011 00:00:11 CST
Add months to date posted by Justin Musgrove on 02 February 2014
Tagged: java, java-date, and java-date-math
Share on: Facebook Google+
All the code on this page is available on github:
DatePlusMonths.java