Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Daylight Savings Time in Calendar (http://www.velocityreviews.com/forums/t621631-daylight-savings-time-in-calendar.html)

Philipp 06-22-2008 09:45 AM

Daylight Savings Time in Calendar
 
Hello,
In my Locale there is Daylight Savings Time (DST) in effect. I would
like to get the time of the day without the DST (ie, the hour as
indicated by the sun position). To do this I use Calendar and set the
DST_OFFSET to 0. But that doesn't work. See example code below. What am
I doing wrong?

Thanks Phil

-- SSCCE --

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class TestCalendar {
public static void main(String[] args) {
Date now = new Date(1214127677625L);
System.out.println(now);
Calendar cal = new
GregorianCalendar(TimeZone.getTimeZone("Europe/Zurich"));

cal.setTimeInMillis(now.getTime()); // setting both calendars to now

System.out.println("current DST: " + cal.get(Calendar.DST_OFFSET));
System.out.println(" With DST: " + cal.get(Calendar.HOUR_OF_DAY));

cal.set(Calendar.DST_OFFSET, 0); // putting DST to 0
System.out.println("new DST: " + cal.get(Calendar.DST_OFFSET));
System.out.println(" Without DST: " + cal.get(Calendar.HOUR_OF_DAY));
}
}


// Prints
Sun Jun 22 11:41:17 CEST 2008
current DST: 3600000
With DST: 11
new DST: 0
Without DST: 11

// Expected
Sun Jun 22 11:41:17 CEST 2008
current DST: 3600000
With DST: 11
new DST: 0
Without DST: 10

John B. Matthews 06-22-2008 01:11 PM

Re: Daylight Savings Time in Calendar
 
In article <485e1efe$1_6@news.bluewin.ch>,
Philipp <sicsicsic@freesurf.ch> wrote:

> Hello,
> In my Locale there is Daylight Savings Time (DST) in effect. I would
> like to get the time of the day without the DST (ie, the hour as
> indicated by the sun position). To do this I use Calendar and set the
> DST_OFFSET to 0. But that doesn't work. See example code below. What am
> I doing wrong?

[...]

Setting DST_OFFSET has no effect because it's final; you might be able
to use add().

Instead, I think it helps to distinguish between the model (Calendar)
and the view (Format). Note how the time doesn't change, only the zone
of the view:

> sscce

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class date5 {

private static GregorianCalendar gc = new GregorianCalendar();
private static final SimpleDateFormat f =
new SimpleDateFormat("'Zone: 'z' Hour: 'HH");

public static void main(String[] args) {

print(gc);

f.setTimeZone(TimeZone.getTimeZone("EST"));
print(gc);

f.setTimeZone(TimeZone.getTimeZone("UTC"));
print(gc);
}

private static void print(GregorianCalendar gc) {
System.out.println(f.format(gc.getTime())
+ " " + gc.getTimeInMillis());
}
}

> output

Zone: EDT Hour: 09 1214140051344
Zone: EST Hour: 08 1214140051344
Zone: UTC Hour: 13 1214140051344

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

Roedy Green 06-22-2008 03:46 PM

Re: Daylight Savings Time in Calendar
 
On Sun, 22 Jun 2008 11:45:22 +0200, Philipp <sicsicsic@freesurf.ch>
wrote, quoted or indirectly quoted someone who said :

>n my Locale there is Daylight Savings Time (DST) in effect. I would
>like to get the time of the day without the DST (ie, the hour as
>indicated by the sun position). To do this I use Calendar and set the
>DST_OFFSET to 0. But that doesn't work. See example code below. What am
>I doing wrong?


Another approach is to use a TimeZone name that includes no DST. See
http://mindprod.com/jgloss/timezone.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green 06-22-2008 03:51 PM

Re: Daylight Savings Time in Calendar
 
On Sun, 22 Jun 2008 11:45:22 +0200, Philipp <sicsicsic@freesurf.ch>
wrote, quoted or indirectly quoted someone who said :

>// Prints
>Sun Jun 22 11:41:17 CEST 2008
>current DST: 3600000
> With DST: 11
>new DST: 0
> Without DST: 11
>
>// Expected
>Sun Jun 22 11:41:17 CEST 2008
>current DST: 3600000
> With DST: 11
>new DST: 0
> Without DST: 10


Date and Calendar cause me to boil with anger at their goofy design
that subverts type checking. I suspect what might be happening is DST
is being ignored when you have done a setTimeZone.

There are some comments in the JavaDoc that suggest there is some
problem with changing the offset. Did you get any
IllegalArgumentExcepetions you ignored?

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Philipp 06-22-2008 06:07 PM

Re: Daylight Savings Time in Calendar
 
John B. Matthews wrote:
> In article <485e1efe$1_6@news.bluewin.ch>,
> Philipp <sicsicsic@freesurf.ch> wrote:
>
>> Hello,
>> In my Locale there is Daylight Savings Time (DST) in effect. I would
>> like to get the time of the day without the DST (ie, the hour as
>> indicated by the sun position). To do this I use Calendar and set the
>> DST_OFFSET to 0. But that doesn't work. See example code below. What am
>> I doing wrong?

> [...]
>
> Setting DST_OFFSET has no effect because it's final; you might be able
> to use add().


Thanks for your answer.
In my example SSCCE the DST is actually changed (verified by the get()
and its printout). It isn't final, but it has no effect.


> Instead, I think it helps to distinguish between the model (Calendar)
> and the view (Format). Note how the time doesn't change, only the zone
> of the view:


Yes absolutely, as the time in millis is a GMT timestamp (and not the
time in a given locale). That's what I want to use.
What you are suggesting is that I should find a local which matches mine
("Europe/Zurich") but which has no DST. I don't know if that exists,
but anyhow I want to be able to use the program in any local, and the
hour without DST should be calculated correctly. So I really want to
take any locale and remove the DST (only if there is one).

I know of a workaround which is to write:

cal.setTimeInMillis(now.getTime() - cal.get(Calendar.DST_OFFSET));

but this is ugly as I am actually changing the timestamp and not the
representation of the timestamp.

Phil


Philipp 06-22-2008 06:29 PM

Re: Daylight Savings Time in Calendar
 
Roedy Green wrote:
> On Sun, 22 Jun 2008 11:45:22 +0200, Philipp <sicsicsic@freesurf.ch>
> wrote, quoted or indirectly quoted someone who said :
>
>> // Prints
>> Sun Jun 22 11:41:17 CEST 2008
>> current DST: 3600000
>> With DST: 11
>> new DST: 0
>> Without DST: 11
>>
>> // Expected
>> Sun Jun 22 11:41:17 CEST 2008
>> current DST: 3600000
>> With DST: 11
>> new DST: 0
>> Without DST: 10

>
> Date and Calendar cause me to boil with anger at their goofy design
> that subverts type checking.


Absolutely. Also Date should be immutable.

> I suspect what might be happening is DST
> is being ignored when you have done a setTimeZone.


Well the DST rule is still applied correctly after the set, so the new
value is actually ignored.

> There are some comments in the JavaDoc that suggest there is some
> problem with changing the offset. Did you get any
> IllegalArgumentExcepetions you ignored?


No Exception. But a modified test shows that the DST in the time zone
and the DST in the Calendar are two different values. The one which is
relevant for the Calendars output is the one in the TimeZone (which
can't be changed). :-(

Unless I missed something, I thus have to use the workaround then (see
my answer to John B. Mathews).

Phil

Roedy Green 06-22-2008 07:51 PM

Re: Daylight Savings Time in Calendar
 
On Sun, 22 Jun 2008 20:29:52 +0200, Philipp <sicsicsic@freesurf.ch>
wrote, quoted or indirectly quoted someone who said :

>No Exception. But a modified test shows that the DST in the time zone
>and the DST in the Calendar are two different values. The one which is
>relevant for the Calendars output is the one in the TimeZone (which
>can't be changed). :-(


and no documentation GRRR!
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

John B. Matthews 06-22-2008 09:07 PM

Re: Daylight Savings Time in Calendar
 
In article <485e94a5$1_2@news.bluewin.ch>,
Philipp <sicsicsic@freesurf.ch> wrote:

> John B. Matthews wrote:
> > In article <485e1efe$1_6@news.bluewin.ch>,
> > Philipp <sicsicsic@freesurf.ch> wrote:
> >
> >> Hello,
> >> In my Locale there is Daylight Savings Time (DST) in effect. I would
> >> like to get the time of the day without the DST (ie, the hour as
> >> indicated by the sun position). To do this I use Calendar and set the
> >> DST_OFFSET to 0. But that doesn't work. See example code below. What am
> >> I doing wrong?

> > [...]
> >
> > Setting DST_OFFSET has no effect because it's final; you might be able
> > to use add().

>
> Thanks for your answer.
> In my example SSCCE the DST is actually changed (verified by the get()
> and its printout). It isn't final, but it has no effect.


Mine says "public static final int DST_OFFSET." Possibly, DST is
changing for some other reason.

> > Instead, I think it helps to distinguish between the model (Calendar)
> > and the view (Format). Note how the time doesn't change, only the zone
> > of the view:

>
> Yes absolutely, as the time in millis is a GMT timestamp (and not the
> time in a given locale). That's what I want to use. What you are
> suggesting is that I should find a local which matches mine
> ("Europe/Zurich") but which has no DST. I don't know if that
> exists, but anyhow I want to be able to use the program in any local,
> and the hour without DST should be calculated correctly. So I really
> want to take any locale and remove the DST (only if there is one).
>
> I know of a workaround which is to write:
>
> cal.setTimeInMillis(now.getTime() - cal.get(Calendar.DST_OFFSET));
>
> but this is ugly as I am actually changing the timestamp and not the
> representation of the timestamp.


IIUC, you want to know what hour a local clock would say in the absence
of daylight savings time for a given instant. I see what you mean about
finding a matching time zone without DST:-)

Instead, you can change a copy of the instant. Use add() to subtract the
daylight savings for the TimeZone belonging to the Calendar instance;
it's zero for standard time. Here's a comparison:

> sscce

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class date5 {

private static GregorianCalendar gc = new GregorianCalendar();
private static final SimpleDateFormat f1 =
new SimpleDateFormat("MMM dd, yyyy HH:mm:ss");
private static final SimpleDateFormat f2 =
new SimpleDateFormat("'gmt'Z'; hour: 'HH");

public static void main(String[] args) {
print(gc, TimeZone.getTimeZone("UTC"));
print(gc, TimeZone.getTimeZone("EST"));
print(gc, TimeZone.getDefault());

TimeZone tz = gc.getTimeZone();
gc.add(GregorianCalendar.MILLISECOND, -tz.getDSTSavings());
print(gc, tz);
}

private static void print(GregorianCalendar gc, TimeZone tz) {
f2.setTimeZone(tz);
long m = gc.getTimeInMillis();
System.out.println(
f1.format(gc.getTime())
+ "; ms: " + m
+ "; " + f2.format(gc.getTime())
+ "; dst: " + tz.inDaylightTime(gc.getTime()));
}
}

> output

Jun 22, 2008 16:56:19; ms: 1214168179567; gmt+0000; hour: 20; dst: false
Jun 22, 2008 16:56:19; ms: 1214168179567; gmt-0500; hour: 15; dst: false
Jun 22, 2008 16:56:19; ms: 1214168179567; gmt-0400; hour: 16; dst: true
Jun 22, 2008 15:56:19; ms: 1214164579567; gmt-0400; hour: 15; dst: true

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

Arne Vajh°j 06-22-2008 09:28 PM

Re: Daylight Savings Time in Calendar
 
Philipp wrote:
> In my Locale there is Daylight Savings Time (DST) in effect. I would
> like to get the time of the day without the DST (ie, the hour as
> indicated by the sun position). To do this I use Calendar and set the
> DST_OFFSET to 0. But that doesn't work. See example code below.


> Date now = new Date(1214127677625L);
> System.out.println(now);
> Calendar cal = new
> GregorianCalendar(TimeZone.getTimeZone("Europe/Zurich"));
>
> cal.setTimeInMillis(now.getTime()); // setting both calendars to now
>
> System.out.println("current DST: " + cal.get(Calendar.DST_OFFSET));
> System.out.println(" With DST: " + cal.get(Calendar.HOUR_OF_DAY));
>
> cal.set(Calendar.DST_OFFSET, 0); // putting DST to 0
> System.out.println("new DST: " + cal.get(Calendar.DST_OFFSET));
> System.out.println(" Without DST: " + cal.get(Calendar.HOUR_OF_DAY));


> Sun Jun 22 11:41:17 CEST 2008
> current DST: 3600000
> With DST: 11
> new DST: 0
> Without DST: 11


> Sun Jun 22 11:41:17 CEST 2008
> current DST: 3600000
> With DST: 11
> new DST: 0
> Without DST: 10


To me it makes perfectly sense that you can not change the DST
behavior of a time zone.

Because that is how the real world is.

You need to specify a time zone that matches your
requirements.

Example:

DateFormat df = new SimpleDateFormat("HH");
df.setTimeZone(TimeZone.getTimeZone("GMT+01:00"));
System.out.println(df.format(cal.getTime()));

(and do not call cal.set(Calendar.DST_OFFSET, 0) because
that messes things up)

Arne


John B. Matthews 06-23-2008 01:45 AM

Re: Daylight Savings Time in Calendar
 
In article <gaydnam-zaeBVcPVnZ2dnUVZ_hudnZ2d@comcast.com>,
Lew <lew@lewscanon.com> wrote:

> John B. Matthews wrote:
> > Mine says "public static final int DST_OFFSET." Possibly, DST is
> > changing for some other reason.

>
> That doesn't mean that the offset cannot change. It means that the integer
> representing which field is the offset does not change.
>
> So yes, DST_OFFSET is a final field. However, its value is not the value of
> the offset.


You're right! My bad. Sadly, this occurred to me well _after_ I'd hit
send:-) Thanks.

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews


All times are GMT. The time now is 02:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.