-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateDifference.java
More file actions
42 lines (38 loc) · 1.45 KB
/
Copy pathDateDifference.java
File metadata and controls
42 lines (38 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package basic;
import java.text.*;
import java.util.*;
class DateDifference {
public static void main(String[] args) throws Exception {
Scanner ob = new Scanner(System.in);
System.out.println("Enter dates in format 'dd/mm/yyyy'");
System.out.print("Date 1:");
String sdate1 = ob.nextLine();
System.out.print("Date 2:");
String sdate2 = ob.nextLine();
if (isValidDate(sdate1) == false && isValidDate(sdate2) == false) {
System.out.println("Invalid Input,Enter Valid Dates");
System.exit(0);
}
//Converting String Dates into java Date format
Date date1 = new SimpleDateFormat("dd/MM/yyyy").parse(sdate1);
Date date2 = new SimpleDateFormat("dd/MM/yyyy").parse(sdate2);
long diff = daysBetween(date1, date2);
System.out.println("Difference=" + diff + " days");
}
public static long daysBetween(Date one, Date two)//Method to find date difference
{
//Converting dates into millisecond and divide it by millisecond in a day
long difference = (one.getTime() - two.getTime()) / 86400000;
return Math.abs(difference);
}
public static boolean isValidDate(String d)//Method to check Validity of dates
{
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
try {
df.parse(d);
return true;
} catch (ParseException e) {
return false;
}
}
}