首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java Calendar setTimeZone方法行为

Java Calendar setTimeZone方法行为
EN

Stack Overflow用户
提问于 2014-09-09 17:07:45
回答 2查看 525关注 0票数 1

当我使用java6在windows上运行下面的代码时,它会打印以下输出。

代码语言:javascript
运行
复制
private static SimpleDateFormat timeZoneFormatter = new SimpleDateFormat("Z");
public static void main(String[] args) {
    try {
        String format = "yyyyMMdd";

        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Date date1 = dateFormat.parse("20140330");
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);

        Date date2 = dateFormat.parse("20140401");

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        calendar1.setTimeZone(TimeZone.getTimeZone("GMT"));
        calendar2.setTimeZone(TimeZone.getTimeZone("GMT"));

        System.out.println(timeZoneFormatter.format(calendar1.getTime()));
        System.out.println(timeZoneFormatter.format(calendar2.getTime()));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
+0200
+0300

据我所知,无论我在calendar对象上设置什么时区,它都会接受SimpleDateFormat对象上的默认时区信息。我是不是漏掉了什么?如果这是正常行为,在什么情况下我们应该使用Calendar.setTimezone()方法?

EN

回答 2

Stack Overflow用户

发布于 2014-09-09 18:05:10

在您的示例中,时区设置无效的原因是:

对日历对象应用setTimeZone,则应对timeZoneFormatter对象应用setTimeZone

请参见下面我的可执行示例代码。此代码将演示时区的用法和影响。

代码语言:javascript
运行
复制
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimeZoneTEST {
    private static SimpleDateFormat timeZoneFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    public static void main(String[] args) {
        try {
            String timezone_GMT = "GMT";
            String timezone_NYC = "America/New_York";
            long currentLongTime = System.currentTimeMillis();

            // Effect of time zone when displaying time as hhmm
            System.out.format("Current time(HHMM) in [%s] time zone is [%d]\n", timezone_GMT, getCurrentTime(currentLongTime, timezone_GMT));
            System.out.format("Current time(HHMM) in [%s] time zone is [%d]\n", timezone_NYC, getCurrentTime(currentLongTime, timezone_NYC));

            // Effect of time zone when displaying the formatted string representation of time
            System.out.format("Current time(String representation) in [%s] time zone is [%s]\n", timezone_GMT, fmtDateWithTZ(currentLongTime,timezone_GMT));
            System.out.format("Current time(String representation) in [%s] time zone is [%s]\n", timezone_NYC, fmtDateWithTZ(currentLongTime,timezone_NYC));
            } catch (Exception e) {
        }
    }

    public static int getCurrentTime(long longTime, String timeZone){
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
        cal.setTimeInMillis(longTime);
        int hhmm = cal.get(Calendar.HOUR_OF_DAY) * 100 + cal.get(Calendar.MINUTE);
        return hhmm;    
    }

    public static String fmtDateWithTZ( long ms, String timeZone) {
        timeZoneFormatter.setTimeZone(TimeZone.getTimeZone(timeZone));
        return timeZoneFormatter.format( new Date( ms) );
    } 
}

运行代码后,用户应该会看到以下输出(实际显示的时间将是当前时间):

GMT时区的

当前时间为945时

美国/纽约时区的当前时间(HHMM)为545

GMT时区的当前时间(字符串表示)为2014-09-09 09:45:42 GMT

美国/纽约时区的当前时间(字符串表示)为2014-09-09 05:45:42美国东部时间

A备注,通过应用不同的时区,可以根据指定的时区以不同的方式显示相同的时间组件。这与时间的长时间表示法不同,长时间表示法测量自1970年1月1日午夜以来所经过的毫秒数。此数量与时区无关,其值不会随时区变化。

票数 4
EN

Stack Overflow用户

发布于 2014-09-09 18:10:38

在您的示例中,timeZoneFormatter没有格式化calendar1的时区,而是由calendar1.getTime()返回的匿名日期的时区。正在格式化的就是这个Date对象。由于日期没有时区,因此它将根据格式化程序的时区进行格式化,在本例中为默认时区。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25740680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档