onkar
onkar

Reputation: 19

How to get difference between two dates

I have to calculate the difference between two dates. Here is my code:

        System.out.println("date1  "+date1);
        System.out.println("date2  "+date2);

        totalDifference=date2.getSeconds()-date1.getSeconds();

        System.out.println("Total Difference is"+totalDifference);

The problem that I am facing is that the result is 0:

 09-24 17:24:53.839: I/System.out(9317): date1  Wed Aug 15 00:00:00 GMT+05:30 2012
 09-24 17:24:53.839: I/System.out(9317): date2  Mon Sep 24 17:24:00 GMT+05:30 2012
 09-24 17:24:53.839: I/System.out(9317): Total Difference is 0

What I am doing wrong?

Thanks guys for the solution and other valuable resources,Please help me for this question also. Hey suppose if I enter 31st Aug 23:59:00 and next date 1 Sept 00:02:00 , I need to show the record as 1 day. Please help me for this one

Upvotes: 1

Views: 3403

Answers (5)

Yuvi
Yuvi

Reputation: 689

Just see the code below an make small changes according to you.

package com.datediff;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class DateDiffActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Calendar thatDay = Calendar.getInstance();
    thatDay.set(Calendar.DAY_OF_MONTH,24);
    thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
    thatDay.set(Calendar.YEAR, 2012);

    Calendar today = Calendar.getInstance();

    long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); 

    Toast.makeText(getApplicationContext(), diff+"", Toast.LENGTH_LONG).show();

    }
}

Upvotes: 0

Jram
Jram

Reputation: 26

getSeconds() return the second of the date, so in your examaple,

date1 Wed Aug 15 00:00:00 GMT+05:30 2012

date2 Mon Sep 24 17:24:00 GMT+05:30 2012

In both cases, the second of the date is 0. If you want the difference you have an example in

Android/Java - Date Difference in days

Upvotes: 1

Manoj Kumar
Manoj Kumar

Reputation: 1520

The best example for this scenario is given here. It contains something like

Calendar calendar1 = Calendar.getInstance();
  Calendar calendar2 = Calendar.getInstance();
  calendar1.set(2007, 01, 10);
  calendar2.set(2007, 07, 01);
  long milliseconds1 = calendar1.getTimeInMillis();
  long milliseconds2 = calendar2.getTimeInMillis();
  long diff = milliseconds2 - milliseconds1;
  long diffSeconds = diff / 1000;
  long diffMinutes = diff / (60 * 1000);
  long diffHours = diff / (60 * 60 * 1000);
  long diffDays = diff / (24 * 60 * 60 * 1000);
  System.out.println("\nThe Date Different Example");
  System.out.println("Time in milliseconds: " + diff
 + " milliseconds.");
  System.out.println("Time in seconds: " + diffSeconds
 + " seconds.");
  System.out.println("Time in minutes: " + diffMinutes 
+ " minutes.");
  System.out.println("Time in hours: " + diffHours 
+ " hours.");
  System.out.println("Time in days: " + diffDays 
+ " days.");

Try to google your problems initially :)

Upvotes: 0

Vikdor
Vikdor

Reputation: 24124

You could use getTime() method on java.util.Date to get the date in milliseconds and use that for finding out the difference between two java.util.Date variables, to get a more precise difference.

Upvotes: 0

JHS
JHS

Reputation: 7871

The date.getSeconds() method returns the seconds in that date object.

In your case both dates have 00 seconds so the difference is 0.

You would have to use date.getTime().

Upvotes: 1

Related Questions