my dog learned polymorphism*
The moose likes Beginning Java and the fly likes basic calendar questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "basic calendar questions" Watch "basic calendar questions" New topic
Author

basic calendar questions

Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
Hey Java Gurus!

I am trying to set a range of dates using Calendar (i.e. 09-20-09 to 09-27-09). I've looked over the API but have still have a few questions.

How do you set the start and end date in Calendar?
How would you compare a Date object to see if it is contained in a Calendar's range?

Any generic examples or links to articles would be greatly appreciated.

As always, thanks for your valuable time.

Just Another Guy Hooked on Java
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Hey,

you do not use a single calendar object to declare a time range.

For that you would use two Calendar instances.



You can then use the calendars before(), after() and compare() methods to compare to another Calendar or Date object.


JDBCSupport - An easy to use, light-weight JDBC framework -
Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
Thanks for clarifying how to set a range with Date or Calendar.

Two follow up questions...

When you use the equals() method on a Date object the API states that it "Compares two dates for equality." and returns a boolean. Does that mean that it compares "Date Object A" to "Date Object B" and if they are the same date (year, month, day, hours, etc), it returns true?

I want to take two strings, convert them to Date objects using SimpleDateFormat and compare them to a date range. If I can use before(), after() and compare() with a Date object, is there any reason I would want to convert them to a Calendar object? (I only ask because several other programmers suggested using Calendar).

Thanks again for all your help!
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
As for the equals question, yes it will return true if Calendar a and Calendar b are meaningfully equivalent.

You could use the Date's before, after, etc. methods. However, they have all been deprecated and the use of the Calendar class is encouraged instead.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

java.util.Date's equals method checks for instance equality - not just the day, but the same hour, minute, second and millisecond as well. Although java.sql.Date ignores the time part most of the time, it doesn't when using equals.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
I appreciate your help very much.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Adam

You can also unzip the source of the jdk and see what is there in java.util.Date#equals() method.

for Jdk 1.6 I see,
public boolean equals(Object obj) {
return obj instanceof Date && getTime() == ((Date) obj).getTime();
}

So it doesn't compare day, month, year, hour etc one by one for two date objects. It just compares the 'time' value which is 'long' representation of the date. This way the comparison becomes easier and faster.

Regards
Maulin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38478
    
  23
It is unnecessary to compare day, month, year etc., because those values are all calculated from the long value called time.
Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
As always, many thanks to all
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: basic calendar questions