File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes GregorianCalendar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "GregorianCalendar" Watch "GregorianCalendar" New topic
Author

GregorianCalendar

Navi Lock
Ranch Hand

Joined: Jul 29, 2003
Posts: 39
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

Joined: Dec 04, 2001
Posts: 188
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

Joined: Jul 29, 2003
Posts: 39
Hi Jason, Thanks a bunch. Your explaination helped.

-Navi
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
something like:



java j2ee job interview questions with answers | Learn the core concepts and the key areas
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jul 22, 2000
Posts: 9043
    
  10
Thanks, Jim.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GregorianCalendar
 
Similar Threads
JPanel scroll paint problems
GregorianCalendar
date and time
How to compare time reliably
Adjusting GregorianCalendar to reflect a specific TimeZone