Calendar in JSP

Posted on 2005-04-05
Last Modified: 2010-04-01
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.
Question by:dhulipala
    LVL 8

    Accepted Solution

    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);

    LVL 12

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    Great sound, comfort and fit, excellent build quality, versatility, compatibility. These are just some of the many reasons for choosing a headset from Sennheiser.
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now