EDIT - Ah, I didn't set the calendars as non-lenient. This means that Java accepts 8/13/2007 as a valid date and translates it as 8/1/2008
Try 8/1/2008,10/4/2007 and you get exactly the same number 274.
The correction is to add 2 lines to the code:
c1.setLenient(false);
and
c2.setLenient(false);
just before their values are set.
Here is the full amended code anyway:
import java.io.*;
import java.util.*;
public class myprog {
public static final long ONE_HOUR = 60 * 60 * 1000L;
public static void main(String args[]){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Calendar c1 = new GregorianCalendar();
Calendar c2 = new GregorianCalendar();
Calendar tmp = new GregorianCalendar();
String[] dates = new String[2];
String[] ymd = new String[3];
try{
System.out.println("\nEnter 2 dates e.g. 5/8/2007,1/8/2007\n");
dates = br.readLine().split(",");
ymd = dates[0].split("/");
c1.setLenient(false);
c1.set( Integer.parseInt(ymd[2]),
Integer.parseInt(ymd[1]),
Integer.parseInt(ymd[0]) , 0 , 0 , 0);
ymd = dates[1].split("/");
c2.setLenient(false);
c2.set( Integer.parseInt(ymd[2]),
Integer.parseInt(ymd[1]),
Integer.parseInt(ymd[0]) , 0 , 0 , 0);
if(c1.after(c2))
{
tmp = c1;
c1 = c2;
c2 = tmp;
}
System.out.println("\nDays between " + dates[0] + " and " + dates[1] + "\n");
System.out.println( (c2.getTime().getTime() - c1.getTime().getTime()) /
(ONE_HOUR * 24));
} catch (Exception e){
e.printStackTrace();
}
}
}
Ok. Call this myprog.java :
myprog.java :
import java.io.*;
import java.util.*;
public class myprog {
public static final long ONE_HOUR = 60 * 60 * 1000L;
public static void main(String args[]){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Calendar c1 = new GregorianCalendar();
Calendar c2 = new GregorianCalendar();
Calendar tmp = new GregorianCalendar();
String[] dates = new String[2];
String[] ymd = new String[3];
try{
System.out.println("\nEnter 2 dates e.g. 5/8/2007,1/8/2007\n");
dates = br.readLine().split(",");
ymd = dates[0].split("/");
c1.set( Integer.parseInt(ymd[2]),
Integer.parseInt(ymd[1]),
Integer.parseInt(ymd[0]) , 0 , 0 , 0);
ymd = dates[1].split("/");
c2.set( Integer.parseInt(ymd[2]),
Integer.parseInt(ymd[1]),
Integer.parseInt(ymd[0]) , 0 , 0 , 0);
if(c1.after(c2))
{
tmp = c1;
c1 = c2;
c2 = tmp;
}
System.out.println("\nDays between " + dates[0] + " and " + dates[1] + "\n");
System.out.println( (c2.getTime().getTime() - c1.getTime().getTime()) /
(ONE_HOUR * 24));
} catch (Exception e){
e.printStackTrace();
}
}
}
You just enter the 2 dates as directed.
Example output:
Enter 2 dates e.g. 5/8/2007,1/8/2007
1/8/2007,5/8/2007
Days between 1/8/2007 and 5/8/2007
4
Enter 2 dates e.g. 5/8/2007,1/8/2007
5/8/2007,5/8/2006
Days between 5/8/2007 and 5/8/2006
365
This will allow dates in any order (the tmp Calendar is for swapping if first date > second date). Works for leap years also.
In Java the Calendar class allows us to subtract 2 dates from each other and then all we have to do is convert into days.