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 毫秒,因此 SSS 将为 020 毫秒。
S = 秒的小数部分。
将给出 SSS = 200 毫秒
新解释背后的基本原理是,对于微秒和纳秒,2 ms 作为 ,2
并不真正适合 ,SSSSSS
- 正如您所说。
关于java - 为什么Java时间戳解析在毫秒部分前缀0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59682914/