Solved

The Calendar() class, comparing two times?

Posted on 2003-12-11
8
1,828 Views
Last Modified: 2010-03-31
Hi i recently asked on EE which is the best class for time management, the reply i got was Calendar class, i am able to input times and create an instance of this class, however i get totally lost when i need to compare them?

Is there any easy way of comparing two times? For example;

time1 > time2

Or is there a specific method that Calendar class provides for this? If so, how would i go about using it? So far i have had no luck! Any help would be much appreciated. Thank you.

Mr S.M.
0
Comment
Question by:Programmer_to_be
8 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 30 total points
ID: 9922717
The Calendar class has a before and an after method that can be used for comparisons.

public boolean before(Object when);
public boolean after(Object when);
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9925914
Check the Calendar API specifications for more:

http://java.sun.com/j2se/1.4.1/docs/api/java/util/Calendar.html
0
 
LVL 4

Expert Comment

by:tapasvi
ID: 9926153
easiest way to compare time is through getTimeInMillis() method of Calendar class.
simple long subtraction.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 30

Expert Comment

by:Mayank S
ID: 9926185
Performance-wise - No. Because it would result in two method calls rather than one method-call in case of before () or after () or equals ().

Mayank.
0
 
LVL 4

Expert Comment

by:tapasvi
ID: 9926266
Calendar a,b;
//initialize a and b with your time...

a.getTimeInMillis() > b.getTimeInMillis() //compare

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9926344
Yeah, that's what.... it has 2 method calls. One getTimeInMillis () for a, and one for b. Its better to use after () or before() or equals () depending upon the situation.

Mayank.
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9926694
--> a.getTimeInMillis() > b.getTimeInMillis() //compare

Yes that will work, but isn't:
a.after(b);
Just so much more readable!

--> Yeah, that's what.... it has 2 method calls
When you call a.after(b) it will do exactly the same number of calls (in fact one more for the delegation, and another for the cast), but as stated above, it is just a lot easier to read, which is more important than the sub-microsecond improvement in performance.
0
 

Author Comment

by:Programmer_to_be
ID: 9932127
Thanks everyone, all the methods suggested work great thank you.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from 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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

816 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

8 Experts available now in Live!

Get 1:1 Help Now