Skip to content

Not able to get difference between two datetime?

Please check the below code. I am trying to get the difference but every time getting 0. Can anybody please point me what is the problem with below code?

SimpleDateFormat sDateFormat = new SimpleDateFormat("hh:mm:ss dd/mm/yyyy");
try {

    long d1 = sDateFormat.parse("10:04:00 04/04/2014").getTime();
    long d2 = sDateFormat.parse("10:09:00 04/04/2014").getTime();

    long difference = d2 - d1;

    Log.i(TAG,">> Difference = "+difference);

} catch (ParseException e) {
    e.printStackTrace();
}

Answer

your formatter does not fit the date format used.

Try:

new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");