How to get the number of days between 2 given dates
Hi all,
How can I find the number of days between any 2 given dates.
Thanks
Hi
Here's a dirty way:
If you've got two Date objects in Java, call getTime() on each one (which gives you a value in milliseconds), then subtract the two millisecond values, and divide the result by the number of milliseconds in a day (24 * 60 * 60 * 1000). There's more `elegant' ways to do this, but this method only requires one line of Java.
Best wishes
Kevin
Similar Messages
-
I wanted to know how do you calculate the number of days between two dates
i wanted to know how do you calculate the number of days between two dates in java ? i get both the dates from the database. i guess there are many issues like leap year and Febuary having diff no of months ..etc.
thanks..
I solve my problem as
public class MyExample {
public static void main(String a[]) {
String stdate = "2009-03-01";
java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis());
java.sql.Date preDate = java.sql.Date.valueOf(stdate);
System.out.println(currentDate);
System.out.println(preDate);
// int dateCom = preDate.compareTo(currentDate);
// System.out.println(dateCom);
long diff = currentDate.getTime() - preDate.getTime();
int days = (int) Math.floor(diff / (24 * 60 * 60 * 1000));
System.out.println(days);
} -
How to get the number of days in a month?
hi all
is there any way to get the number of days in a given month with a given year? for instance, if year is 2004 and the month is July or February, how can i get the number of days? thanks.Gee, I don't know ... Maybe this:
Calendar cal = Calendar.newInstance();
cal.set(Calendar.YEAR, 2004);
cal.set(Calendar.MONTH, Calendar.FEBUARY);
System.out.println("max days in month: " + cal.getActualMaximum(Calendar.DAY_OF_MONTH));
Do you not bother reading what people have already posted? Read the API docs on the Calendar class. -
How to get the number of days of February.....
Hi ,
how can I get the number of days (28 or 29) of February of the current year or the next...????
Or is there any other way to find that a year is a leap or not (i mean the year has 365 or 366 days).??
Thanks
Simonselect add_months(to_date('&v_year', 'YYYY'), 12) -
to_date('&v_year', 'YYYY') from dual;Jens, it seems we have made the same mistake, trunc usage seems mandatory :
SQL> ed
Wrote file afiedt.buf
1* select add_months(to_date('2007', 'YYYY'), 12) - to_date('2007', 'YYYY') from dual
SQL> /
ADD_MONTHS(TO_DATE('2007','YYYY'),12)-TO_DATE('2007','YYYY')
366
SQL> ed
Wrote file afiedt.buf
1* select add_months(trunc(to_date('2007', 'YYYY'), 'YYYY'), 12) - trunc(to_date('2007', 'YYYY'), 'YYYY') from dual
SQL> /
ADD_MONTHS(TRUNC(TO_DATE('2007','YYYY'),'YYYY'),12)-TRUNC(TO_DATE('2007','YYYY')
365
SQL> Just to clarify my past doubt.
Nicolas. -
SQL to count the number of days between two dates
Does any one have or know how to count the number of days/weeks between 2 dates.
I have thought about using MONTHS_BETWEEN, but do not know how to make it accurate down to one day?
Any suggestions would be appreciated.here are the different queries I came up with to do this, you may want to check it for perticular cases involving Sun and Sat as start and end dates.
select to_number(to_char(to_date('21-JUL-00','dd-mon-yy'), 'ww'))
- to_number(to_char(to_date('10-JUL-00','dd-mon-yy'), 'ww')) from dual;
select (Next_Day(to_date('21-JUL-00','dd-mon-yy')-1, 'Sunday')
- Next_Day(to_date('10-JUL-00','dd-mon-yy')-1, 'Sunday'))/7 from dual;
select (Next_Day(to_date('21-JUL-00','dd-mon-yy')-6, 'Sunday')
- Next_Day(to_date('10-JUL-00','dd-mon-yy')-6, 'Sunday'))/7 from dual;
null -
How to get the number of days of a month belonging to a date interval
Hi, i am getting mad around a problem, i have 2 dates and a month, i wanto to retrieve the number of days belonging to the month that are in the interval.
eg:
month january 2011 . begin_date = 11/JAN/2011, END_DATE 30/MAY/2011 result is 21
month january 2011 . begin_date = 11/DEC/2010, END_DATE 10/JAN/2011 result 10
month january 2011 .begin_date = 02/FEB/2011 , END_DATE 25/may/2011 result 0
month january 2011. begin_date = 03/JAN/2011 , END DATE 05/JAN/2011 result 3
and so on ...
i appreciate any suggestion
thank you
AndreaOh, I didnt see your result.
SQL> with t as
2 (select to_date('11/01/11','dd/mm/yy') from_dt,
3 to_date('30/05/11','dd/mm/yy') to_dt,
4 'Jan-11' mnth from dual
5 union all
6 select to_date('11/12/10','dd/mm/yy') from_dt,
7 to_date('10/01/11','dd/mm/yy') to_dt,
8 'Jan-11' mnth from dual
9 union all
10 select to_date('02/02/11','dd/mm/yy') from_dt,
11 to_date('25/05/11','dd/mm/yy') to_dt,
12 'Jan-11' mnth from dual
13 union all
14 select to_date('03/01/11','dd/mm/yy') from_dt,
15 to_date('05/01/11','dd/mm/yy') to_dt,
16 'Jan-11' mnth from dual
17 )
18 select from_dt,to_dt,mnth,
19 greatest(
20 least(last_day(to_date(mnth,'Mon-yy')),to_dt)
21 -
22 greatest(to_date(mnth,'Mon-yy'),from_dt)+1
23 ,0) cnt
24 from t;
FROM_DT TO_DT MNTH CNT
11-JAN-11 30-MAY-11 Jan-11 21
11-DEC-10 10-JAN-11 Jan-11 10
02-FEB-11 25-MAY-11 Jan-11 0
03-JAN-11 05-JAN-11 Jan-11 3 -
hai bloggers,
see we have 365 days per year for first half we are going to have 180+ days (if we select first half as the prompt and we select week number as prompt and the last date of that particular week should show in the report)and number of days upto that date or week also should display...if we select second half and week number of that particular half it should show number of days upto that week and highest date of that week number of days should count from 1 to 180+ only mean (july 1 st is 1 st day)
thanksYou need to use timestampdiff function for calculating number of days. One of the way to calculate is like this
case when "Time"."Calendar Half Year" = 1 then timestampdiff(SQL_TSI_DAY,CAST(EVALUATE('TO_DATE(%1,%2)','01-01'||'@{YEAR}{2010}','DD-MM-YYYY') AS DATE),CAST(EVALUATE('TO_DATE(%1,%2)','30-06'||'@{YEAR}{2010}','DD-MM-YYYY') AS DATE)) else timestampdiff(SQL_TSI_DAY,CAST(EVALUATE('TO_DATE(%1,%2)','01-07'||'@{YEAR}{2010}','DD-MM-YYYY') AS DATE),CAST(EVALUATE('TO_DATE(%1,%2)','31-12'||'@{YEAR}{2010}','DD-MM-YYYY') AS DATE)) end
If you want calculate between 1st day of the half and last date of a week then use following
case when "Time"."Calendar Half Year" = 1 then timestampdiff(SQL_TSI_DAY,CAST(EVALUATE('TO_DATE(%1,%2)','01-01'||'@{YEAR}{2010}','DD-MM-YYYY') AS DATE),MAX(MAX("Time"."Calendar Date"))) else timestampdiff(SQL_TSI_DAY,CAST(EVALUATE('TO_DATE(%1,%2)','01-07'||'@{YEAR}{2010}','DD-MM-YYYY') AS DATE),MAX(MAX("Time"."Calendar Date"))) end
Hope it will answer your question
Thanks,
Phani. -
(Groovy Expression) Get the no. of days between two dates
Hi All,
I am using ADF-BC-11g to create an entity object with 2 specific columns ActualDate and EstimatedDate both of Date type. I also want an additional field DaysBetweenTwoDates showing the difference between these two dates. Hence, I add another attribute DaysBetweenTwoDates in my entity object and after removing the Persistent property, I modify the expression field as ActualDate - EstimatedDate. When I test my AM, I get an error as below:
*(oracle.jbo.JboException) JBO-29000: Unexpected exception caught: groovy.lang.MissingMethodException, msg=No signature of method: java.sql.Date.minus() is applicable for argument types: (java.sql.Date) values: {2009-04-01}*
Can anyone please tell me how to achieve the same?
Please note that I can not add a new column DaysBetweenTwoDates in the table nor can I have a new VO attribute subtracting and storing the difference as there are some logical and refreshing constraints.
Any suggestions would be really helpful.
Thanks
NeerajThanks John,
Oops...I did not think on this ground at all. Thanks for directing towards right direction. However, just to know, is that possible anyways thorugh groovy expression? Do we have any docs where we can search for the functions available in groovy expression?
Regards,
Neeraj -
Getting number of days between 2 dates
Hello All,
I require the number of days between 2 dates.
The number of days is between low date : PR Date (not an input variable) and the high date.
I have created an customer exit variable to get sy-datum in made it as formula variable.
I have also created a formula variable for PR Date , and subtracted the 2 dates.
It is not giving the correct result.
Any suggestions.
Thanks & Regards,
VLRead through this web link. It will show you how to calculate the days between 2 dates directly in your BEX query.
No exit needed to calculate this.
http://teklink.co.uk/sap/sap-bi-bw-how-to-use-replacement-path-variables-to-perform-date-calculations-in-the-bex-analyzer/ -
How to calculate number of days between two date in Template design?
Hello guys
I have a situation where I have to create a template that returns data, and one of the thing of the existing report is that there is a column that is actually the number of days between start date and end date columns..
So in template, how would I be able to do the same? I have start date and end date columns on the template, now when I created another column using expression like end date - start date and preview the template, I am getting errors saying :
Caused by: oracle.xdo.parser.v2.XPathException: Cannot convert 03/31/2009 to number.
at oracle.xdo.parser.v2.XSLStylesheet.flushErrors(XSLStylesheet.java:1534)
at oracle.xdo.parser.v2.XSLStylesheet.execute(XSLStylesheet.java:521)
at oracle.xdo.parser.v2.XSLStylesheet.execute(XSLStylesheet.java:489)
at oracle.xdo.parser.v2.XSLProcessor.processXSL(XSLProcessor.java:271)
at oracle.xdo.parser.v2.XSLProcessor.processXSL(XSLProcessor.java:155)
at oracle.xdo.parser.v2.XSLProcessor.processXSL(XSLProcessor.java:192)
Please advice
ThanksHi
There is an extension function you can use, from the javadoc:
date_diff
public static long date_diff(java.lang.String format,
java.lang.String fromDate,
java.lang.String toDate,
java.lang.String locStr,
java.lang.String tzID)
Method to get the difference between two dates in the given locale. The dates need to be in "yyyy-MM-dd" format. This function supports only Gregorian calendar.
Parameters:
format - the format to which the difference is required; allowed formats are y (for Year), m(for month), w(for week), d(for day), h(for hour), mi(for minute), s(for seconds) and ms(for milliseconds)
fromDate - the first date
toDate - the second date
locStr - locale string -> lang-Territory
tzID - timezone ID ->http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimeZone.html
Returns:
the difference in dates in the desired format
For example
<?xdoxslt:date_diff(‘d’,’2009-09-14’, ‘2009-09-20’,’en-US’,1)?>
give a result of 6
You can substitute in columns for the dates, just remember the date format required.
Regards
Tim -
Number of days between two dates
I have two dates-String Date1, String Date2(format of both the dates is 'DD-MM-YY'eg;'28-Jan-2008'). I want to find the difference between these dates;ie if value of String Date1 is '28-Jan-2008' and value of String Date2 is '28-Jan-2009', I need to find the number of days between these dates. What need to be done? Can anyone help me out please.
Thanks in advance for all the help.
Regards,
AneesDoing a search is a bit ineffiecient. I'd take advantage of the fact that dates are acually stored in milliseconds. Subtract the two millisecond values (Data.getTime()) and divide by the number of milliseconds in a day. One complication, though, is daylight saving time but if you round rather than truncate in the division it should be fine.
private static final long MILLIS_PER_DAY = 24L * 60L * 60L * 1000L;
int dayDiff = (int)((date2.getTime() - date1.getTime() + MILLIS_PER_DAY/2) / MILLIS_PER_DAY); -
Number of days in given date/month
Hi,
Can you send query to find the number of days in given date/month
Regards,
Venkat.If you want No. of days between two date then you can just Subtract them. If you want the number of days in a month you can get the first and last day of the month and subtract them.
Solution for a month.
SQL> with t as (select to_date('&month','mmyyyy') as dt from dual)
2 select (last_day(dt) - trunc(dt, 'mm'))+1
3 from t
4 /
Enter value for month: 012009
old 1: with t as (select to_date('&month','mmyyyy') as dt from dual)
new 1: with t as (select to_date('012009','mmyyyy') as dt from dual)
(LAST_DAY(DT)-TRUNC(DT,'MM'))+1
31
SQL> /
Enter value for month: 022009
old 1: with t as (select to_date('&month','mmyyyy') as dt from dual)
new 1: with t as (select to_date('022009','mmyyyy') as dt from dual)
(LAST_DAY(DT)-TRUNC(DT,'MM'))+1
28
SQL> /
Enter value for month: 122009
old 1: with t as (select to_date('&month','mmyyyy') as dt from dual)
new 1: with t as (select to_date('122009','mmyyyy') as dt from dual)
(LAST_DAY(DT)-TRUNC(DT,'MM'))+1
31Edited by: Karthick_Arp on Feb 19, 2009 9:56 PM -
How do i get number of days between 2 dates?
How do i get number of days between 2 dates?
and the result must be in int.
for example
Ex. startdate: 2006-06-01 enddate: 2006-06-30 and the result is: 30
how to do so? thxmel
Iv'e used this method. It assumes startdate,enddate and days have been defined before but you could pass them as args.
void days()
try
Date d1 = DateFormat.getDateInstance().parse(startdate);
Date d2 = DateFormat.getDateInstance().parse(enddate);
long days = (d2.getTime()-d1.getTime())/1000/60/60/24;
days = ""+days;
catch (ParseException e)
System.out.println("Invalid date format");
}It actually gets the duration in ms and divides down to days.
Regards
Chris -
How can I get number of days between 2 dates ?
How can I get number of days between 2 dates ?
Give me answer as soon as possible.....Mukesh_Prajapat wrote:
How can I get number of days between 2 dates ?
Give me answer as soon as possible.....Is google broken again?
[How To Ask Questions The Smart Way|http://www.catb.org/~esr/faqs/smart-questions.html] -
How can i get number of days between two dates represented by two dates?
how can i get number of days between two dates represented by two date objects. One is java.sql.Date, the other is java.util.Date?
tej_222 wrote:
But how do I do that conversion. from java.sql.date and java.util.date to calender?
-thanks for the quick response.You may find the following utility code samples useful:
[http://balusc.blogspot.com/2007/09/calendarutil.html]
[http://balusc.blogspot.com/2007/09/dateutil.html]
ganeshmb wrote:
(date1.getTime() - date2.getTime())/(1000*60*60*24) should do.
getTime returns millsecond value of date object and the difference divided by no of milliseconds in a day should fetch you the difference in terms of days.This doesn't respect the DST. Use java.util.Calendar.
Maybe you are looking for
-
Hi, i have managed to to put windows on my mac air. However i cant acess the internet. I understand that i need the drivers of which i have downloaded the support software on to a flash drive. How do i install this onto the windows version. not sure
-
911-PLEASE HELP! OS X Mountain Lion "Blocked Plug-Ins"anyone please?
All Plug-ins enabled, no extensions, this is killing my ability to bid on jobs from a site. Only purchased the upgrade to expand my capabilities & software, so I could bid on more gigs & now I can't even service clients I already had with this "block
-
How to sync the standby database with the primary?
Hi..We have dataguard setup for our production databases(10.2.0.3). I need one clarification regarding the standby database sync with primary.We identified one of our databases is not in sync with the primary and for longtime the archives are not get
-
Flash Builder + Flash Catalyst = what for Flash?
Does the combination of Catalyst with Builder cannibalize Flash?
-
Font Licensing for Commercial Use - Video Games
Hello everyone. I need some SERIOUS help so any advice would be greatly appreciated. I'm currently in the process of Producing a video game which is using Minion Pro (Bold and Medium). I've been trying to call anyone and everyone at Adobe to figure o