[英]Why Java timestamp parsing is prefixing 0 in millisecond part?
final String OLD_FORMAT = "mm:ss.SS";
final String TARGET_FORMAT = "HH:mm:ss,SSS";
String timeInputStr="00:17.20"; // input is always in mm.ss.SS m--> minutes, ss-> seconds , and SSS is decimal fraction of seconds always , not actual milliseconds
String timeOutputStr="";
Date d=new Date();
DateFormat formatter= new SimpleDateFormat(OLD_FORMAT);
DateFormat nformatter= new SimpleDateFormat(TARGET_FORMAT);
try{
d = formatter.parse(timeInputStr);
}
catch (ParseException e){
System.out.println("Can't Parse date "+d + " from: " +lrcTime );
}
timeInputStr=nformatter.format(d);
System.out.println( "For Input String: " + lrcTime + " -> Parsed date "+ formatter.format(d) + "-> will print as to " + timeInputStr);
return timeOutputStr;
它给了我以下输出:
For Input String: 00:17.20 -> Parsed date 00:17.20-> will print as to 00:00:17,020
但我想解析这样的字符串00:00:17,200
我错过了什么?
在较新的日期时间类中改进了歧义:
S = 毫秒
SS 为 20 表示 20 ms,因此 SSS 将为 020 ms。
S = 秒的分数。
会给 SSS = 200 ms
新解释背后的基本原理是,对于微秒和纳秒,2 ms as ,2
并不真正适合,SSSSSS
- 正如您所说的那样。
格式化程序将.20
解释为 20 毫秒,而不是 0.2 秒(即 200 毫秒)。 要解决此问题,您只需在字符串中添加一个零即可。
d = formatter.parse(lrcTime + "0");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.