为什么 org.joda.time.DateTimeFormatter 不能正确解析自定义日期时间与毫秒?

发布于 2024-12-04 12:36:59 字数 1018 浏览 1 评论 0原文

我有一个自定义格式化程序...

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找一个思念的角度 2024-12-11 12:36:59

您说过现在是 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...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文