How to find number of months between two given dates in Java?
Problem Description :
Write a program in Java that prints number of months between two given dates.
Concept :
For finding number of months between two given dates we are using below algorithm.
- Convert both given dates to Calendar.
- Get number of years between two dates using Calendar class method public int get(int field) and constant YEAR.
- Total number of months = 12 * number of years + (number of months in two calendar dates using Calendar class method public int get(int field) and constant MONTH).
- Finally get number of remaining days using Calendar class method public int get(int field) and Constant DAY_OF_MONTH.
Recommended :
Java Program :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.datetime; | |
import java.text.DateFormat; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Scanner; | |
/** | |
* | |
* @author Rohit Agarwal | |
* @category Date and Time | |
* @problem Number of months between two given dates | |
* | |
*/ | |
public class MonthsBetweenTwoDates { | |
public static void main(String[] args) { | |
Scanner input = null; | |
try { | |
input = new Scanner(System.in); | |
System.out.println("Enter first date in dd/MM/yyyy format :"); | |
String first = input.next(); | |
System.out.println("Enter second date in dd/MM/yyyy format :"); | |
String second = input.next(); | |
// If Date is valid, converting String to date. | |
Date mydate1 = getValidDate(first); | |
Date mydate2 = getValidDate(second); | |
if (mydate1 != null && mydate2 != null) { | |
// Creating Calendar class instance | |
Calendar calendar1 = Calendar.getInstance(); | |
Calendar calendar2 = Calendar.getInstance(); | |
// Converting Date to Calendar | |
calendar1.setTime(mydate1); | |
calendar2.setTime(mydate2); | |
int diffYear = Math.abs(calendar1.get(Calendar.YEAR) - calendar2.get(Calendar.YEAR)); | |
int diffMonth = diffYear * 12 + Math.abs(calendar1.get(Calendar.MONTH) - calendar2.get(Calendar.MONTH)); | |
int diffdays = Math.abs(calendar1.get(Calendar.DAY_OF_MONTH) - calendar2.get(Calendar.DAY_OF_MONTH)); | |
System.out.println(diffMonth + " Months " + diffdays + " Days"); | |
} else { | |
System.out.println("Dates are invalid."); | |
} | |
} finally { | |
if (input != null) { | |
input.close(); | |
} | |
} | |
} | |
private static Date getValidDate(String date) { | |
Date mydate = null; | |
if (isValidDateFormat(date)) { | |
/* | |
* d -> Day of month | |
* M -> Month of year | |
* y -> Year | |
*/ | |
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); | |
/* | |
* By default setLenient() is true. We should make it false for | |
* strict date validations. | |
* | |
* If setLenient() is true - It accepts all dates. If setLenient() | |
* is false - It accepts only valid dates. | |
*/ | |
dateFormat.setLenient(false); | |
try { | |
mydate = dateFormat.parse(date); | |
} catch (ParseException e) { | |
mydate = null; | |
} | |
} | |
return mydate; | |
} | |
private static boolean isValidDateFormat(String date) { | |
/* | |
* Regular Expression that matches String with format dd/MM/yyyy. | |
* dd -> 01-31 | |
* MM -> 01-12 | |
* yyyy -> 4 digit number | |
*/ | |
String pattern = "(0?[1-9]|[12][0-9]|3[01])\\/(0?[1-9]|1[0-2])\\/([0-9]{4})"; | |
boolean result = false; | |
if (date.matches(pattern)) { | |
result = true; | |
} | |
return result; | |
} | |
} |
Output :
![]() |
Output - How to find number of months between two given dates in Java? |
Note : You can also use this calculator for validating your result.
References :
Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.
Tags : Date and Time problems, Number of months between two dates in Java, Solution in Java, DateFormat, SimpleDateFormat, String, Regular expressions, if else statement.
How to find number of months between two given dates in Java?
Reviewed by Rohit Agarwal
on
12/11/2016
Rating:

No comments:
Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.