不是一个优雅的解决方案,但它适用于我们。我不得不为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;
}
本文介绍了一个针对应用中SimpleDateFormat不适用的情况,作者创建了一个自定义的OurOwnCustomDateFormat类,通过重写format方法以适应特定时区需求。这个解决方案虽然不优雅,但在实际场景中解决了日期格式化的问题。
1772

被折叠的 条评论
为什么被折叠?



