开发者

Why does org.joda.time.DateTimeFormatter not parse custom date time with millis correctly?

开发者 https://www.devze.com 2023-04-05 03:36 出处:网络
I have a custom formatter... // default our time zone to开发者_运维技巧 the machine local one. private static final DateTimeZone LOCAL_TZ = DateTimeZone.getDefault();

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?


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...)

0

精彩评论

暂无评论...
验证码 换一张
取 消