Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Compare just time portion of calendar and date objects

Posted on 2010-11-27
2
Medium Priority
?
525 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

926 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