cancel
Showing results for 
Search instead for 
Did you mean: 

Difference between two dates

Former Member
0 Kudos

Hi All,

I have two date fields start date and end date.I need to calculate the difference between two dates ie end date - start date.Can some one of you suggest me on the same.

Regards,

Naaz

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Please go through these threads,

Regards,

ramesh

Answers (6)

Answers (6)

Former Member
0 Kudos

Hi All,

These are the values which I am giving Date1 : 2/28/2000 and Date2 : 3/1/2004.Their difference I am get it as 4 years 1 month 27days whereas I have 2 get the answer as 4years 2 days.

This is the code that I have written 2 retrive it :

Date d1 = wdContext.currentContextElement().getDate1();

Date d2 = wdContext.currentContextElement().getDate2();

int years = d2.getYear()-d1.getYear();

int months = d2.getMonth()-d1.getMonth();

int days = (d2.getDate()-d1.getDate());

wdComponentAPI.getMessageManager().reportSuccess( years"Yrs"months"Months"days+"days");

Thankx,

Naaz.

Former Member
0 Kudos

Hi All,

All the values are coming perfectly but if there are any leap years in between I am not getting the exact days.

Thankx,

Naaz.

Former Member
0 Kudos

Hi Ramesh,

Thankx for the quick reply.I got the output right.I am getting it in days but i want the output in year, month and days format (Ex.: 2 Years 3 Monts 4 Days).

Former Member
0 Kudos

Convert the days into years,months and days using division and modulo division.

For Example:

long days = 1234;

int years = 1234/365;

int balanceDays = 1234%365;

int months = balanceDays/30;

balanceDays = balanceDays%30;

former_member205363
Contributor
0 Kudos

Hi,

Suppose we have 400 days difference then tell me how many years months days. we cant show.

If you want in this format then

int years = d2.getYear()-d1.getYear();

int months = d2.getMonth()-d1.getMonth();

int days = d2.getDate()-d1.getDate();

take care about difference of days.

Regards,

Lakshmi Prasad.

piyush_kumar6
Active Contributor
0 Kudos

Hi Naaz,

Check the following code

import java.util.Calendar;

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

Output

Time in milliseconds: = diff

Time in seconds: = diffSeconds

Time in minutes: = diffMinutes

Time in hours: = diffHours

Time in days: = diffDays

Regards,

Piyush

Former Member
0 Kudos

Hi,



Date startDate = wdContext.currentContextElement().getStartDate();
Date endDate = wdContext.currentContextElement().getEndDate();
 
long diff = endDate.getTime() - startDate.getTime();
long days = (diff / (1000 * 60 * 60 * 24));
System.out.println(days);

Regards

Ayyapparaj

Former Member
0 Kudos

Hi Ghousia Naaz ,

Refer the following code

Calendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25);
    
    Calendar newyears = new GregorianCalendar(1999, Calendar.JANUARY, 1);
    
    // Determine which is earlier
    boolean b = xmas.after(newyears);            // false
    b = xmas.before(newyears);                   // true
    
    
    // Get difference in milliseconds
    long diffMillis = newyears.getTimeInMillis()-xmas.getTimeInMillis();
    
    // Get difference in seconds
    long diffSecs = diffMillis/(1000);           // 604800
    
    // Get difference in minutes
    long diffMins = diffMillis/(60*1000);        // 10080
    
    // Get difference in hours
    long diffHours = diffMillis/(60*60*1000);    // 168
    
    // Get difference in days
    long diffDays = diffMillis/(24*60*60*1000);  // 7