SimpleDateFormat错误地分析了字符串 [英] SimpleDateFormat incorrectly parsing string

查看:76
本文介绍了SimpleDateFormat错误地分析了字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String s = 19.17.38.008000;
DateFormat f = new SimpleDateFormat("HH.mm.ss.SSSSSS");
Date d = f.parse(s);
system.out.println(d);

这是我正在运行的代码,运行正常,除非在打印时打印时间 19:17:46.请有人给我解释一下

this is the code I am running it runs fine except when it prints it prints the time 19:17:46. Please someone explain this to me

作为旁注:

String s = 19.17.38.008000;
DateFormat f = new SimpleDateFormat("HH.mm.ss");
Date d = f.parse(s);
system.out.println(d);

此代码将正确打印相同的字符串(减去毫秒).有人请告诉我我在这里想念的东西.

this code will print the same string correctly minus the milliseconds. Someone please tell me what I am missing here.

感谢您的回答,我认为这里的问题是我将38.008000读取为.008秒,但sdf却将SSS读取为8000毫秒.

Thanks for the answers I think the issue here is I was reading 38.008000 as .008 seconds but sdf is reading SSS as 8000 milliseconds which are not the same thing.

推荐答案

SSSSSS仍然是毫秒,即使您放置了6个也是如此. 19:17:38 + 008000 ms是19:17:46因此,如果令人惊讶,它会正确.

SSSSSS is still milli-seconds even if you put 6 of them. 19:17:38 + 008000 ms is 19:17:46 so it correct, if surprising.

AFAIK Java 8中的java.time库支持微秒(和纳秒)的时间戳.

AFAIK The java.time library in Java 8 supports micro-second (and nano-second) timestamps.

感谢@Meno.

这篇关于SimpleDateFormat错误地分析了字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆