Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Compare just time portion of calendar and date objects

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

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

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

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…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses

705 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