time operations day difference

hi , ?have to subruct two days like 13/08/07 03/08/07 and result must be

10 ? at my program ?have dates as string '20070102' yyyyddmm

first ?am converting them to date

public Date toDate(String dateString){

int year = Integer.parseInt(dateString.substring(0,4));

int month = Integer.parseInt(dateString.substring(4,6));

int day = Integer.parseInt(dateString.substring(6,8));

Calendar c1 = Calendar.getInstance();

c1.set(year,month,day);

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy");

return c1.getTime();

}

then subsructing two dates

using this function

public int dateDifference(Date d1, Date d2){

int days=0;

Calendar c1 = Calendar.getInstance();

Calendar c2 = Calendar.getInstance();

c1.setTime(d1);

c2.setTime(d2);

long first = c2.getTime().getTime();

long second = c1.getTime().getTime();

long result = (first - second) /(24*3600*1000);

days = (int) result;

return days;

}

but somethings going wrong like difference between x and y not always z it s for ex '20070113'-'20070101' = 12 but '20070108'-'20070101' = 6

how can it be.

thnks.

[1238 byte] By [javabymurata] at [2007-11-27 9:12:55]
# 1
try this: http://forum.java.sun.com/thread.jspa?threadID=156593&messageID=1865789 http://forum.java.sun.com/thread.jspa?threadID=554248
Yannixa at 2007-7-12 21:59:52 > top of Java-index,Java Essentials,Java Programming...
# 2
Nah, those links don't point to the preferred way of doing this, sorry to say.Better to use DateFormat or SimpleDateFormat to parse directly into a Date, then convert the date to Calendar using Calendar.setTime(Date).
OleVVa at 2007-7-12 21:59:52 > top of Java-index,Java Essentials,Java Programming...
# 3
Finally add 1 day to c1 repeatedly until you reach c2. Count the days underway, and you'll end up knowing how many there are.
OleVVa at 2007-7-12 21:59:52 > top of Java-index,Java Essentials,Java Programming...
# 4

This thread isn't concerned with exactly the same subject, but should give you inspiration and a fine code example:

http://forum.java.sun.com/thread.jspa?threadID=5189228

There's probably a better code example (because I posted it :-) here:

http://forum.java.sun.com/thread.jspa?threadID=5163904

OleVVa at 2007-7-12 21:59:52 > top of Java-index,Java Essentials,Java Programming...
# 5
thanks for ur helps but ?think ?am exactly doing same things in my code what is my wrong why result is changing?
javabymurata at 2007-7-12 21:59:52 > top of Java-index,Java Essentials,Java Programming...
# 6

I'd say you're doing a funny mix of the two approaches Yannix and I suggested to you, including a superfluous conversion to Calendar and back.

I cannot tell you why your code gives the unexpected result with the data you've tried. You may want to investigate what happens in the long division, which casts away any remainder.

I can tell you that your code will not work across the change from standard time to summer time, since there's a day with only 23 hours in it, and that day will surely get lost in your division. My approach will give you the expected result over a change to and from summer time.

OleVVa at 2007-7-12 21:59:52 > top of Java-index,Java Essentials,Java Programming...
# 7

public class Diff

{

public static void printDiff(String sdate1, String sdate2, String fmt, TimeZone tz)

{

SimpleDateFormat df = new SimpleDateFormat(fmt);

Date date1 = null;

Date date2 = null;

try

{

date1 = df.parse(sdate1);

date2 = df.parse(sdate2);

}

catch (ParseException pe)

{

pe.printStackTrace();

}

Calendar cal1 = null;

Calendar cal2 = null;

if (tz == null)

{

cal1=Calendar.getInstance();

cal2=Calendar.getInstance();

}

else

{

cal1=Calendar.getInstance(tz);

cal2=Calendar.getInstance(tz);

}

// different date might have different offset

cal1.setTime(date1);

long ldate1 = date1.getTime() + cal1.get(Calendar.ZONE_OFFSET) + cal1.get(Calendar.DST_OFFSET);

cal2.setTime(date2);

long ldate2 = date2.getTime() + cal2.get(Calendar.ZONE_OFFSET) + cal2.get(Calendar.DST_OFFSET);

// Use integer calculation, truncate the decimals

int hr1= (int)(ldate1/3600000); //60*60*1000

int hr2= (int)(ldate2/3600000);

int days1 = (int)hr1/24;

int days2 = (int)hr2/24;

int dateDiff = days2 - days1;

int weekOffset = (cal2.get(Calendar.DAY_OF_WEEK) - cal1.get(Calendar.DAY_OF_WEEK))<0 ? 1 : 0;

int weekDiff = dateDiff/7 + weekOffset;

int yearDiff = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);

int monthDiff = yearDiff * 12 + cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);

System.out.println();

System.out.println("DateTime 1: " + sdate1);

System.out.println("DateTime 2: " + sdate2);

System.out.println("Date difference : " + dateDiff);

System.out.println("Week difference : " + weekDiff);

System.out.println("Month difference: " + monthDiff);

System.out.println("Year difference : " + yearDiff);

}

public static void main(String[] args)

{

String fmt= null;

String sdate1 = null;

String sdate2 = null;

fmt= "MM-dd-yyyy HH:mm:ss";

sdate1 = "12-31-2002 23:59:59";

sdate2 = "01-01-2003 00:00:01";

// Result is independent of format

// null will print in local timezone

// Print out 1 day, i month, 1 year difference

System.out.println("In your local time:");

printDiff(sdate1, sdate2, fmt, null);

// Beijing timezone, if you are not in +8 timezone, the resuls are all 0's

System.out.println("In Beijing time:");

printDiff(sdate1, sdate2, fmt, TimeZone.getTimeZone("GMT+08:00"));

// for testing the weekDiff

fmt= "MM-dd-yyyy HH:mm:ss";

sdate1 = "12-31-2002 23:59:59";

sdate2 = "01-06-2003 00:00:01";

System.out.println("In your local time:");

printDiff(sdate1, sdate2, fmt, null);

fmt= "MM-dd-yyyy HH:mm:ss";

sdate1 = "01-04-2003 23:59:59";

sdate2 = "01-05-2003 00:00:01";

System.out.println("In your local time:");

printDiff(sdate1, sdate2, fmt, null);

// something interesting here

fmt= "MM-dd-yyyy HH:mm:ss";

sdate1 = "12-31-1996 23:59:59";

sdate2 = "01-01-1997 00:00:01";

System.out.println("In your local time:");

printDiff(sdate1, sdate2, fmt, null);

}

}

vinayak_ra at 2007-7-12 21:59:52 > top of Java-index,Java Essentials,Java Programming...
# 8
thanks for all, I think u are right olew cause ?looked for a subruction using calendar but there was not and ?had to use time and divisions to find result ,it was adding usind minus numbers or just as ur sayin adding until to reach second date seems ok,
javabymurata at 2007-7-12 21:59:52 > top of Java-index,Java Essentials,Java Programming...