[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


How to find difference in time between two Date objects

Posted on 2005-04-09
Medium Priority
Last Modified: 2010-03-31
Dear java experts ,
           Can somebody help me find the difference in time inthe form of STRING.
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a dd-MMM-yyyy");

Date Time1=sdf.parse(request.getParameter(time1).trim());
Date Time2=sdf.parse(request.getParameter(time2).trim());

I really want time 2 - time 1 in minutes and that too in STRING.

any help will be very highly apprecaiatted.
Question by:Testsubbu
1 Comment
LVL 92

Accepted Solution

objects earned 2000 total points
ID: 13745411
You can get millisend difference using:

long msecs = Time2.getTime() - Time1.getTime();

then divid it to get minutes:

long mins = msecs / (1000 * 60);

To convert to a String something like:

String s = mins+" minutes";

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

873 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