相关疑难解决方法(0)

转换给定时区的日期/时间 - java

我想将此GMT时间戳转换为GMT + 13:

2011-10-06 03:35:05
Run Code Online (Sandbox Code Playgroud)

我尝试过大约100种不同的DateFormat,TimeZone,Date,GregorianCalendar等组合来尝试做这个非常基本的任务.

这段代码完成了我对CURRENT TIME的要求:

Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));

DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");    
formatter.setTimeZone(TimeZone.getTimeZone("GMT+13"));  

String newZealandTime = formatter.format(calendar.getTime());
Run Code Online (Sandbox Code Playgroud)

但我想要的是设定时间而不是使用当前时间.

我发现任何时候我都尝试设置这样的时间:

calendar.setTime(new Date(1317816735000L));
Run Code Online (Sandbox Code Playgroud)

使用本地机器的TimeZone.这是为什么?我知道当"new Date()"返回UTC + 0时,为什么当你设置Time以毫秒为单位时,它不再假设时间是UTC?

有可能:

  1. 设置对象的时间(日历/日期/时间标准)
  2. (可能)设置初始时间戳的时区(calendar.setTimeZone(...))
  3. 使用新的TimeZone格式化时间戳(formatter.setTimeZone(...)))
  4. 返回具有新时区时间的字符串.(formatter.format(calendar.getTime()))

在此先感谢您的帮助:D

java formatting timezone datetime timestamp

63
推荐指数
6
解决办法
28万
查看次数

标签 统计

datetime ×1

formatting ×1

java ×1

timestamp ×1

timezone ×1