on 11-20-2008 6:00 AM
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.