Solved

Compare just time portion of calendar and date objects

Posted on 2010-11-27
2
512 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
[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
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
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…
Suggested Courses

623 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