CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2010
    Posts
    1

    Finding the time between two Dates

    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
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Finding the time between two Dates

    So what part of that exactly is causing you difficulty?

    Judge a man by his questions, rather than his answers...
    Voltaire
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Finding the time between two Dates

    Have you considered using Joda Time which is much better at manipulating dates than the calendar class.

    BTW: please use code tags when posting code.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Mar 2010
    Posts
    74

    Re: Finding the time between two Dates

    Quote Originally Posted by white feather View Post
    ..................
    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
    If you dont ;like 8 hours, 44 minutes, and 39 seconds old.
    , dont print this information.

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
  •  





Click Here to Expand Forum to Full Width

Featured