我的时间戳看起来像这样:
2015-03-21T11:08:14.859831
2015-03-21T11:07:22.956087
我读了Wiki article on ISO 8601,但没有得到最后6位的含义.
我尝试使用“yyyy-MM-dd’T’HH:mm:ss.sss”或“yyyy-MM-dd’T’HH:mm:ss.ssssss”将其降低到毫秒.它是否比毫秒更精确 – 高达几微秒?
解决方法:
Is it just more precise than milliseconds?
是的,在这种情况下,它是微秒.
ISO-8601实际上并未指定最大精度.它指出:
If necessary for a particular application a decimal fraction of hour, minute or second may be included. If a decimal fraction is included, lower order time elements (if any) shall be omitted and the decimal fraction shall be divided from the integer part by the decimal sign specified in ISO 31-0, i.e. the comma [,] or full stop [.]. Of these, the comma is the preferred sign. If the magnitude of the number is less than unity, the decimal sign shall be preceded by two zeros in accordance with 3.6.
The interchange parties, dependent upon the application, shall agree the number of digits in the decimal fraction. […]
(你很少将逗号视为小数分隔符 – 至少,这是我的经验.)
不幸的是,根据我的经验,在Java 7中解析这样的值很棘手 – 没有“只消耗小数位并做正确的事情”的格式说明符.您可能会发现在解析之前需要手动切断尾随的3位数字,以毫秒为单位.
由于Java 8支持的精度为纳秒,因此它更加简单 – 事实上,内置的ISO格式化程序可以解析它:
import java.time.*;
import java.time.format.*;
public class Test {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
System.out.println(LocalDateTime.parse("2015-03-21T11:07:22.956087", formatter));
}
}