java - How to subtract start time from stop time -


I have the following two dates in a string format and want to get the time spent for those people.

date = new date (new simpleadform () .pars ("10/17/2014, 2:19:22 pm"). GetTime () - New SimpleDetermate () .pars ("10/17/2014, 2:19:32 PM"). GetTime ()); System.out.println ("Elapsed time:" + spent);

However, I get the ... parse exception: uneven date: "10/17/2014, 2:19:22 pm" I know?

Note: It is here that the date you created was generated by the parsing block new Simple Format ("yyyy-MM-dd HH: mm: ss"). Format (time)

  • You SimpleDateFormat constructor Must specify the format in.
  • Your string in format dd / MM / yyyy, hh: mm: ss a
  • Your argument for subtraction is incorrect because it will give you the difference of long value between date and you are making the date from the value that is not valid . For this, you have to get the exact difference from that long price. You have to divide by diff (1000 * 60) for minutes, (1000 * 60 * 60) seconds and (1000 * 60 * 60 * 24) Get the day to get it.

      SimpleDateFormat sdf = new SimpleDateFormat ("d / dm / yyyy, hh: mm: ss a"); Long gap = sdf.parse ("10/17/2014, 2:19:22 pm"). GetTime () - sdf.parse ("10/17/2014, 2:18:32 PM"). GetTime (); System.out.println ("Inter:" + (diff / (1000d * 60) "min");      

    Note: 1000d is used to make floating point arithmetic is.


Comments