1 java parsing date simpledateformat
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;
Run Code Online (Sandbox Code Playgroud)
它给了我以下输出:
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;
Run Code Online (Sandbox Code Playgroud)
但我想解析这样的字符串 00:00:17,200
我错过了什么?
在较新的日期时间类中改进了歧义:
S = 毫秒
SS 为 20 表示 20 ms,因此 SSS 将为 020 ms。
S = 秒的分数。
会给 SSS = 200 毫秒
新解释背后的基本原理是,微秒和纳秒 2 毫秒,2并不真正适合诸如,SSSSSS- 正如您所说的那样。