java - 为什么Java时间戳解析在毫秒部分前缀0?

标签 java parsing simpledateformat date

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

我错过了什么?

最佳答案

较新的日期时间类中存在歧义,已得到改进:

旧的SimpleDateFormat

  • S = 毫秒

    SS 为 20 表示 20 毫秒,因此 SSS 将为 020 毫秒。

较新的 DateTimeFormatter

  • S = 秒的小数部分。

    将给出 SSS = 200 毫秒

新解释背后的基本原理是,对于微秒和纳秒,2 ms 作为 ,2 并不真正适合 ,SSSSSS - 正如您所说。

关于java - 为什么Java时间戳解析在毫秒部分前缀0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59682914/

相关文章:

java - 如何将 Jpeg2000 (.jp2) 渲染到 ImageView?

c# - 解析 TSV 文件

java - 为什么我从 SimpleDateFormat 收到错误 "Unparseable date"?

java.text.ParseException : Unparseable date: "04 Mar 2016 20:33+05:30" (at offset 3) 异常

java - dateFormat 不包含解析的日期

java - 将日期从 PST 转换为 MST 时区

java - 将 lambda 替换为 contains 的方法引用

android - Android环境解析的500KB json文件

java - 如何阻止 Eclipse 卡在很长的自动完成列表上?

java - 如何在 Android 应用程序上使用 JSON 解析的 Activity 之间传递字符串?