Get the time difference in android?

In my Android app, I get the date and time in the following format on an HTTP request.

Tuesday April 17 16:23:33 IST 2012

Now I want to calculate the time difference between this time and the current time. I have many ways to calculate the time difference on the Internet, but all these solutions are in different formats. How can I calculate the time difference with an earlier time

Edit: This is the code I used to work it.

  DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
  Date dateOne = df.parse("Tue apr 22 13:07:32 IST 2012");
  Date dateTwo = df.parse("Tue Apr 22 13:07:33 IST 2012");   
  long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
  System.out.println("difference:" + timeDiff); //differencs in ms

but I have to accept the current time in the format "Tue apr 22 13:07:32 IST 2012" for use as dateTwo.

+3
source share
5 answers

you can also use this code:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
Date dateOne = df.parse("2011-02-08 10:00:00 +0300");
Date dateTwo = df.parse("2011-02-08 08:00:00 +0100");   
long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
System.out.println("difference:" + timeDiff);   // difference: 0
+6
source

Date pdate = /* your date comming from HTTP */
Date cdate = new Date(System.currentTimeMillis());

long difference = cdate.getTime() - pdate.getTime();
+1

SimpleDateFormat(String pattern), , .

parse(String) . Date. Date.getDateInstance().getTime(), .

- .

+1

, .

String format = "Tue Apr 17 16:23:33 IST 2012";
    int month = 0;

    Calendar yourDate = Calendar.getInstance();

    String[] months = new String[] { "Jan", "Feb", "Mar", "Apr", "May",
            "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    String datArr[] = format.split(" ");

    String[] time = datArr[3].split(":");

    for (int i = 0; i < months.length; i++) {
        if (months[i] == datArr[1]) {
            month = i;
        }
    }

    yourDate.set(Integer.parseInt(datArr[5]), month, Integer.parseInt(datArr[2]), Integer.parseInt(time[0]),
            Integer.parseInt(time[1]));

    Log.i("Your Date= ", yourDate.toString());
    Log.i("Now Date= ",Calendar.getInstance().toString());

, .

0

, , , , , , .

: DateTimeDifference GitHub Link

long currentTime = System.currentTimeMillis();
long previousTime = (System.currentTimeMillis() - 864000000); //10 days ago

Log.d("DateTime: ", "Difference With Second: " + AppUtility.DateTimeDifference(currentTime, previousTime, AppUtility.TimeDifference.SECOND));
Log.d("DateTime: ", "Difference With Minute: " + AppUtility.DateTimeDifference(currentTime, previousTime, AppUtility.TimeDifference.MINUTE));
if(AppUtility.DateTimeDifference(currentTime, previousTime, AppUtility.TimeDifference.MINUTE) > 100){
    Log.d("DateTime: ", "There are more than 100 minutes difference between two dates.");
}else{
    Log.d("DateTime: ", "There are no more than 100 minutes difference between two dates.");
}
0

All Articles