Calendar in JSP

If I have the date and time in datetime format as Tuesday, April 05,2005 10:00AM and Wednesday, April 06,2005 12:00PM
how can I compute the duration between these two datetimes without the user having to input the duration through GUI.
Thank You.
Who is Participating?
This sample program will help you
import java.text.*;
import java.util.*;
public class DateDiff
      public static void main(String[] args)
            String date1="Tuesday, April 05,2005 10:00AM ";
            String date2="Wednesday, April 06,2005 12:00PM ";            
            SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM dd,yyyy hh:mmaaa");
        Date d1 = sdf.parse(date1);
        Date d2 = sdf.parse(date2);
            long days = (d2.getTime() - d1.getTime())/1000/60/60/24;
            System.out.println("These are the days");
            catch(Exception e) {
                  System.out.println("This is the exception"+e);

Java Dates really piss me off!!!  I am sure that there has to be a supplimentary date class out there to do all of the crap that I have to do on a regular basis.  Anyway....

....there isn't, that I know of, any predetermined way to do easy date math.  there are a few options though:

The getTime function;
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
this is an integer which you can then do simple subtraction with and then calculate out the difference into some kind of workable unit (days, months, years) even seconds would be more helpful than milliseconds.

The other option is a bit more tedious;
If you need to know the number of years, months, days....etc you can use the getYear,getMonth,getDate...etc functions which returns an integer value which can then be used to perform simple math on.  Be careful with the month because for some ungodly reason they decided that january should be 0 instead of 1, so consequently December is represented by 11.  Just add 1 whenever retrieving the integer value for the month.  Day and year are fine.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.