Solved

The Calendar() class, comparing two times?

Posted on 2003-12-11
8
1,842 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 43
ejb mdb examples 1 19
Why doesn't this text field show up on my Applet frame? 2 28
Selenium docs api java index 3 64
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

735 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