fork download
  1. import java.text.SimpleDateFormat;
  2. import java.text.ParseException;
  3. import java.time.Duration;
  4. import java.util.Date;
  5.  
  6. class Ideone {
  7. public static void main(String[] args) throws java.lang.Exception {
  8. new Ideone().calTime("01:18:19.92", "01:18:19.57");
  9. }
  10.  
  11. public void calTime(String A, String B) {
  12. String sFormat = "HH:mm:ss.SSS";
  13. SimpleDateFormat dFormat = new SimpleDateFormat(sFormat);
  14.  
  15. try {
  16.  
  17. Date aDate = dFormat.parse(A);
  18. Date bDate = dFormat.parse(B);
  19.  
  20. long interval = aDate.getTime() - bDate.getTime();
  21. System.out.println("interval in ms: " + interval);
  22. Duration d = Duration.ofMillis(interval);
  23. System.out.println("duration: " + d);
  24.  
  25. } catch (ParseException ex) {
  26. System.out.println("Failed to parse");
  27. }
  28. }
  29. }
Success #stdin #stdout 0.14s 321472KB
stdin
Standard input is empty
stdout
interval in ms: 35
duration: PT0.035S