Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basic calendar questions

 
Adam Confino
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adam Confino
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adam Confino
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate your help very much.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48646
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As always, many thanks to all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic