Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about timezones

Status
Not open for further replies.

amethystct

Programmer
Jan 26, 2004
21
FR
I'm using Calendar to compare 2 dates to see their difference in time. I couldn't get the locale to set properly so I created a lookup of states to just give me the offset. The problem is that for states that don't observe daylight savings I need to set a different offset during dst. Is there a isDst I can use to get this? OR, if someone knows how to actually get the locale to work so the Calendar could set the offset itself that'd be great!

What I'm currently using to set the Calendar is below.

int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;

Calendar returnCal = null;
String datePassed = "2004-09-14 12:00:00";

try {

year = Integer.parseInt(datePassed.substring(0,4));
month = Integer.parseInt(datePassed.substring(5,7));
day = Integer.parseInt(datePassed.substring(8,10));
hour = Integer.parseInt(datePassed.substring(11,13));
minute = Integer.parseInt(datePassed.substring(14,16));
if (datePassed.length() > 16) {//not always passing in the seconds
second = Integer.parseInt(datePassed.substring(17,19));
}

month = month - 1;

int gmtNum = getOffSet("AZ");

String[] ids2 = TimeZone.getAvailableIDs(gmtNum * 60 * 60 * 1000);
if (ids2.length == 0)
System.exit(0);

SimpleTimeZone pdt = new SimpleTimeZone(gmtNum * 60 * 60 * 1000, ids2[0]);

pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);

returnCal = new GregorianCalendar(pdt);

returnCal.set(year, month, day, hour, minute,second);

System.out.println("Calendar after setting with date/time passed in: " + returnCal);
System.out.println("new timezone is: " + pdt + " zone offset: " + gmtNum);

} catch (Exception exc) {
System.out.println("### Exception setting the date in DateUtility.createDateWithTimeZone: " + exc.toString());
}
 
Timezones are a lot of fun (not). We have a customer in Indiana, and the part of the state they're in doesn't follow daylight savings time.

The Timezone class in java.util does a good job, but it isn't, and can't ever be perfect. Because timezones are set by state and local goverments, any particular location can change timezone at the whim of a politician.

Best thing to do is have a DB table of locations, and each location has a reference to a timezone. The timezone table details an offset from UTC, if it participates in daylight savings, what the DST offset value is, when the location goes into DST, and when it leaves DST. The last is tricky, as you have to allow for things like: "Leaves DST on the last Sunday in October".

Just be grateful you're only dealing with Gregorian calendars. The Hebrew calendar is a total bear to work with (see and don't get me started on the Chinese/Asian calendars.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top