File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Converting Date to PST Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting Date to PST" Watch "Converting Date to PST" New topic

Converting Date to PST

Kishore Nair

Joined: Nov 30, 2004
Posts: 10
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?

Never resign to your fate
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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.)

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Kishore Nair

Joined: Nov 30, 2004
Posts: 10
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.
I agree. Here's the link:
subject: Converting Date to PST
It's not a secret anymore!