是否可以使用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] 删除。
我来说两句