Given a method with two date strings in “yyyy-mm-dd” format as input. Write code to find the difference between two dates in months. The return type of the output is an integer that returns the difference between two dates in months.
Input and Output Format
- Input consists of two date strings.
- The format of date: yyyy-mm-dd.
- The output is an integer.
Refer sample output for formatting specifications
Sample Input 1:
2012-03-01
2012-04-16
Sample Output 1:
1
Sample Input 2:
2011-03-01
2012-04-16
Sample Output 2:
13
Find the difference between Dates in months in Java
Following are the steps to find the difference between dates in month:
- Input string from the user using BufferReader Input stream.
- Pass both the string to monthsBetweenDates() method.
- Inside the method, create a SimpleDateFormat using the given pattern(yyyy-mm-dd).
- Parse both the string from the beginning of the given string to produce a date.
- Now, get the instance of Calendar class and set the time with the given date
- Then, get the current month and year of the given calendar field and once again set the time with the given date.
- Repeat the above step for the second date.
- Now, calculate the difference between them and return the months to the user.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.text.*; import java.util.*; public class Main { public static void main(String[] args) throws ParseException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s1 = br.readLine(); String s2 = br.readLine(); System.out.println(monthsBetweenDates(s1,s2)); } public static int monthsBetweenDates(String s1, String s2) throws ParseException { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date d1=sdf.parse(s1); Date d2=sdf.parse(s2); Calendar cal=Calendar.getInstance(); cal.setTime(d1); int months1=cal.get(Calendar.MONTH); int year1=cal.get(Calendar.YEAR); cal.setTime(d2); int months2=cal.get(Calendar.MONTH); int year2=cal.get(Calendar.YEAR); int n=((year2-year1)*12)+(months2-months1); return n; } }
Output
Java Get all months between two dates
Input and Output Format
- Input consists of two date strings.
- The format of date: yyyy-mm-dd.
- The output is the list of months.
Refer sample output for formatting specifications
Sample Input 1:
1994-02-10
1994-08-05
Sample Output 1:
[1994-02-01, 1994-03-01, 1994-04-01, 1994-05-01, 1994-06-01, 1994-07-01, 1994-08-01]
Program to list all months between two dates
Following are the steps to get all the months between two dates:
- Input string from the user using BufferReader Input stream.
- Pass both the string to monthsBetweenDates() method.
- Inside the method, create a SimpleDateFormat using the given pattern(yyyy-mm-dd ) and create an empty array list to hold months.
- Parse both the string from the beginning of the given string to produce a date.
- Now, get the instance of Calendar class and set the time with the given date also set the value for the current field for YEAR, MONTH, and DAY_OF_MONTH.
- Repeat the above step for the second date.
- Now, set the instance of Calendar class cal1 to the current instance (i.e now).
- Add months to the array list while iterating over the current calendar(i.e now) object.
- At last, return the array list.
package com.demo3; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; public class MonthsBetween2 { public static void main(String[] args) throws IOException, ParseException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s1 = br.readLine(); String s2 = br.readLine(); System.out.println(monthsBetweenDates(s1, s2)); } public static ArrayList<String> monthsBetweenDates(String s1, String s2) throws ParseException { ArrayList<String> n = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = sdf.parse(s1); Date d2 = sdf.parse(s2); Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.setTime(d1); cal1.set(cal1.get(Calendar.YEAR),cal1.get(Calendar.MONTH),1); cal2.setTime(d2); cal2.set(cal2.get(Calendar.YEAR),cal2.get(Calendar.MONTH),2); Calendar now = cal1; while(now.before(cal2)){ n.add(sdf.format(now.getTime())); now.add(Calendar.MONTH,1); } return n; } }
Output
Thus, in this way, we can find the difference between months on a given date in Java. Along with this, we learn how to get a list of all months between given dates.