0

I am working on android application where i am getting date from server like : Thu Jul 02 14:18:58 GMT+04:00 2015 and after parsing it through SimpleDateFormat i am getting it like: Thursday , July 02 , 2015 02:18 PM. And through Calender.getTime() i am getting the time Thu Jul 02 14:57:25 GMT+04:00 2015 and after parsing it i am getting Thursday , July 02 , 2015 02:57 PM. Now i just need to take the time difference between two date and times. I was doing like the following thing but it is giving me NumberFormat Exception. Please help me out here.

        Calendar cal = Calendar.getInstance();
        cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE , MMMM dd , yyyy hh:mm a");
        String systemTime = sdf.format(cal.getTime());
        int lastLocationSyncInt = Integer.parseInt(lastLocationSync);
        int systemTimeInt = Integer.parseInt(systemTime);
        int lastSeenTimeDifference =  systemTimeInt -lastLocationSyncInt ;
        mMap.addMarker(markerOptions).setSnippet(lastSeenTimeDifference+"");
android_explorer
  • 345
  • 1
  • 3
  • 10
  • possible duplicate of [Calculate elapsed time in Java / Groovy](http://stackoverflow.com/questions/567659/calculate-elapsed-time-in-java-groovy) – Basil Bourque Jul 02 '15 at 21:13

1 Answers1

1

Instead of converting into Integer, format in date and get the difference as like below

long days = 0;
        long hours = 0;
        SimpleDateFormat sourceFormat = new SimpleDateFormat(
                "EEEE , MMMM dd , yyyy hh:mm a");
        Date sDate = null;
        Date sEdate = null;
        try {
            sDate = sourceFormat.parse(startdate);
            sEdate = sourceFormat.parse(enddate);

            long diff = sDate.getTime() - sEdate.getTime();
            long seconds = diff / 1000;
            long minutes = seconds / 60;
            hours = minutes / 60;
            days = hours / 24;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

if you have any doubt feel free to comment

Madhu
  • 1,780
  • 23
  • 47