java dateformat gmt_没有Java时区偏移的SimpleDateFormat(自定义时区的GMT + 00:00))

本文介绍了一个针对应用中SimpleDateFormat不适用的情况,作者创建了一个自定义的OurOwnCustomDateFormat类,通过重写format方法以适应特定时区需求。这个解决方案虽然不优雅,但在实际场景中解决了日期格式化的问题。

不是一个优雅的解决方案,但它适用于我们。我不得不为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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值