• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting Date to PST

 
Kishore Nair
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to deploy an app on both East Coast and West coast machines. I need to simply create a new date to insert into a table with PST, regardless of which machine I am on. Here is what I am doing.
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);

// set up rules for daylight savings time
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);

// create a GregorianCalendar with the Pacific Daylight time zone
// and the current date and time
Calendar calendar = new GregorianCalendar(pdt);
java.util.Date sqlDate = calendar.getTime();
java.sql.Date sqDate = new java.sql.Date(sqlDate.getTime());
ps.setDate(3, sqDate);

This is straight out of the examples. No matter what I do, I get EST out of this. I am on an East Coast machine. What am I doing wrong?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your date field in the database a String? If not, it shouldn't matter what the value is...dates should be recorded as GMT and then translated for your locale. Thus, it might be inserted as a PST date, but when you view it in the database, the viewing software shows it as EST. (Set the time zone on your computer to PST and see what you get.)
 
Kishore Nair
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel, it's not defined as a string. It's a date. I am viewing the date by getting it as System.out.println( rs.getString("updt_dt") ); This should not convert it back to EST right? I have a workaround for now. This is what I am doing :
if( calendar.getTimeZone().getID().equalsIgnoreCase("America/New_York"))
{
calendar.add(Calendar.HOUR, -3);
sqlDate = sqlDf.parse(sqlDf.format( calendar.getTime()));
}

But that is ugly programming, in my opinion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic