/*
File name : LL.java
Date : 27 March 2013
Author : Shivam Tiwari
Organization : http://mycodedock.blogspot.in/
Description : Calculating number of days between two dates in java
*/
//import the Calendar and Date library
import java.util.Calendar;
//import scanner library to get inputs
import java.util.Scanner;
//this is the main class
public class Main {
//this is the main function
public static void main
(String[] args
) {
//create a new Scanner object
Scanner ScannerObj
= new Scanner
(System.
in);
//create two calendar instance
//integer variables to hold input from user
int FirstDateInputYear, FirstDateInputMonth, FirstDateInputDay, SecondDateInputYear, SecondDateInputMonth, SecondDateInputDay;
//get input from user
System.
out.
println("Enter first date's year STRICTLY in YYYY syntax -"); FirstDateInputYear = ScannerObj.nextInt();
System.
out.
println("Enter first date's month STRICTLY in MM syntax -"); FirstDateInputMonth = ScannerObj.nextInt();
System.
out.
println("Enter first date's day STRICTLY in DD syntax -"); FirstDateInputDay = ScannerObj.nextInt();
System.
out.
println("Enter second date's year STRICTLY in YYYY syntax -"); SecondDateInputYear = ScannerObj.nextInt();
System.
out.
println("Enter second date's month STRICTLY in MM syntax -"); SecondDateInputMonth = ScannerObj.nextInt();
System.
out.
println("Enter second date's day STRICTLY in DD syntax -"); SecondDateInputDay = ScannerObj.nextInt();
//set these dates in calendar instances
FirstDate.set(FirstDateInputYear, FirstDateInputMonth, FirstDateInputDay);
SecondDate.set(SecondDateInputYear, SecondDateInputMonth, SecondDateInputDay);
//get the days in between
int DaysInBetween = daysInBetween(FirstDate.getTimeInMillis(),SecondDate.getTimeInMillis());
//print result
System.
out.
println("The number of days between " + FirstDateInputYear
+ " " + FirstDateInputMonth
+ " " + FirstDateInputDay
+ " and " + SecondDateInputYear
+ " " + SecondDateInputMonth
+ " " + SecondDateInputDay
+ " are " + DaysInBetween
+ "."); }
//making the daysBetweenTwoDates function
public static int daysInBetween(long f, long s){
//24 hours in a day
//60 minutes in each hour
//60 seconds in each minute
//1000 milliseconds in each second
return (int)((s - f)/(1000*60*60*24));
}
}