Reputation: 538
Calendar cl = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
cl.setTimeInMillis(time);
Toast.makeText(getActivity(), cl.getTime().toString(), Toast.LENGTH_LONG).show();
What ever time I put it always returns the time in EST. I want it to return the time in PST. Does anyone know that could posibly be wrong?
P.S.
My local time is EST.
Upvotes: 3
Views: 2339
Reputation: 3767
This isn't really an answer but it's too big to put into a comment. This is what I found.
My test looked like this
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
Calendar cl = Calendar.getInstance(tz);
System.out.println(tz.getDisplayName());
String dayname = cl.getDisplayName(Calendar.DAY_OF_WEEK,Calendar.SHORT,Locale.US);
String monthname = cl.getDisplayName(Calendar.MONTH,Calendar.SHORT,Locale.US);
int hour = cl.get(Calendar.HOUR);
int min = cl.get(Calendar.MINUTE);
int sec = cl.get(Calendar.SECOND);
int mill = cl.get(Calendar.MILLISECOND);
int year = cl.get(Calendar.YEAR);
System.out.println("Time = " + cl.getTime().toString());
System.out.println("Manually = " +
dayname + " " + monthname + " " +
hour + ":" + min +":" + sec + ":" + mill + " " +
cl.getTimeZone().getDisplayName(Locale.US) + " " + year);
Which gave the output
Pacific Standard Time
Time = Tue Nov 05 11:36:33 EST 2013
Manually = Tue Nov 8:36:33:238 Pacific Standard Time 2013
Looking at Calendar.getTime()
:
public final Date getTime() {
return new Date(getTimeInMillis());
}
Following the bouncing ball...
public long getTimeInMillis() {
if (!isTimeSet) {
updateTime();
}
return time;
}
private void updateTime() {
computeTime();
// The areFieldsSet and areAllFieldsSet values are no longer
// controlled here (as of 1.5).
isTimeSet = true;
}
But I don't have the source for computTime()
so that's where I stopped.
Gabriel's answer works, fwiw.
Upvotes: 3
Reputation: 566
Calendar.getTime() returns a java Date instance which represents the number of milliseconds from the epoch January 1, 1970, 00:00:00 GMT. Calling toString() on a Date instance will print the date based on the timezone configured on the server. To properly format a date for output in another timezone, you'll want to look at using a date formatter.
TimeZone est = TimeZone.getTimeZone("America/New_York");
Calendar cal = Calendar.getInstance(est);
DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
formatter.setTimeZone(est);
System.out.println(formatter.format(cal.getTime()));
Upvotes: 1
Reputation: 1249
Try this:
TimeZone.setDefault(TimeZone.getTimeZone("PST"));
Calendar cl = Calendar.getInstance();
Upvotes: 3