Timezone/DST

B

Big Jim

Hi Guys, I'm having problems detecting DST. On a solaris box in the UK
(and on my windows 2000 PC) I have the following test prog:

import java.util.*;

public class DateTest
{
public static void main(String args[])
{
printDate(new java.util.Date(1163203200282L), "Nov 11?");
printDate(new java.util.Date(1152230400386L), "Jul 7th?");
}

static void printDate(java.util.Date date, String desc)
{
GregorianCalendar c = new GregorianCalendar(
date.getYear(), date.getMonth(), date.getDate());

System.out.println("*******************************");
System.out.println(desc);

System.out.println("Date : " + date);
System.out.println("Long : " + date.getTime());
System.out.println("Zone Offset : " +
c.get(Calendar.ZONE_OFFSET));
System.out.println("DST Offset : " +
c.get(Calendar.DST_OFFSET));
System.out.println("Long Corrected : " + (
date.getTime() + c.get(Calendar.ZONE_OFFSET) +
c.get(Calendar.DST_OFFSET)));
System.out.println("Date Corrected : " + new Date(
date.getTime() - (c.get(Calendar.ZONE_OFFSET) +
c.get(Calendar.DST_OFFSET))));

System.out.println("*******************************");
}
}
which produces the following output:

*******************************
Nov 11?
Date : Sat Nov 11 00:00:00 GMT 2006
Long : 1163203200282
Zone Offset : 0
DST Offset : 0
Long Corrected : 1163203200282
Date Corrected : Sat Nov 11 00:00:00 GMT 2006
*******************************

*******************************
Jul 7th?
Date : Fri Jul 07 01:00:00 BST 2006
Long : 1152230400386
Zone Offset : 0
DST Offset : 0
Long Corrected : 1152230400386
Date Corrected : Fri Jul 07 01:00:00 BST 2006
*******************************

Why am I not getting a DST offset for July 7th when it seems to know
it's a BST date?

Cheers, Richard.
 
O

Oliver Wong

Big Jim said:
Hi Guys, I'm having problems detecting DST. On a solaris box in the UK
(and on my windows 2000 PC) I have the following test prog:

See the post I made to your "Java Date Problem" thread. I suspect you
may be taking the wrong approach to this problem. You can avoid the time
zone issues altogether if you use number of (milli)seconds since the Unix
epoch.

- Oliver
 
B

Big Jim

Roedy Green said:
check the system properties. Perhaps your OS gives a different default
Timezone in winter and summer treating your timezone specially.

see http://mindprod.com/applets/wassup.html
see http://mindprod.com/jgloss/timezone.html

It seems stranger than that:

Date = new Date(long);
Calendar c = new GregorianCalendar(date.getYear(), date.getMonth(),
date.getDate());
TimeZone tz = c.getTimeZone();

then, if it's a BST date,

tx.getDSTSavings returns 3600000 but c.get(Calendar.DST_OFFSET) returns 0!

This is on Solaris 2.8 and win 2000 using 1.4.2_08. Anyone know what's going
on there?
 
P

P.Hill

Big said:
Hi Guys, I'm having problems detecting DST. On a solaris box in the UK
(and on my windows 2000 PC) I have the following test prog:

GregorianCalendar c = new GregorianCalendar(
date.getYear(), date.getMonth(), date.getDate());

NOOOOOOOO!!!!

see http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html#getYear()
<quote>
getYear

public int getYear()

Deprecated. As of JDK version 1.1, replaced by
Calendar.get(Calendar.YEAR) - 1900.

Returns a value that is the result of subtracting 1900 from the
year that contains or begins with the instant in time represented by
this Date object, as interpreted in the local time zone.

Returns:
the year represented by this date, minus 1900.
</quote>

Please do a little RTFM on Date and Calendar.
getYear() is deprecated for a reason. Date.getTime() is very useful
for pushing a value into a calendar.setTime().

So the answer to the question of why you can't detect
DST with the above code is:

Because in the year 106 AD there was not DST! :)

HTH,
-Paul

p.s. This is another place where a nice long simpleDateFormat showing
all fields (set to an appropriate TZ) helps you to understand what is
going on.
 
B

Big Jim

P.Hill said:
NOOOOOOOO!!!!

see http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html#getYear()
<quote>
getYear

public int getYear()

Deprecated. As of JDK version 1.1, replaced by
Calendar.get(Calendar.YEAR) - 1900.

Returns a value that is the result of subtracting 1900 from the year
that contains or begins with the instant in time represented by this Date
object, as interpreted in the local time zone.

Returns:
the year represented by this date, minus 1900.
</quote>

Please do a little RTFM on Date and Calendar.
getYear() is deprecated for a reason. Date.getTime() is very useful
for pushing a value into a calendar.setTime().

So the answer to the question of why you can't detect
DST with the above code is:

Because in the year 106 AD there was not DST! :)

HTH,
-Paul

p.s. This is another place where a nice long simpleDateFormat showing all
fields (set to an appropriate TZ) helps you to understand what is going
on.

Aha! it all makes sense ...

my introduction into the world of financial application software (coming
from telecoms) has been painful, not least into the world of java dates ....

Thanks again.
 

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,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top