Java(GMT + 00:00)中没有针对自定义时区的时区偏移量的SimpleDateFormat

安德兹:

是否可以使用SimpleDateFormat类在Java中格式化日期时间,以提供日期的时区部分,而不在日期后加上+0000。

编辑

我们正在Java中更改默认时区,如下所示:

SimpleTimeZone tz = new SimpleTimeZone(0, "Out Timezone");        
TimeZone.setDefault(tz);

不幸的是,我无权删除此代码。我会猜测整个系统停止工作。我认为最初的作者将其用于解决某些日光节约问题。

考虑到这一点,我想将日期格式化为:

2011-12-27 09:00 GMT

要么

2011-12-27 09:00 BST

我只能将SimpleDateFormat输出为:

2011-12-27 09:00:00 GMT + 00:00

使用格式字符串yyyy-MM-dd HH:mm:ss z

我看不到任何简单时区都引用冬令时间(GMT)或夏令时间(BST)的地方。

安德兹:

根本不是一个优雅的解决方案,但对我们有用。我必须为DateFormat / SimpleDateFormat创建一个自定义实现。看起来如下所示:

static {
    // this would be initialized like something as follows when the application starts
    // which causes the headaches of SimpleDateFormat not to work...
    SimpleTimeZone tz = new SimpleTimeZone(0, "Out Timezone");             
    TimeZone.setDefault(tz);  
}

// therefore this class will workaround the issue, 

public class OurOwnCustomDateFormat
    extends SimpleDateFormat {

    /** The pattern to use as the format string. */
    protected String pattern;

    public OurOwnCustomDateFormat(String pattern) {
         super(pattern);
         // store the pattern
         this.pattern = pattern;
    }

    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) {

         // custom implementation to format the date and time based on our TimeZone            
         toAppendTo.insert(pos.getBeginIndex(), "the date with our custom format calculated here");
         return toAppendTo; 
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Java中没有为几个时区设置偏移量

Java SimpleDateFormat时区偏移量,分钟用冒号分隔

如何从Java中的时区偏移量获取时区?

GMT中的Java时间

Linux Shell脚本中GMT的给定时区偏移量

为什么时区模式“○○○○”不显示完整的GMT + 00:00偏移格式?

Java日期格式-GMT +04:00

为什么对同一时区中的某些时间戳而言,矩时区显示不正确的GMT偏移量?

php根据给定的GMT偏移量设置我自己的时区

时区偏移量的java.time DateTimeFormatter模式

如何在 Java 中更改 Date 对象的时区偏移量?

将时区偏移量“+00:00”转换为分钟

Java DateTimeFormatter:具有GMT日期的DateTimeParseException

Java 8 DateTimeFormatter无法解析“ Etc / GMT”时区

使用Java将当前GMT时间转换为CST时区

Java 设置时区不默认为 gmt+0

强制将Java时区设置为GMT / UTC

java lucene自定义分析器和令牌生成器在termvector偏移量中创建问题?

是否可以使用 GMT 偏移量作为 moment.js 中的时区将时间戳转换为日期和时间?

GMT / UTC以给定时区的小时/分钟为单位的偏移量,不含夏令时(DST)

ParseException:无法解析的日期:“ 2016年3月30日星期三00:00:00 GMT + 05:30”(偏移量4)

如何在Elm中以GMT显示当前时区偏移

在Swift中获取时区(GMT)

在JavaScript中获取客户的GMT偏移量

从gmt偏移量转换PHP时间

Rest API 的 Java 日期如下: Mon, 01 Jan 1990 00:00:00 GMT

了解Etc / GMT时区

python时区GMT转换

PHP时区GMT转换