[Advanced-java] Daylight saving
Joe Sam Shirah
jshirah at attglobal.net
Fri Jul 18 18:48:34 2003
> So, *when* the Daylight Savings takes effect for each TimeZone
> is not part of the API, it is left to the user to create...
This is not the case, as can be shown by a pretty simple program:
---------------------------------------------------------
import java.text.*;
import java.util.*;
public class ShowTZStuff
{
SimpleDateFormat sdf = new SimpleDateFormat();
TimeZone tz = sdf.getTimeZone();
public JLocaleDisplay2()
{
System.out.println( sdf.format( new Date() ) );
System.out.println( tz );
System.out.println( "Use DaylightTime? " + tz.useDaylightTime() );
} // end constructor
public static void main(String[] args)
{
new JLocaleDisplay2();
} // end main
} // end class ShowTZStuff
---------------------------------------------------------
If your location does not use Daylight Time, the code should be modified
to set it to one that does.
In addition, while the rules are not available in normal form,
toString() does show the information. The output on my machine ( East Coast
US - DST ) is
---------------------------------------------------------
7/18/03 1:33 PM
sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings
=360
0000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=A
meri
ca/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0
,sta
rtMode=3,startMonth=3,startDay=1,startDayOfWeek=1,startTime=7200000,startTim
eMod
e=0,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000,endTimeMod
e=0]
]
Use DaylightTime? true
---------------------------------------------------------
Ugly, but it is there. And the time shown is DST. If you look at the
source for TimeZone.java, you will see that sun.util.calendar.ZoneInfo and
sun.util.calendar.ZoneInfoFile are referenced.
If anyone wants to get deeper into what's going on, there is a section
in "Advanced Programming for the Java 2 Platform Chapter 8" at
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/p
erf3.html
discusses the creation of 323 time zones. I think some of the code has
changed, but the profile and HAT info are still worthwhile.
Joe Sam
Joe Sam Shirah - http://www.conceptgo.com
conceptGO - Consulting/Development/Outsourcing
Java Filter Forum: http://www.ibm.com/developerworks/java/
Just the JDBC FAQs: http://www.jguru.com/faq/JDBC
Going International? http://www.jguru.com/faq/I18N
Que Java400? http://www.jguru.com/faq/Java400
----- Original Message -----
From: <dharmendra.sharan@ubs.com>
To: <venkatakrishnan.kan@wipro.com>; <advanced-java@xcf.berkeley.edu>
Sent: Friday, July 18, 2003 10:34 AM
Subject: RE: [Advanced-java] Daylight saving
Hi Venkat,
I think you need to look *closely* at java.util.SimpleTimeZone
The point I need to make is that this class allows you to specify the
Daylight Savings Time for any given TimeZone. So, *when* the Daylight
Savings takes effect for each TimeZone is not part of the API, it is left to
the user to create the appropriate SimpleTimeZone object. SimpleTimeZone API
does provide initialization parameters and methods to allow you to specify
the Start date and time when Daylight Savings is starts and similary for
when the Daylight Savings ends. (which you refer in your email as the
"forward" and "backward" dates)
Should Java have all possible DaylightSavings Start date-time and end
Date-times for each TimeZone, stored as part of a SimpleTimeZone or some
other class? And I think your point of view is yes it should, however even
if it doesn't it *does* provide an easy to use API for creating such a
TimeZone as required, I don't know the reason why this approach was taken
(as I'm not yet involved in the Java Community process!), If I have to take
a guess then my guess would be, perhaps the Daylight Savings Time has not
been consistent enough *historically* for a given TimeZone to allow for such
crisp start/end date-time rules for each TimeZone to be framed within the
SimpleTimeZone class. I encourage you to look for answers... If anyone has
the reasoning for the same please enlighten us. Thanks!
<snippet>
// examples of creating SimpleTimeZone objects for a given TimeZone
// with start date-time and end date-time rules for Daylight Savings.
// Base GMT offset: -8:00
// DST starts: at 2:00am in standard time
// on the first Sunday in April
// DST ends: at 2:00am in daylight time
// on the last Sunday in October
// Save: 1 hour
SimpleTimeZone(-28800000, // Base GMT offset in milliseconds
"America/Los_Angeles", // given TimeZone for which we are
specifying DST rules
Calendar.APRIL, 1, -Calendar.SUNDAY, // Calendar date when Daylight
Savings starts
7200000, // time in milliseconds when the Daylight Savings
starts
Calendar.OCTOBER, -1, Calendar.SUNDAY, // Calendar date when
Daylight Savings ends
7200000, // time in milliseconds when the Daylight Savings starts
3600000) // time in milliseconds to be applied for Daylight
savings
</snipper>
note: see
http://java.sun.com/j2se/1.4.2/docs/api/java/util/SimpleTimeZone.html for
details
Hope this explains.
Regards,
Dharmendra
ps: have a good day!
-----Original Message-----
From: K.P.Venkatakrishnan [mailto:venkatakrishnan.kan@wipro.com]
Sent: Friday, July 18, 2003 12:04 AM
To: Sharan, Dharmendra
Subject: FW: [Advanced-java] Daylight saving
Hi Dharmendra,
What I want is the dates and not the OFFSET. Please see the
explanation I gave to Matt below. Anyway, I have decided to write some
Code of my own to achieve this since Java doesn't provide for it.
Regards,
Venkat.
-----Original Message-----
From: K.P.Venkatakrishnan [mailto:venkatakrishnan.kan@wipro.com]
Sent: Thursday, July 17, 2003 6:28 PM
To: 'Salerno Matthew L Contr HQ SSG/SWDUB'
Subject: RE: [Advanced-java] Daylight saving
Hi Matt,
My code would look something like this.
Timezone tz = Timezone.getTimezone('timezone-name');
Date fwDate = tz.getForwardDaylightSwitchingDay(int year);
Date bwDate = tz.getBackwardDaylightSwitchingDay(int year);
What I expect in these methods is:
Date getForwardDaylightSwitchingDay(int year){
int day usePrivateDateToGetThisDate(int startMonth, int
startDay,
int
startDayOfWeek);
//Note: startMonth, startDay, startDayOfWeek are private members
//of SimpleTimezone, not Timezone
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(year,startMonth,day);
Date retDate = cal.getTime();
}
Similar implementation for getBackwardDaylightSwitchingDay(int year).
Hope I am clear.
I am quite surprised that Java doesn't provide such an interface.
Regards,
Venkat.
-----Original Message-----
From: Salerno Matthew L Contr HQ SSG/SWDUB
[mailto:Matthew.Salerno@Gunter.AF.mil]
Sent: Thursday, July 17, 2003 6:07 PM
To: venkatakrishnan.kan@wipro.com
Subject: RE: [Advanced-java] Daylight saving
I have done work with timezones on a project; I am not sure what
exactly you are trying to do... Can you send a code snippet of what you
have now?
Matt
-----Original Message-----
From: advanced-java-bounces@lists.xcf.berkeley.edu
[mailto:advanced-java-bounces@lists.xcf.berkeley.edu] On Behalf Of
K.P.Venkatakrishnan
Sent: Thursday, July 17, 2003 6:16 AM
To: advanced-java@lists.xcf.berkeley.edu
Subject: RE: [Advanced-java] Daylight saving
Yes, I have used them. What I want is the dates and not the duration of
fwd/bwd switch.
Regards,
Venkat.
-----Original Message-----
From: advanced-java-bounces@lists.xcf.berkeley.edu
[mailto:advanced-java-bounces@lists.xcf.berkeley.edu] On Behalf Of Jukka
Sundberg
Sent: Thursday, July 17, 2003 2:47 PM
To: advanced-java@lists.xcf.berkeley.edu
Subject: Re: [Advanced-java] Daylight saving
Have you checked super class methods for SimpleTimeZone,
For example, TimeZone.getTimeZone("GMT-8").getID() returns
"GMT-08:00". and also method
getDSTSavings()
K.P.Venkatakrishnan wrote:
> Hi,
>
> Is it possible to get the forward and backward switching
> 'dates' for a given timezone using Java.
>
> SimpleTimeZone doesn't seem to provide this. We can only
set
> the rules for the timezone (not even
>
> get/read the rule of a timezone) and verify whether a date falls in
DST
> or not.
>
>
>
> Thanks and regards,
>
> Venkat.
>
>
**************************Disclaimer************************************
>
> Information contained in this E-MAIL being proprietary to Wipro
Limited is
> 'privileged' and 'confidential' and intended for use only by the
individual
> or entity to which it is addressed. You are notified that any use,
copying
> or dissemination of the information contained in the E-MAIL in any
manner
> whatsoever is strictly prohibited.
>
>
************************************************************************
***
>
_______________________________________________
Advanced-java mailing list
Advanced-java@lists.xcf.berkeley.edu
http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
**************************Disclaimer************************************
Information contained in this E-MAIL being proprietary to Wipro Limited
is
'privileged' and 'confidential' and intended for use only by the
individual or entity to which it is addressed. You are notified that
any use, copying
or dissemination of the information contained in the E-MAIL in any
manner
whatsoever is strictly prohibited.
************************************************************************
***
_______________________________________________
Advanced-java mailing list
Advanced-java@lists.xcf.berkeley.edu
http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
**************************Disclaimer************************************
Information contained in this E-MAIL being proprietary to Wipro Limited is
'privileged' and 'confidential' and intended for use only by the individual
or entity to which it is addressed. You are notified that any use, copying
or dissemination of the information contained in the E-MAIL in any manner
whatsoever is strictly prohibited.
***************************************************************************
Visit our website at http://www.ubs.com
This message contains confidential information and is intended only
for the individual named. If you are not the named addressee you
should not disseminate, distribute or copy this e-mail. Please
notify the sender immediately by e-mail if you have received this
e-mail by mistake and delete this e-mail from your system.
E-mail transmission cannot be guaranteed to be secure or error-free
as information could be intercepted, corrupted, lost, destroyed,
arrive late or incomplete, or contain viruses. The sender therefore
does not accept liability for any errors or omissions in the contents
of this message which arise as a result of e-mail transmission. If
verification is required please request a hard-copy version. This
message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities or
related financial instruments.
_______________________________________________
Advanced-java mailing list
Advanced-java@lists.xcf.berkeley.edu
http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java