转换给定时区的日期/时间 - java [英] Convert Date/Time for given Timezone - java

查看:150
本文介绍了转换给定时区的日期/时间 - java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要把这个GMT时间戳转换成GMT + 13:

I want to convert this GMT time stamp to GMT+13:

2011-10-06 03:35:05

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

I have tried about 100 different combinations of DateFormat, TimeZone, Date, GregorianCalendar etc. to try to do this VERY basic task.

这段代码为CURRENT TIME做了什么:

This code does what I want for the 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());

但是我想要的是设置时间,而不是使用当前时间。

But what I want is to set the time rather then using the current time.

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

I found that anytime I try to set the time like this:

calendar.setTime(new Date(1317816735000L));

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

the local machine's TimeZone is used. Why is that? I know that when "new Date()" returns UTC+0 time so why when you set the Time in milliseconds does it no longer assume the time is in UTC?

可以:


  1. 设置对象上的时间(日历/日期/时间戳)

  2. (可能)设置初始时间戳的时区(calendar.setTimeZone(...))

  3. 使用新的TimeZone格式化时间戳(formatter.setTimeZone(...)) )

  4. 返回一个新时区的字符串。 (formatter.format(calendar.getTime()))

  1. Set the time on an object (Calendar/Date/TimeStamp)
  2. (Possibly) Set the TimeZone of the initial time stamp (calendar.setTimeZone(...))
  3. Format the time stamp with a new TimeZone (formatter.setTimeZone(...)))
  4. Return a string with new time zone time. (formatter.format(calendar.getTime()))

提前感谢任何帮助:D

Thanks in advance for any help :D

推荐答案

了解计算机时间的工作原理非常重要。有了这个说法,我同意如果创建一个API来帮助您像实时处理计算机时间,那么它应该以这样一种方式工作,即可实时处理它。在大多数情况下,这是这种情况,但有一些主要的疏忽,需要注意。

Understanding how computer time works is very important. With that said I agree that if an API is created to help you process computer time like real time then it should work in such a way that allows you to treat it like real time. For the most part this is the case but there are some major oversights which do need attention.

无论如何我离题!如果您有UTC偏移量(比UTC偏移更好地在UTC工作),您可以计算以毫秒为单位的时间,并将其添加到您的时间戳。请注意,SQL时间戳可能会与Java时间戳不同,因为计算从时代过去的方式并不总是相同的 - 取决于数据库技术和操作系统。

Anyway I digress!! If you have your UTC offset (better to work in UTC than GMT offsets) you can calculate the time in milliseconds and add that to your timestamp. Note that an SQL Timestamp may vary from a Java timestamp as the way the elapse from the epoch is calculated is not always the same - dependant on database technologies and also operating systems.

我建议您使用System.currentTimeMillis()作为您的时间戳,因为这些可以在Java中更一致地处理,而不用担心将SQL时间戳转换为java Date对象等。

I would advise you to use System.currentTimeMillis() as your time stamps as these can be processed more consistently in java without worrying about converting SQL Timestamps to java Date objects etc.

要计算您的偏移量,可以尝试以下方式:

To calculate your offset you can try something like this:

Long gmtTime =1317951113613L; // 2.32pm NZDT
Long timezoneAlteredTime = 0L;

if (offset != 0L) {
    int multiplier = (offset*60)*(60*1000);
    timezoneAlteredTime = gmtTime + multiplier;
} else {
    timezoneAlteredTime = gmtTime;
}

Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(timezoneAlteredTime);

DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");

formatter.setCalendar(calendar);
formatter.setTimeZone(TimeZone.getTimeZone(timeZone));

String newZealandTime = formatter.format(calendar.getTime());

我希望这是有帮助的!

这篇关于转换给定时区的日期/时间 - java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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