Daylight Savings Time in Calendar  
Author Message
Arne Vajh鴍





PostPosted: 2008-6-26 8:11:00 Top

java-programmer, Daylight Savings Time in Calendar Philipp wrote:
> Philipp wrote:
>> John B. Matthews wrote:
>> 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.
>
> Update: this workaround is actually wrong during one hour at the time
> change period. So it's not a good solution. If anybody knows of a better
> one, I'd be happy to use it.

DateFormat df = new SimpleDateFormat("HH");
TimeZone tz = TimeZone.getTimeZone("GMT");
tz.setRawOffset(cal.getTimeZone().getRawOffset());
df.setTimeZone(tz);
System.out.println(df.format(cal.getTime()));

Arne
 
Arne Vajh鴍





PostPosted: 2008-6-26 8:11:00 Top

java-programmer >> Daylight Savings Time in Calendar Philipp wrote:
> Philipp wrote:
>> John B. Matthews wrote:
>> 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.
>
> Update: this workaround is actually wrong during one hour at the time
> change period. So it's not a good solution. If anybody knows of a better
> one, I'd be happy to use it.

DateFormat df = new SimpleDateFormat("HH");
TimeZone tz = TimeZone.getTimeZone("GMT");
tz.setRawOffset(cal.getTimeZone().getRawOffset());
df.setTimeZone(tz);
System.out.println(df.format(cal.getTime()));

Arne
 
Philipp





PostPosted: 2008-6-26 22:53:00 Top

java-programmer >> Daylight Savings Time in Calendar Arne Vajh鴍 wrote:
> Philipp wrote:
>> Philipp wrote:
>>> John B. Matthews wrote:
>>> 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.
>>
>> Update: this workaround is actually wrong during one hour at the time
>> change period. So it's not a good solution. If anybody knows of a
>> better one, I'd be happy to use it.
>
> DateFormat df = new SimpleDateFormat("HH");
> TimeZone tz = TimeZone.getTimeZone("GMT");
> tz.setRawOffset(cal.getTimeZone().getRawOffset());
> df.setTimeZone(tz);
> System.out.println(df.format(cal.getTime()));
>
> Arne

Thank you Arne, this works perfect!
I somehow missed the setRawOffset() in TimeZone and thus thought that
TimeZones were immutable.

Phil