Discussions

General J2EE: Elapsed Date

  1. Elapsed Date (2 messages)

    Hi,
     I want to find out the elapsed date for example, if I have two dates I want to find out the difference between them in days. Can this be done? If yes can anyone send me the code this is really urgent.
    Sarvesh

    Threaded Messages (2)

  2. Elapsed Date[ Go to top ]

    This is a very easy problem. If you have two dates, and you know the date that occurs later, simply subtract the time of the other date from the later date.

    Date now = new Date(System.currentTimeMillis());
    Date later = new Date(System.currentTimeMillis());

    long diff = later.getTime() - now.getTime();

    You can divide 'diff' by the number of milliseconds per day, to get the difference in days, or by the number of milliseconds per hour, to get the differnce in hours, etc, you get the idea.

    Raffi
  3. Elapsed Date[ Go to top ]

    If using the precise difference in milliseconds won't work for you, there are utility methods in the java.util.Calendar class for determine the difference between two dates.