为什么 org.joda.time.DateTimeFormatter 不能正确解析自定义日期时间与毫秒?
我有一个自定义格式化程序...
// default our time zone to the machine local one.
private static final DateTimeZone LOCAL_TZ = DateTimeZone.getDefault();
// format of date (i.e., timestamp) is yyyy-MM-dd HH:mm:ss.S
private static final DateTimeFormatter YEAR_MONTH_DAY_HOUR_MINUTE_SECOND_MILLIS_FORMATTER =
new DateTimeFormatterBuilder()
.appendYear(4,4)
.appendLiteral('-')
.appendMonthOfYear(1)
.appendLiteral('-')
.appendDayOfMonth(1)
.appendLiteral(' ')
.appendHourOfDay(2)
.appendLiteral(':')
.appendMinuteOfDay(1)
.appendLiteral(':')
.appendSecondOfDay(1)
.appendLiteral('.')
.appendMillisOfDay(1)
.toFormatter().withZone(LOCAL_TZ);
我做了类似的事情...
String value = "2011-06-21 05:00:00.0";
YEAR_MONTH_DAY_HOUR_MINUTE_SECOND_MILLIS_FORMATTER.parseDateTime(value);
如果我在调试器中查看 org.joda.time.DateTime,我会看到 hour:min:second.millis 已转换为 00:00: 00.0。
什么给?我尝试过在 HourOfDay、MinuteOfDay、SecondOfDay 等上使用 minDigits 。这是 JodaTime 2.0 中的错误吗?或者(更有可能)我自己的无知?
I have a custom formatter...
// default our time zone to the machine local one.
private static final DateTimeZone LOCAL_TZ = DateTimeZone.getDefault();
// format of date (i.e., timestamp) is yyyy-MM-dd HH:mm:ss.S
private static final DateTimeFormatter YEAR_MONTH_DAY_HOUR_MINUTE_SECOND_MILLIS_FORMATTER =
new DateTimeFormatterBuilder()
.appendYear(4,4)
.appendLiteral('-')
.appendMonthOfYear(1)
.appendLiteral('-')
.appendDayOfMonth(1)
.appendLiteral(' ')
.appendHourOfDay(2)
.appendLiteral(':')
.appendMinuteOfDay(1)
.appendLiteral(':')
.appendSecondOfDay(1)
.appendLiteral('.')
.appendMillisOfDay(1)
.toFormatter().withZone(LOCAL_TZ);
I do something like...
String value = "2011-06-21 05:00:00.0";
YEAR_MONTH_DAY_HOUR_MINUTE_SECOND_MILLIS_FORMATTER.parseDateTime(value);
If I look at the org.joda.time.DateTime in a debugger, I will see that the hour:minute:second.millis got converted to 00:00:00.0.
What gives? I've tried mucking around with minDigits on HourOfDay, MinuteOfDay, SecondOfDay and so on. Is this a bug in JodaTime 2.0? Or (more likely) my own ignorance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您说过现在是 0 毫秒一天。换句话说,这是一天中的第一毫秒。这显然与 5 小时相冲突,并且看起来最后指定的值优先。
我怀疑你想要
appendMillisOfSecond
- 已指定秒内的毫秒数。(如果您不清楚其中的区别,请告诉我。我最近一直在为 Noda Time 编写解析器和格式化程序,所以我的观点与大多数人有所不同......)
You've said that it's 0 millis of the day. In other words, it's the first millisecond of the day. That obviously conflicts with it being hour 5, and it looks like the last-specified value is taking precedence.
I suspect you want
appendMillisOfSecond
- the milliseconds within the already-specified second.(Let me know if the difference isn't clear to you. I've recently been writing the parsers and formatters for Noda Time, so I'm coming from a somewhat different perspective to most people...)