2

For someone else who might stumble here, the link refered to in this question gives misleading results

My First Date: 1986-04-08. Current Date: 2013-11-28.

Code:

public long seconds(Date date){

        String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",getResources().getConfiguration().locale).format(Calendar.getInstance().getTime());

        String DateStr=String.valueOf(formattedDate);
        Date d = null;
        try {
            d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",getResources().getConfiguration().locale).parse(DateStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        java.sql.Date dx = new java.sql.Date(d.getTime());
        Date d1 = date;
        Date d2 = dx;
        t4.setText("BirthDate"+date+"\n Current Date:"+dx);
        long seconds = (d2.getTime()-d1.getTime())/1000;
        return seconds;
    }

However when I check the results here: http://www.calculator.net/age-calculator.html?today=04%2F04%2F1986&ageat=11%2F28%2F2013&x=32&y=10 it gives me a slight different result. I am unsure where I am going wrong.

Skynet
  • 7,820
  • 5
  • 44
  • 80
  • In above second(Date date){} implementation you are getting diff in seconds. When u convert seconds into age difference you need to consider leap years between 1986 and 2003. Best way is to utilize Calendar class. – MAkS Nov 28 '13 at 11:24
  • Now I am confused one answer below says that this calculation is more precise, negating the daylight savings and all. And I am concerned about the leap years too now.. – Skynet Nov 28 '13 at 11:26
  • Check if Calender class does provide such method other wise right your own implementation. I guess day light saving should not effect your result. – MAkS Nov 28 '13 at 11:43

3 Answers3

4

The online service you link to is wrong: it counts the age as whole days and then assumes that each day is exactly 24 hours long. Most of the time that's correct, but in most places in the world there are days with daylight savings time transitions and timezone transitions, meaning there have been days with 23, 25, or some other number of hours. The number you get from your Java code is more precise.

Joni
  • 108,737
  • 14
  • 143
  • 193
2

I think you're somehow mixing java.sql.Date and java.util.Date.

I would try simplifying the code. Something like this.

public class Test012 {

    public static void main(String[] args) throws Exception {
        System.out.println( seconds() );
        System.out.println( seconds2() );
        System.out.println( days3() );
    }

    public static long seconds() throws Exception {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Date d1 = sdf.parse("1986-04-08");
        java.util.Date d2 = sdf.parse("2013-11-28");
        return ( d2.getTime() - d1.getTime() ) / 1000;
    }

    public static long seconds2() throws Exception {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Date d1 = sdf.parse("1986-04-08");
        java.util.Date d2 = new java.util.Date();
        return ( d2.getTime() - d1.getTime() ) / 1000;
    }

    public static long days3() throws Exception {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Date d1 = sdf.parse("2008-01-01");
        java.util.Date d2 = sdf.parse("2009-01-01");
        return ( d2.getTime() - d1.getTime() ) / 1000 / 60 / 60 / 24;
    }

}

I also tried

select datediff(ss, '4/8/1986', '11/28/2013')  --- US date format

in SQL Server and it prints the same thing as this java program, it prints 872294400. So this seems to be the correct value.

Are you sure the dates coming on your input are the right ones (are equal to those I hardcoded in my test program)? I would check that too.

Also, are you sure your dates have zero time parts? That's what the link/service you posted assumes.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • Yes that is confirmed. However the website is misleading. – Skynet Nov 28 '13 at 11:33
  • The website seems to be using zero time parts and probably does what I did in SQL :) – peter.petrov Nov 28 '13 at 11:35
  • I need to add time parts, however for the start date I assume it to be at midnight. – Skynet Nov 28 '13 at 11:36
  • I see. See the updated test program then. I mean, I am not sure what the actual problem is. Once you parse the end date correctly, you don't have any more problems. – peter.petrov Nov 28 '13 at 11:37
  • Yes, for sure :) It would be too wrong otherwise. Let me update my program once again. 2008 was leap. See what days3 prints out - 366 days, that's due to the presence of 29-Feb-2008. – peter.petrov Nov 28 '13 at 11:48
  • Perfect man, I tested it on my end too! It takes leap ones into consideration! – Skynet Nov 28 '13 at 11:52
  • I think the website I linked to takes the 11:59 hours into consideration for the present day - till the end of the present day. – Skynet Nov 28 '13 at 11:54
  • Thanks man, the website is completely misleading. Java rocks so do we!! – Skynet Nov 28 '13 at 12:48
0

Try this code:--

 public static long secondsBetween(Calendar startDate, Calendar endDate) {
    Calendar date = (Calendar) startDate.clone();
    long daysBetween = 0;
    while (date.before(endDate)) {
        date.add(Calendar.DAY_OF_MONTH, 1);
        daysBetween++;
    }
    return daysBetween*24*3600;
}

Hope it helps you.. Enjoy..!

Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47