Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

Calendar in JSP

Hi,
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.
0
dhulipala
Asked:
dhulipala
1 Solution
 
koppchaCommented:
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");
            try{
        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");
                  System.out.println(days);
             }
            catch(Exception e) {
                  System.out.println("This is the exception"+e);
            }
            
                  


      }
}
0
 
jessegivyCommented:
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now