Results 1 to 2 of 2
  1. #1
    white feather is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default Find the time between two dates

    Java Code:
    void age() {
    		boolean isValid = false;
    		Calendar user = null;
    		while (!isValid) {
    			System.out.print("Enter Date (mm/dd/yyyy): ");
    			DateFormat df = new SimpleDateFormat("MM/dd/yy");
    			String s = In.in();
    			Date date = null;
    			try {
    				date = df.parse(s);
    			} catch (ParseException e) {
    				System.out.println("Date error");
    			}
    			user = Calendar.getInstance();
    			user.setTime(date);
    
    			if (user.getTime().getTime() < calendar.getTime().getTime())
    				isValid = true;
    			else
    				System.out.println("Invalid date. Enter again");
    		}
    		int userMonths = user.get(Calendar.MONTH);
    		int userDays = user.get(Calendar.DATE);
    
    		int day = calendar.get(Calendar.DATE);
    		int month = calendar.get(Calendar.MONTH) + 1;
    		hours = calendar.get(Calendar.HOUR_OF_DAY);
    		minutes = calendar.get(Calendar.MINUTE);
    		seconds = calendar.get(Calendar.SECOND);
    
    		Date startDate = calendar.getTime();
    		Date endDate = user.getTime();
    		long l1 = startDate.getTime();
    		long l2 = endDate.getTime();
    		double d = (l1 - l2) * 0.001;
    
    		int[] daysInMonths = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
    				31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
    		double yearz = (d / 31536000);
    		double monthz = ((yearz % 1) * 365) / 30.3167;
    
    		int totalMonths = (int) monthz;
    		int startingMonth = month;
    		int totalDays = 0;
    
    		if (userMonths < startingMonth) {
    
    			if (userDays != day) {
    				for (int i = userMonths + 1; i < startingMonth + 1; i++) {
    					totalDays += daysInMonths[i - 1];
    				}
    			}
    		} else if (userMonths >= startingMonth) {
    			for (int i = 0; i <= ((12 - (userMonths + 1)) + month); i++) {
    				totalDays += daysInMonths[i + userMonths];
    			}
    		}
    		double daysToDivideBy = totalDays / (totalMonths + 1);
    		double dayz = ((monthz % 1) * daysToDivideBy);
    
    		System.out.println("You are: \nYears: " + (int) yearz + "\nMonths: "
    				+ totalMonths + "\nDays: " + (int) dayz + "\nHours: " + hours
    				+ "\nMinutes: " + minutes + "\nSeconds: " + seconds + " old");
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTimeInMillis((long) d);
    		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
    		System.out.println(formatter.format(calendar.getTime()));
    	}
    This is what i have so far. It calculates the year and month correctly but it has a little trouble with the day
    Ex.
    if the user enters Dec. 1, 1990 for their birth date the output should be
    You are 19 years, 4 months, 12 days, 8 hours, 44 minutes, and 39 seconds old.

    Thanks in advance

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    P.S. You already have the answer on Daniweb.

    Also, do not cross-post this way without, at least, posting a link to the other thread(s), and keeping all thread "up to date". No one likes wasting their time making suggestions that have already been made.
    Last edited by masijade; 04-14-2010 at 01:44 PM.

Similar Threads

  1. UTC Dates
    By PedroCosta in forum Advanced Java
    Replies: 3
    Last Post: 04-01-2010, 06:08 PM
  2. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 01:06 AM
  3. Can we find out program excecution Time?
    By makpandian in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 01:30 PM
  4. Help with dates
    By bumblyb33 in forum New To Java
    Replies: 1
    Last Post: 02-13-2009, 02:54 PM
  5. help with dates and time
    By osval in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 12:41 PM

Tags for this Thread

Posting Permissions

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