简体   繁体   English

如何使用时区偏移量将日期转换为时间?

[英]How to convert a date to time with timezone offset?

How can I print a Date into format HH:mm+-GMT using SimpleDateFormat ? 如何使用SimpleDateFormatDate打印为HH:mm+-GMT格式?

"EEE, d MMM yyyy HH:mm:ss Z"    Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ"                 010704120856-0700

How could I just print: 13:01-7 for the above example? 我如何才能打印:上面的示例为13:01-7 So, the timezone offset to be represented with the shortest possible value? 那么,以尽可能短的值表示的时区偏移量呢?

If you are on Java 7, you could use the X flag to get the timezone. 如果您使用的是Java 7,则可以使用X标志获取时区。

Java 7 SimpleDateFormat javadoc: Java 7 SimpleDateFormat javadoc:

X Time zone ISO 8601 time zone -08; X时区ISO 8601时区-08; -0800; -0800; -08:00 -08:00

Try below: 请尝试以下方法:

import java.util.Date;
import java.text.SimpleDateFormat;

class array05{
  public static void main(String[] args){
      SimpleDateFormat sf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
      System.out.println(sf.format(new Date()));
  }
}

For me it have been printing Вт, 12 ноя 2013 14:22:24 +0200 对我来说,它一直在打印Вт, 12 ноя 2013 14:22:24 +0200

Understanding how computer time is 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. 话虽如此,我同意,如果创建了一个API来帮助您像实时一样处理计算机时间,那么它应该以一种允许您将其视为实时的方式工作。 For the most part this is the case but there are some major oversights which do need attention. 在大多数情况下都是这种情况,但确实需要注意一些重大疏忽。

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. 如果您有UTC偏移量(比GMT偏移量更适合在UTC中使用),则可以以毫秒为单位计算时间并将其添加到时间戳中。 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. 请注意,SQL时间戳记可能与Java时间戳记有所不同,因为从历元开始算起的计算方式并不总是相同-取决于数据库技术以及操作系统。

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

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

I hope this is helpful! 我希望这是有帮助的!

Try this, the shortest possible as you quoted in question, 尝试一下,尽量避免出现问题,

long millis = new Date().getTime();
System.out.println(SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT).format(millis));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有时区偏移的情况下将日期转换为毫秒? - How to convert date to milliseconds with out timezone offset? 如何使用java-8将UTC秒的时间和秒的时区偏移量转换为日期? - How to convert time in utc second and timezone offset in second to date using java-8? 如何将日期和时间转换为用户时区 - How to convert date and time to user timezone 将日期从 GMT 时区转换为本地时区 -- 使用 ISO_OFFSET_DATE_TIME - Convert date from GMT timezone to local time zone -- using ISO_OFFSET_DATE_TIME 将日期和时间转换为选定的时区 - convert date and time into selected timezone 如何在freemarker中将带有区域偏移的日期时间字符串转换为本地时间? - How to convert date time string with zone offset to local time in freemarker? 如何将日期/时间从一个时区转换为另一个时区? - How do I convert date/time from one timezone to another? 如何将 DateTime 保存为包含时区偏移量的日期? - How to save DateTime as Date with TimeZone offset included? 如何转换具有2位时区偏移量的日历? - How to convert Calendar with timezone offset of 2 digits? 如何将GMT的偏移量转换为本地时区? - How to convert an offset from GMT to the local timezone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM