Solved

Compare just time portion of calendar and date objects

Posted on 2010-11-27
2
498 Views
Last Modified: 2012-06-27
In my code I have a date and a calendar object and I want to compare just the time portion. I'm thinking of doing something like this

public myfunc(Calendar cal1, Date date1)
{
   Calendar cal2 = (Calendar)cal1.clone();
   cal2.setTime(date1);
   if (cal2.after(cal1)
     <more code here...>
}

But I'm having trouble with the setTime() function since it also assigns the date info as well. So I need a setTime() that will only set the time.
0
Comment
Question by:opike
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34223835
create a new calendar and just set the time to be same as inputs

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, cal1.get(Calendar.HOUR));
// set minutes and seconds in same way

0
 

Author Comment

by:opike
ID: 34223838
Yeah...I guess I was struggling to hard to be more elegant.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JUnit 4 @Before and @BeforeClass differences 3 60
iterator/ListIterator approach 17 27
runtime exception 2 28
Is there a simpler dropbox system? 10 22
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

809 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