1

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

onkar
  • 19
  • 1
  • 6
  • Also look at this question and answers: http://stackoverflow.com/questions/2689379/how-to-get-a-list-of-dates-between-two-dates-in-java?rq=1 Joda Time could help you in this case. – Averroes Sep 24 '12 at 12:03
  • This was answered here http://stackoverflow.com/questions/5351483/calculate-date-time-difference-in-java . You get the result 0, because the seconds in your both defined dates is 00. – Nejc Sep 24 '12 at 12:03
  • replace getSeconds() with getTime() – V.J. Sep 24 '12 at 12:10
  • Friends,I have updated the question with the bold content. Please go through it. – onkar Sep 24 '12 at 12:21

5 Answers5

1

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().

JHS
  • 7,761
  • 2
  • 29
  • 53
1

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

Community
  • 1
  • 1
Jram
  • 26
  • 3
  • Thanks mate :) Hey suppose if I enter 31st Aug 23:59:00 and next date 1 sept 00:02:00 , I need to show the reocrd as 1 day. Please help me for this one – onkar Sep 24 '12 at 12:13
  • There are several options, you can use getDay() to get the "number" of the day. So in your example you would obtain 31 and 1. Just compare them to now if they are different. – Jram Sep 25 '12 at 12:27
  • getDay() gives the number of day of current month – onkar Sep 25 '12 at 12:45
  • Check the difference (in days) with the first method, and if the result is 0, check also the days. – Jram Sep 25 '12 at 13:07
0

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.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
0

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 :)

Manoj Kumar
  • 1,510
  • 3
  • 20
  • 40
0

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();

    }
}
Yuvi
  • 639
  • 6
  • 9
  • 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 – onkar Sep 25 '12 at 10:31
  • simple dude, just check seconds you get if it greater than 1 then display as 1 day. – Yuvi Sep 28 '12 at 09:46
  • wat about 31st Aug 23:59:00 and next date 31st Aug 23:59:02? – onkar Sep 28 '12 at 10:34