Problem with date difference
I have a problem with date difference in Java. I saw that there are already threads about that so I apologize for opening this thread.
I have to write a program who accepts two dates from a user in a way that the user has
to give:
1. day of the first date
2. month of the first day
3. year of the first
and the same for the second date. The result (date difference) has to be in days, months and years depending of users choice. If he wants months then it will be months.
Here is the code:
Code:
import java.util.Scanner;
import java.util.Formatter;
import java.lang.Math;
import java.util.Calendar;
public class My_date {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
Scanner ent = new Scanner(System.in);
Formatter fmt = new Formatter();
int choice;
int firstDay;
int firstMonth;
int firstYear;
int secondDay;
int secondMonth;
int secondYear;
float differenceDays;
float differenceMonths;
float differenceYears;
System.out.print("Enter the day of the first date: ");
firstDay = ent.nextInt();
System.out.print("Enter the month of the first date: ");
firstMonth = ent.nextInt();
System.out.print("Enter the year of the first date: ");
firstYear = ent.nextInt();
cal1.set(firstYear, firstMonth, firstDay);
System.out.print("Enter the day of the second date: ");
secondDay = ent.nextInt();
System.out.print("Enter the month of the second date: ");
secondMonth = ent.nextInt();
System.out.print("Enter the year of the second date: ");
secondYear = ent.nextInt();
cal2.set(secondYear, secondMonth, secondDay);
float milisek1 = cal1.getTimeInMillis();
float milisek2 = cal2.getTimeInMillis();
float difference = milisek2 - milisek1;
System.out.print("In which unit do you want the difference? (1 – days 2 – months 3 – years): ");
choice = ent.nextInt();
if (choice == 1) {
differenceDays = difference / (1000l * 60 * 60 * 24);
fmt.format("%.4f", differenceDays);
System.out.println("Difference between " + firstDay + "." + firstMonth + "." + firstYear + "." + " and " + secondDay + "." + secondMonth + "." + secondYear + "." + " is " + fmt + " days.");
}
else if (choice == 2) {
differenceMonths = difference / (1000l * 60 * 60 * 24 * 31);
fmt.format("%.4f", differenceMonths);
System.out.println("Difference between " + firstDay + "." + firstMonth + "." + firstYear + "." + " and " + secondDay + "." + secondMonth + "." + secondYear + "." + " is " + fmt + " months.");
}
else if (choice == 3) {
differenceYears = difference / (1000l * 60 * 60 * 24 * 365);
fmt.format("%.4f", differenceYears);
System.out.println("Difference between " + firstDay + "." + firstMonth + "." + firstYear + "." + " and " + secondDay + "." + secondMonth + "." + secondYear + "." + " is " + fmt + " years.");
}
else{
System.out.println("Wrong choice!");
}
}
}
The problem is with the month difference and the year difference because months can have 28, 29, 30 or 31 days and a year can have 365 or 366. I don't get the correct result. The day difference seems also suspicious because i get for example 5.9.2010. and 6.9.2010. the difference 1,0012 shouldn't it be only 1?
Please help me with some advice or hints.
Thanks in advance!
Mick