• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GregorianCalendar

 
Navi Lock
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I am using GregorianCalendar to compare 2 dates to see it they are equal.
I create 1 gc object, set the yyyy , mm , dd then the same for the second gc object.

GregorianCalendar gregorianCalendar1 = new GregorianCalendar();
gregorianCalendar1.set(2006,2,1,0, 0, 0);
step**",
GregorianCalendar gregorianCalendar2 = new GregorianCalendar();
gregorianCalendar2.set(2006,2,1,0, 0, 0);

System.out.println("TIMEAAAA 2 "+gregorianCalendar2.getTimeInMillis());
System.out.println("TIMEAAAAA 1 "+gregorianCalendar1.getTimeInMillis());
System.out.println("COMPARE "+gregorianCalendar1.getTime().equals (gregorianCalendar2.getTime()));
------------------->>>>> THis is ALWAYS TRUE..

BUT if i have a "Delay / for Loop" of more than 250 at "step**",between the creation of first GC object instance and the second GC object, then the result of the comparison is always FALSE...



GregorianCalendar gregorianCalendar1 = new GregorianCalendar();

gregorianCalendar1.set(Calendar.YEAR,2006);
for(int h = 0; h < 20000 ; )
{
h++;
}

GregorianCalendar gregorianCalendar2 = new GregorianCalendar();
gregorianCalendar2.set(Calendar.YEAR,2006);

try {

System.out.println("TIME 2 "+gregorianCalendar2.getTimeInMillis());
System.out.println("TIME 1 "+gregorianCalendar1.getTimeInMillis());
System.out.println("COMPARE "+gregorianCalendar1.getTime().equals(gregorianCalendar2.getTime()));

} catch (IllegalArgumentException iae) {
iae.printStackTrace(System.out);
}

}



DUNNO why..... Can anybody help me out...

Thanks,
Navi.
[ April 01, 2006: Message edited by: Navi Navi ]
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The set method only sets the year, month, date, hour and minute fields of the GregorianCalendar. Therefore seconds, milliseconds will be retained from when you created the object, you need to clear all the time fields first.

Sets the values for the fields year, month, date, hour, and minute. Previous values of other fields are retained. If this is not desired, call clear first.

However it is probably easier to just use the GregorianCalendar constructor.



Jason
 
Navi Lock
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason, Thanks a bunch. Your explaination helped.

-Navi
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A related question:
If I use the default GregorianCalendar constructor, I get the current date and the current time. What is the simplest way to get the current date at the previous midnight? For example, today is April 2, 2006, and it is about 7 pm. But I want today at 12:01 am minus one minute, in other words the beginning of today.
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like:

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget

Depending how you're displaying the result, this may or may not be noticeable, but milliseconds are part of the state of the GregorianCalendar, which probably should be zeroed out with the rest of them. The fields AM_PM and HOUR are redundant, their values implied by HOUR_OF_DAY, so you can safely skip them.

However,

This is misleading - getInstance() is in the Calendar class, and static. In my opinion it's clearer to either use Calendar.getInstance(), or just use new GregorianCalendar().

Should be unnecessary - a newly created Calendar is already at the current time.

This appears to be for some problem other than the one Marilyn described.
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks. It just seems like there should be a simpler way than calling set() four times, but if there isn't, there isn't.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simpler? Sure, but not from Sun. Calendar and GregorianCalendar are among the more API's they've created. You seek Joda:

[ April 03, 2006: Message edited by: Jim Yingst ]
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jim.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic