How to calculate months between two dates in java using Joda ?

 

How to calculate months between two dates in java using Joda ?

Joda Jar is the one of the opensource jar in java for date related validations and functionalities. Finding the months between the two dates has been a challenging task if we try to do only with Java.

Same requirement can be done very easily using the Joda Jar.

Months Between two dates using Joda Jar:

package in.javadomain;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.joda.time.Period;

public class MonthsBetweenTwoDates {
public static void main(String[] args) {
MonthsBetweenTwoDates testLogics = new MonthsBetweenTwoDates();
try {
// input is MM/dd/yyyy format
System.out.println(testLogics.constructTags("09/06/2015","02/01/2016"));
} catch (ParseException e) {
e.getMessage();
}
}



public String constructTags(String startDateinString, String expiryDateinString) throws ParseException {
String tagNames = "";
String tagNmsShop = "";
String returnString = "";

// Ensure you are giving setLenient(false); which checks the correct format or throw parse exception

// converting the passed string to date format
DateFormat format;
Date expiryDateinDate,startDateinDate;
if (expiryDateinString.contains("/")) {
format = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
format.setLenient(false);
expiryDateinDate = format.parse(expiryDateinString);
} else {
format = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
format.setLenient(false);
expiryDateinDate = format.parse(expiryDateinString);
}


if(startDateinString.contains("/")){
format = new SimpleDateFormat("MM/dd/yyyy",Locale.ENGLISH);
startDateinDate = format.parse(startDateinString);
format.setLenient(false);
}else {
format = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
startDateinDate = format.parse(startDateinString);
format.setLenient(false);
}


org.joda.time.LocalDate date2 = new org.joda.time.LocalDate(expiryDateinDate);
org.joda.time.LocalDate date1 = new org.joda.time.LocalDate(startDateinDate);

while (date1.isBefore(date2) || date1.isEqual(date2) || (date1.getMonthOfYear() <= date2.getMonthOfYear() && (date1.getYear() <= date2.getYear()))) {
if (tagNames.isEmpty() && tagNmsShop.isEmpty()) {
tagNames = date1.toString("MMMM yyyy");
} else {
tagNames = tagNames + "," + date1.toString("MMMM yyyy");
}
date1 = date1.plus(Period.months(1));
}
returnString = tagNames;
if (returnString.contains(",") && returnString.length() == 1) {
return "";
} else {
return returnString;
}
}
}

Output:

September 2015,October 2015,November 2015,December 2015,January 2016,February 2016

Note: Ensure you have added the Joda Jar to your classpath.

Below program may help to understand joda jar a bit:

package in.javadomain;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class JodaJar {

public static void main(String[] args) {
try {
String date = "29-11-2015";
SimpleDateFormat sd = new SimpleDateFormat("dd-MM-yyyy");
Date todayDate = sd.parse(date);
org.joda.time.LocalDate date1 = new org.joda.time.LocalDate(todayDate);
// prints 2015-11-03
System.out.println(date1.withDayOfMonth(3));

// prints feb 1, because feb 1 is the 32nd day of the year
System.out.println(date1.withDayOfYear(32));

/* to get the day of the year - starts*/
// prints 333
System.out.println("Getting the current year : " + date1.dayOfYear().get());

// 333
System.out.println("Day of the year : " + date1.getDayOfYear());
/* to get the day of the year - ends*/


// prints 29
System.out.println("Getting the current day : " + date1.dayOfMonth().get());
// prints 29
System.out.println("Day of the month : " + date1.getDayOfMonth());

} catch (ParseException e) {
}

}
}

Output:


2015-11-03
2015-02-01
Getting the current year : 333
Day of the year : 333
Getting the current day : 29
Day of the month : 29

645 total views, 3 views today