Time-problem
Hi,
I want to set a countdown. Therefore i substract the long-value of the goaldate with the the value of the count-start-date.
But the problem is that the new Date-object is one hour to high.
For example 17:00 - 15:00 = 3:00
Any suggestions?
regards.
Message was edited by:
Olek
[330 byte] By [
Oleka] at [2007-11-27 6:58:12]

A date is not a time duration. You're probably crossing a DST boudary or something.If you want to do start time minus duration in hours equals end time then create a Calendar, use add(Calendar.HOUR, -duration).
jverda at 2007-7-12 18:48:48 >

Thx Thats a really easy way to implement that.Hm ... but there is an other problem.I want the difference of two date-Objects.If the timer-starttime is past, the countown should start at the remaining time.Message was edited by: Olek
Oleka at 2007-7-12 18:48:48 >

Do you mean you just want to know if now is past some start time? Date has before and after methods.Also, if you want to start something at a certain time, look into java.util.Timer and TimerTask.
jverda at 2007-7-12 18:48:48 >

No i need the remaining time of a goaltime and the current time.
The counter should start counting down at 12:30 ... but the user starts the program at 14:00 ... the goal time is 17:00 so the countdown should do the counting down from 3:00 to 0:00.
I think the one hour difference results from a difference in timezones ...
here is CentralEuropeanTime ...
Oleka at 2007-7-12 18:48:48 >

Or it results from the current "summer-time" here in europe.The clocks where set one hour forward. In Autum the clock where set back one hour.
Oleka at 2007-7-12 18:48:48 >

> No i need the remaining time of a goaltime and the
> current time.
> The counter should start counting down at 12:30 ...
> but the user starts the program at 14:00 ... the goal
> time is 17:00 so the countdown should do the counting
> down from 3:00 to 0:00.
> I think the one hour difference results from a
> difference in timezones ...
> here is CentralEuropeanTime ...
Then you better refine your requirements.
Because if it does cross a DST change you need to understand if the users expect the duration to still be 3 hours or if they expect the run time to adjust for the cross over.
The two are not the same.
If I want to bake a turkey then I don't care if DST occurs or not - it still has to stay in the oven for 4 hours.
However when I need to get up in 8 hours for work (at 8:00 am) then I do expect to actually have the alarm go off at 8:00 am.
So, do you want to run from time A until time B? Or do you want to start at time A and run for N hours? The two are not equivalent if you cross a DST boundary.
jverda at 2007-7-12 18:48:49 >

> However when I need to get up in 8 hours for work (at
> 8:00 am) then I do expect to actually have the alarm
> go off at 8:00 am.
And if you want 8 hours' sleep and to wake up at 8:00 a.m., then most days you'll go to bed at midnight, but once a year you'll go to bed at 11:00 and once at 1:00.
jverda at 2007-7-12 18:48:49 >

I want to make a counter that runs from A to B
The user can specify A ... but when the user starts the program and the current Date is after A then the counter should count dowm from currentdate - A down to B. E.g. a entry in a database is on 9pm ...
the program should start a countdowm from 7pm -> 02:00, 01:59, 01:58 like this. on 9pm the program delete the entry from the database.
If the user starts the program at 8:05 pm the counter should countdown : 00:55, 00:54 and so on....
The only problem is this DST-problem.
Oleka at 2007-7-12 18:48:49 >

Something is strange. The count-start-dates are Timestamp-object stored in the DB and in CEST-Timezone. So are the goal-date-objects.
But if after i subtract the goal-time-longvalue with the count-time-longvalue to get the remaining hours for counting down to zero, a Date-object created with this long-value is in CET-Timezone ...
...
Oh now i know why ... cause in Januar 1970 there is CET ;)
Hmm but how to solve this problem?
Oleka at 2007-7-12 18:48:49 >

I guess if create a Calendar-object with the subtraction-longvalue and then set the Month to the current Month it should work...but is there an easier way ... Only to subtract two dates ...
Oleka at 2007-7-12 18:48:49 >

Ok this doesn't work ... :(It can't be so difficult to get the difference in Hours of two Date-objects ...
Oleka at 2007-7-12 18:48:49 >

> Something is strange. The count-start-dates are
> Timestamp-object stored in the DB and in
> CEST-Timezone.
No. If they're timestamps, they're not stored in any TZ.
> So are the goal-date-objects.
If they're Timestamps, there's no TZ component to them.
> But if after i subtract the goal-time-longvalue with
> the count-time-longvalue to get the remaining hours
> for counting down to zero,
How are you doing this subtraction.
> a Date-object created with
> this long-value is in CET-Timezone ...
No, it is not in any TZ.
> Hmm but how to solve this problem?
What problem?
jverda at 2007-7-12 18:48:49 >

> Ok this doesn't work ... :(
> It can't be so difficult to get the difference in
> Hours of two Date-objects ...
It's hard to say what you're doing wrong without more details from you. Code would be nice, since your explanations are a little fuzzy.
If you want to know the hours that will elapse from Date A to Date B, get their millis, subtract, and divide by 1000 * 60 * 60.
jverda at 2007-7-12 18:48:49 >

Is this the tired old idea of calculating the difference in milliseconds between the two times, then trying to stuff it into a Calendar object to produce the hours and minutes? I'm so tired of trying to explain why that doesn't work. So just take my word for it, it doesn't work. Do your own arithmetic on that number of milliseconds.
Ok, thx
The problen is to describe the problem in the right way i guess. Sorry for my bad english.
i subtract the mills from B and A ... thats what i mean with "longvalues" of Date-objects ...
I have read something that it should not be good to compare Date-objects with Timestamp-Objects. But both have millis since 1970?!
When i create a new Date or Calendar-object with the millis from a Timestamp-object the resulting Date is in CEST. The strange thing is, that after the subtraction of the millis of two Timestamps(their millis) and then create a Date-instance this Date is in CET. This make sense cause of its time near 1970/01/01. Ok not so strange anymore ;-)
Hmmm ok if i divide the result of the subtraction i get the hours.
Thats good but i need a Date-object with the remaining hours, minutes and days to zero.
Once again : the countdown should show the user how many hours, days and minutes remain until the goal-date.
The idea with the Calendar.add(value, amount)-method is nice but how can i install this Date-Object or whatever who got those remaining hours, minutes and days until the goaldate.
In the DB i store the goal-Timestamp and the begin-to-count-Timestamp. With those two objects i want to create the countdown.
But how ;-)
regards
Oleka at 2007-7-21 22:10:19 >

Ok i can do my own arithmetic if i know the millis remaining until the goal-date.
But consider the counter should start at 4:00 pm and the the user starts the program at 4:01pm,
in this case i need the millis from 4:01 to goal-date.
Then we are again at the subtraction ... wich doesn't work.
Oleka at 2007-7-21 22:10:19 >

Haven't run this, so it might give some errors. :)
long now = Calendar.getInstance().getTime().getTime();
Calendar target = Calendar.getInstance();
target.set(Calendar.HOUR_OF_DAY, target_hour);
target.set(Calendar.MINUTE, target_minute);
target.set(Calendar.SECOND, target_second);
long targetLong = target.getTime().getTime();
if (targetLong < now) {
targetLong += 24*60*60*1000;
}
long diff = targetLong - now;
Good luck...
An interesting case :if you set new Date(7200000) then the resulting date is 1970/01/01, 00:03:00When you set a Calendar with millis = 0 and then add(Calendar.HOUR, 2) the result is 00:003:00 too ... strange !?
Oleka at 2007-7-21 22:10:19 >

Ok now i got the solution :Create a Calendar-object.Set minute, second and hour to 0.Then subtract the 2 millis, divide them with 1000*60set the add(value, amout) method with those minutes then you got the minutes between the two timestamps.
Oleka at 2007-7-21 22:10:19 >

> i subtract the mills from B and A ... thats what i
> mean with "longvalues" of Date-objects ...
Right. That's the numer of millis between B and A. Divide by 3600000 to get hours from A to B
> I have read something that it should not be good to
> compare Date-objects with Timestamp-Objects. But both
> have millis since 1970?!
You can compare Date wth Timestamp.
> When i create a new Date or Calendar-object with the
> millis from a Timestamp-object the resulting Date is
> in CEST.
NO IT IS NOT! I told you this before--Date and Timestamp objects are not in ANY TZ. There is no TZ component.
> The strange thing is, that after the
> subtraction of the millis of two Timestamps(their
> millis) and then create a Date-instance
Don't do that. That Date is meaningless.
> this Date is
> in CET.
No, it's not.
> Hmmm ok if i divide the result of the subtraction i
> get the hours.
> Thats good but i need a Date-object with the
> remaining hours, minutes and days to zero.
No, you don't. A Date object represents a point in time--millis since a particular epoch. It does NOT represent the elapsed time between two arbitrary dates. A Date constructed this way will be meaningless.
> Once again : the countdown should show the user how
> many hours, days and minutes remain until the
> goal-date.
Then just do this with arithmetic on the millis.
jverda at 2007-7-21 22:10:19 >

Thank you again jverd.I finally use only the arithmetic version and no Calendar.But isn't it a good idea for a further version of jdk to implement such a countdown ?see ya
Oleka at 2007-7-21 22:10:19 >
