Solved

The Calendar() class, comparing two times?

Posted on 2003-12-11
8
1,813 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:mayankeagle
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
 
LVL 30

Expert Comment

by:mayankeagle
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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:mayankeagle
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 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

19 Experts available now in Live!

Get 1:1 Help Now