Write a java code to get two date strings as input and write code to find the difference between two dates in days. The return type of the output is an integer that returns the difference between two dates in days.
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-12
2012-03-14
Sample Output 1:
2
Sample Input 2:
2012-04-25
2012-04-28
Sample Output 2:
3
Find the Difference between two dates in days in Java
Following are the step to find the difference between the two dates:
- Input two strings from the user using Buffer Reader input stream.
- Pass both the string to dateDifference() method.
- Inside the method, create a SimpleDateFormat using the given pattern(yyyy-mm-dd)
- First, parse the string s1 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
- With the help of the Calender class, get the time in milliseconds and store it in a variable d1.
- Repeat the above 3 steps for the second string also.
- Now, calculate the difference between both the times and return the difference between two dates in days to the user.
import java.awt.image.BufferStrategy; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.text.*; import java.util.*; public class DaysBetween { 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(dateDifference(s1, s2)); } public static int dateDifference(String s1, String s2) throws ParseException { SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); Date d = sd.parse(s1); Calendar c = Calendar.getInstance(); c.setTime(d); long d1 = c.getTimeInMillis(); d = sd.parse(s2); c.setTime(d); long d2 = c.getTimeInMillis(); int n = Math.abs((int) ((d1 - d2) / (1000 * 3600 * 24))); return n; } }
Output
Java Get all Days 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 days.
Refer sample output for formatting specifications
Sample Input:
2012-03-12
2012-03-14
Sample Output:
[Mon Mar 12 00:00:00 IST 2012, Tue Mar 13 00:00:00 IST 2012]
Program to list all days between two dates
Following are the steps to get all the days between two dates:
- Input string from the user using BufferReader Input stream.
- Pass both the string to dateDifference() method.
- Inside the method, create a SimpleDateFormat using the given pattern(yyyy-mm-dd ) and create an empty array list to hold dates.
- Pass String s1 from the beginning of the string to produce a date.
- Now, get the instance of the Calender class set the time with the given date.
- Pass string s2 also to produce a date by using the parse() method.
- Add days to the array list while iterating over the current calendar(i.e now) object by fetching time.
- At last, return the list of dates.
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.*; public class DaysBetween { 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(dateDifference(s1, s2)); } public static List<Date> dateDifference(String s1, String s2) throws ParseException, ParseException { List<Date> list = new ArrayList<>(); SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); Date d = sd.parse(s1); Calendar c = Calendar.getInstance(); c.setTime(d); Date d2 = sd.parse(s2); while(c.getTime().before(d2)){ Date result = c.getTime(); list.add(result); c.add(Calendar.DATE,1); } return list; } }
Output
Thus, in this way, we learn how to find the difference between two dates in days in Java.