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;
It's giving me the following output:
For Input String: 00:17.20 -> Parsed date 00:17.20-> will print as to 00:00:17,020
But I want to parse such string as 00:00:17,200
What am I missing?
There is an ambiguity that is improved in the newer date time classes:
The old SimpleDateFormat
S = millisecond
SS with 20 means 20 ms, hence SSS will be 020 ms.
The newer DateTimeFormatter
S = fraction of second.
Will give SSS = 200 ms
The rationale behind the new interpretation is that with microseconds and nanoseconds a 2 ms as ,2
does not really fit with things like ,SSSSSS
- as you remarked.
The formatter interprets the .20
as 20 milliseconds, not .2 seconds (which would be 200ms). To resolve this issue, you can simply add a zero to your String.
d = formatter.parse(lrcTime + "0");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.