Solved

The Calendar() class, comparing two times?

Posted on 2003-12-11
8
1,845 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 96
Eclipse with various Java releases 7 85
jmeter usage 4 61
Hibernate Inheritance Strategy for Abstract Base class and two concrete classes 3 38
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

732 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