amethystct
Programmer
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());
}
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());
}