Results 1 to 5 of 5
  1. #1
    Mick is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default 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:

    Java 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

  2. #2
    Mick is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Anybody?

    I don't expect the full sollution only advices what should I do now.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    1. getTimeInMillis() returns a long value. Why are you assigning these to float variables? Make them longs.
    2. ditto for the difference variable.
    3. Learn to format and indent code for better readability.
    Code Conventions for the Java(TM) Programming Language: Contents

    db

  4. #4
    Mick is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    I made them longs now. Sorry for the format and the readability, I made c/p from Eclipse and that's why the code is so ugly.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Mick View Post
    I made them longs now. Sorry for the format and the readability, I made c/p from Eclipse and that's why the code is so ugly.
    The difference between two timestamps can't be expressed by a combination of years, months and days; e.g. is a length of 31 days equal to one month? Which month? Or is it equal to one month plus one day or is it equal to February plus three days (not a leap year)?

    kind regards,

    Jos

Similar Threads

  1. sql date problem
    By realosso in forum New To Java
    Replies: 3
    Last Post: 06-04-2010, 04:42 AM
  2. Calendar date problem
    By groobelar in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 03:47 AM
  3. date problem
    By karlkwanny in forum JDBC
    Replies: 1
    Last Post: 07-30-2009, 10:50 AM
  4. Difference between current date and anothe date
    By vijay balusamy in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 04:15 PM
  5. problem with date class
    By gabriel in forum New To Java
    Replies: 3
    Last Post: 08-03-2007, 01:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •