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.
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);
}
}