日历返回错误时区的日期 [英] Calendar returns date in wrong time zone

查看:27
本文介绍了日历返回错误时区的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 GMT 时区的计算机上执行以下操作

If I execute the following on a computer in the GMT timezone

TimeZone timeZone = TimeZone.getTimeZone('IST');  // India Standard Time
Calendar calendar = Calendar.getInstance(timeZone);

System.out.println(calendar.getTime());

打印

格林威治标准时间 2014 年 10 月 31 日星期五 15:18:22

Fri Oct 31 15:18:22 GMT 2014

为什么在计算机的默认时区中打印日期,而不是在 Calendar 构建时使用的 TimeZone?

Why is the date printed in the computer's default time zone, rather than the TimeZone the Calendar was constructed with?

推荐答案

因为 Date 对象没有将时区作为其状态的一部分,您需要 SimpleDateFormat 来格式化和打印所需的日期时区

because Date object doesn't have timezone as part of its state, You need SimpleDateFormat to format and print the date in your required timezone

这篇关于日历返回错误时区的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆