0

I am calculating time difference between two dates and time but its restuning invalid difference.

here's my sample date and code to calculate the difference.

loginTime=2016-01-24 12:04:30.16
expiryTime = 2016-01-24 13:04:30.16

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date now = new Date();


        try {
            Date startDate = simpleDateFormat.parse(loginTime);
            Date expireDate = simpleDateFormat.parse(expiryTime);

//            String temCurrentDate = new SimpleDateFormat("yyyy-MM-dd").format(now);
//            Date currentDate = new SimpleDateFormat("yyyy-MM-dd").parse(temCurrentDate);
//            String day_of_week = new SimpleDateFormat("EEEE").format(now);

            long difference =startDate.getTime()- expireDate.getTime();

            int hour = (int) difference / (60 * 60 * 1000);


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

Difference between this time is 1 hour but i am getting 11 or 13. Please tell where i am going wrong in this.

Shivam Nagpal
  • 763
  • 2
  • 9
  • 21
  • Possible duplicate of [Calculate Difference between two times in Android](http://stackoverflow.com/questions/14110621/calculate-difference-between-two-times-in-android) – Pankaj Jan 24 '16 at 12:28

1 Answers1

0

Spelling error in your variable:

simpleDateFormat.parse(expiryTime);

Does not match: ExpiryTime = 2016-01-24 13:04:30.16

That's the whole problem.

durbnpoisn
  • 4,666
  • 2
  • 16
  • 30