calendar get the time with a difference of the GMT

A

aquafresh3

Hello,

I have read a lot of post and java tutorial and API about Calendar but
It seems I missed something.

I program a kind of cleaner scheduler in setting specific delay
between the clean.

For that I have got a DB in which I store the delay between each
clean.

First I calculate the new date of the cleaning by getting an instance
of the Calendar object and adding to it the delay like this:

nextClean = Calendar.getInstance();nextClean.add(Calendar.SECOND,
dbClean.getDelay());

And I store this value inside a Hashtable.

Then in a thread I scan the time until I reach the cleaning date like
in the following code:

while (running) {if (cleaningRequestorCollection.isEmpty()) {try {//
Checks each clean time against the current timeHashtable cleanTimes =
cleaningCalculator.getCleanTimes();synchronized (cleanTimes)
{Enumeration enum = cleanTimes.keys();Calendar current =
Calendar.getInstance(); while (enum.hasMoreElements()) {
Integer keyInt = (Integer) enum.nextElement(); int key =
keyInt.intValue(); Calendar calendar = (Calendar)
cleanTimes.get(keyInt); DBCleaningTypeImpl dbClean =
(DBCleaningTypeImpl) dbCleanings.get(keyInt); } // A cleaning
request message has to be sent if (current.after(calendar)) {
// New CleaningRequestor thread CleaningRequestor
cleaningRequestor = new CleaningRequestor(this, cleaningCalculator,
dbClean, clientConnection, cleanTopicName, adminUpdateTopicName);
cleaningRequestorCollection.put(new Integer(dbClean.getID()),
cleaningRequestor); cleaningRequestor.start(); } } } //
waits 5 seconds sleep(Labels.CLEANER_CLEANSCAN_INTERVAL); } catch
(InterruptedException ie) { Logger.logActivity(Labels.LOG_CLEANER
+ " CleaningScan thread interupted"); } }}


I gather the object record in the Hashtable and I obtain the date
(Calendar) of my cleaning date.

But the results show me a difference between the date registered first
in the Hashtable and the date gather in a second time from this
Hashtable.

A concrete exemple is:
The date is Thursday Feb 26 14:10:00
I choose an interval between the clean of 40 second so each 40 second
a cleaning is done
I store the cleaning date value : Thursday Feb 26 14:10:40
(so the next clean is at Thursday Feb 26 14:10:40)

But when I get the value in my thread from the Hastable thanks to this
line:
Calendar calendar = (Calendar) cleanTimes.get(keyInt);

I get the time Thursday Feb 26 15:10:40 so one hour more

As I am in GMT+1 I suppose that the reason and if I put my time to GMT
+-X I get a difference of +-X

So Is it normal if I get a Calendar.getInstance() I have a good time
but if I gather a value from my Hashtable then I get the time plus the
value of the GMT X?

Thank for any help
 
P

P.Hill

aquafresh3 said:
But when I get the value in my thread from the Hastable thanks to this
line:
Calendar calendar = (Calendar) cleanTimes.get(keyInt);

I get the time Thursday Feb 26 15:10:40 so one hour more

As I am in GMT+1 I suppose that the reason and if I put my time to GMT
+-X I get a difference of +-X

So Is it normal if I get a Calendar.getInstance() I have a good time
but if I gather a value from my Hashtable then I get the time plus the
value of the GMT X?

My guess is that you are misunderstanding what class knows about the
timezone. When you to Date().toString() you get a TZ aware string, not
because the date object is aware of a timezone, but because
Date gets a SimpleDateFormat which uses the default calendar
which uses the default timezone ... to create a String.

I would suggest you look at the binary value of each Date object
myDate.getTime() when you stick it in and when you pull it out
and see if they really changed.

But then maybe I don't quit have your problem understood.
-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top