3
Calendar cl =  Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
cl.setTimeInMillis(time); 
Toast.makeText(getActivity(), cl.getTime().toString(), Toast.LENGTH_LONG).show();

无论何时我把它总是返回美国东部时间的时间。我希望它返回 PST 的时间。有谁知道这可能是错误的?

PS
我的当地时间是美国东部时间。

4

3 回答 3

3

这不是一个真正的答案,但它太大而无法发表评论。这就是我发现的。

我的测试看起来像这样

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

这给出了输出

Pacific Standard Time
Time = Tue Nov 05 11:36:33 EST 2013
Manually = Tue Nov 8:36:33:238 Pacific Standard Time 2013

看着Calendar.getTime()

public final Date getTime() {
    return new Date(getTimeInMillis());
}

随着弹跳球...

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;
}

但我没有来源,computTime()所以这就是我停下来的地方。

加布里埃尔的回答有效,fwiw。

于 2013-11-05T16:41:22.937 回答
3

尝试这个:

TimeZone.setDefault(TimeZone.getTimeZone("PST"));
Calendar cl = Calendar.getInstance();
于 2013-11-05T16:16:52.330 回答
1

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()));    
于 2013-11-05T16:22:02.617 回答