在Java中解析微秒的时间 [英] Parse time with microseconds in Java
问题描述
我在使用Java解析时间字符串时遇到问题,格式为 2013-01-09 09:15:03.000000
。在我的数据中,最后三位数始终为0(意味着输入字符串只有毫秒精度),所以我将此格式传递给SimpleDateFormat:
I am having problems parsing time strings in Java that are in the format of 2013-01-09 09:15:03.000000
. In my data, the last three digits are always 0 (meaning the input strings have only millisecond precision), so I passed this format to SimpleDateFormat:
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'000'");
但 formatter.parse(2013-01-09 09:15: 02.500000);
抛出异常:
Unparseable date: "2013-01-09 09:15:02.500000"
at java.text.DateFormat.parse(DateFormat.java:357)
任何人都知道如何正确地做到这一点?我可以使用格式 yyyy-MM-dd HH:mm:ss.SSS
并使用 substring
来获取摆脱最后三位数,但这真的很笨。
Anyone knows how to do it correctly? I can work around by using format yyyy-MM-dd HH:mm:ss.SSS
and using substring
to get rid of last three digits but that's really hacky.
编辑:任何人都可以解释为什么格式字符串 yyyy-MM-dd HH:mm:ss .SSS'000'
不能用于解析时间2013-01-09 09:15:02.500000
can anyone explain why the format string yyyy-MM-dd HH:mm:ss.SSS'000'
can't be used to parse time "2013-01-09 09:15:02.500000"
推荐答案
我已经弄明白了。仅供参考,Apache commons' FastDateFormat
似乎接受 SSS000
格式并正确解析时间。
I've figured out myself. Just FYI, Apache commons' FastDateFormat
seems accepting the SSS000
format and parses the time correctly.
这篇关于在Java中解析微秒的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!