0

I have a Calendar Object, which is in UTC time zone. I want to convert the date and time in that Calendar Object to PST keeping in mind all the DST. I am looking to write a helper function with the following signature:

private Calendar getPSTTime(Calendar utcTime)
{

}
Ken White
  • 123,280
  • 14
  • 225
  • 444
supreethmurthy
  • 87
  • 1
  • 1
  • 9
  • There's no need to say "Java Programming Language" in the title; you've included a tag for it, and the tag system works very well here without any help. Any information you can provide with tags is not necessary in the subject, and you can use the space to better describe your question instead. (You also typically should actually *ask a question* instead of just stating a goal, and show some effort you've made at actually solving the problem yourself.) – Ken White Feb 08 '13 at 23:54
  • Also a dupe of [How to handle calendar TimeZones using Java?](http://stackoverflow.com/q/230126/62576), [Converting a UTC time to a local time zone in Java](http://stackoverflow.com/questions/3010035/converting-a-utc-time-to-a-local-time-zone-in-java?), [Converting date from UTC to PST in Java](http://stackoverflow.com/questions/6075912/converting-date-from-utc-to-pst-in-java?rq=1) and several others. Please at least do a basic search here before posting a question to see if it's already been asked and answered. Some of those I linked are in the "Related" list to the right. Thanks. – Ken White Feb 08 '13 at 23:57
  • I did look for the existing posts. It didnt help me. My question is specific. Also, I don't know how better I can describe the function. I have given the method signature. People who have written java code should understand what I am trying to achieve. – supreethmurthy Feb 09 '13 at 00:34

2 Answers2

1
private Calendar getPSTTime(Calendar utcTime)
{
    TimeZone americaPacific = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar pacificTime = Calendar.getInstance(americaPacific);
    pacificTime.setTime(utcTime.getTime());
    return pacificTime;
}

FYI: All of the available time zone String identifiers are returned by the method TimeZone.getAvailableIDs(). That's where I got "America/Los_Angeles" from.

Asaph
  • 159,146
  • 25
  • 197
  • 199
  • This was the first thing I tried. Doing this will just set the UTC Time. It won't convert the time to PST. Thanks. – supreethmurthy Feb 09 '13 at 00:36
  • @user2055876 Try maybe `TimeZone.getTimeZone("PST");`. Also how did you check that this method doesn't work (it works fine for me)? Common mistake is that people check it by printing `calendar.getTime()` which returns `new Date(milis)` based on milliseconds that are stored in calendar, forgetting that `Date` uses default locale. To check if calendar converts correctly get values directly from it, like `calendar.get(Calendar.HOUR_OF_DAY);`. – Pshemo Feb 09 '13 at 01:21
0

Time event in Java is stored as the number of milliseconds between this event and January 1, 1970, 00:00:00 GMT (e.g. as if it happened in London)

So whenever we talk date and time in Java, as represented by java.util.Date or java.util.Calendar, we always talk about absolute number, regardless of timezone.

Time zone only makes sense when we try to output date/time as string. On top of my head I can recall two ways of doing it:

  • use Calendar, set time zone, use get(Calendar.XXX) method to get individual date/time components in time zone you want
  • use SimpleDateTimeFormat, set time zone, format the whole date/time into single string in time zone you want

There is really no such thing as "time zone conversion" in Java, but there is time zone specific formatting.

However, if you want to make things really confusing, you can add the "correction" to your calendar time value so that it will return field values as if it was in PST timezone:

private Calendar getPSTTime(Calendar utcTime)
{
   TimeZone pstZ = TimeZone.getTimeZone("America/Los_Angeles");
   long offset = pstZ.getOffset(utcTime.getTime());
   Calendar pacificTime = Calendar.getInstance();
   pacificTime.setTimeInMillis(utcTime.getTimeInMillis()-offset); 
   return pacificTime;
}