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

Similar Messages

  • How do you show the number of days per month?

    How do you show the number of days per month?
    I am working on a budget.  I want to know how much to amortize each month to fund an investment.  Income comes monthly, but expenses leave in days, or weeks.  The number of days and weeks in months vary. 
    I want to figure out income and expenses per day, per month and per year, so I know how much can be invested each month, week or day.  For a start I would like a formula that shows how many days are in each month?

    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 can I calculate the maximum number of days between two dates in a range of dates?

    I have a column of dates spanning the couse of a few months.  I would like to know if I can calculate the maximum number of days between each row and display the highest number.  I currently have another column that calculates the days betwen the rows and I am currently just looking at the totals and highlighting the highest period.
    Is this possible?  Any help or suggestions are appreciated.
    Thank you,
    Trevor

    This sounds totally possible,  Can you post a screen shot of your table to make responding more focused?  If you mean you want to:
    A) compute the difference (in days) between two date in the same row, then
    B) find the max duration (in days)
    Here is my take on this problem:
    D2 = C2-B2
    select D2 and fill down
    F1=MAX(D)
    to perform the conditional formatting (to highlight the max duration) select column D, then set up conditional formatting as shown in the 1st image

  • 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
    Thanks

    Hi
    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

  • 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.

  • How can you lower the number of days in IOS8 iPhotos deleted

    In IOS8 photos deleted stay for 30 days. Can you lower the number of days?

    To answer your Subject (rather than the body of your post)
    To get the number of controls, and refs to each, etc, you can combine a
    couple of example VIs and a little index finger grease to make a VI that
    does what you want:
    Help menu >> Find Examples
    Find Example Dialog >>Browse tab
    Programmatically controlling VIs
    Manipulating Applications and VI settings
    Edit Multiple VI Setups.vi
    New Examples for LabVIEW 6.1
    Queues
    Queue Control Seek.vi
    If these two VIs are put in a mixer and shaken (not stirred) you might come
    up with something that tastes write.
    Mike Ashe
    "GL" wrote in message
    news:[email protected]..
    > I need to convert llb from 6i to 6.1 and I am unable to, I beli
    eve
    > because I have to many controls and indicators in the llb.

  • How do you calculate the number of bytes in a hexidecimal array?

    Hi can anybody help me calculate the number of bytes in a hexidecimal array ie:
    04h + 12h + 4Eh + 20h = 84h
    Thanks

    Hi,
    I'm not sure if you want to count the bytes or sum the array. But here's an example of both:
    Hope this helps,
    Paulo
    Attachments:
    ByteCount.vi ‏18 KB

  • How to calculate the number of months between two string as yyyymm

    Dear all,
    I have two month string like yyyymm e.g. 201003 -- 201105. Now I want to calculate the number of months bertween these two month string? How can I do that? Thanks.

    888099 wrote:
    Hi,
    Hope this will help :
         public static void main(String[] args) {
              Calendar cal1 = new GregorianCalendar(2010, 02, 01);
              Calendar cal2 = new GregorianCalendar(2011, 04, 01);
              int years = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
              int months = cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
              System.out.println("number of months : "+Math.abs(years*12 + months));
         }Or with only Strings :
         public static void main(String[] args) {
              String s1 = "201002";
              String s2 = "201104";
              int years = Integer.valueOf(s1.substring(0,4)) - Integer.valueOf(s2.substring(0,4));
              int months = Integer.valueOf(s1.substring(4)) - Integer.valueOf(s2.substring(4));
              System.out.println("number of month : "+Math.abs(years*12 + months));
         }Edited by: 888099 on 28 sept. 2011 05:49Full code solutions will not help the OP, he will be back here with a different Date question. Posters here try to nudge the OP to figure out the solution himself.

  • Calculate Difference in Days Between two Dates

    Hi,
    I'm trying to figure out how to calculate the difference in days between two dates using JavaScript in LiveCycle. (JavaScript knowledge = minimal)
    Where "Start_Date" and "Current_Date" are the names of the two dates in the Hierarchy palette. (both Date/Time Field)
    *Current date is using the Object > Value > Runtime Property > Current Date/Time
    I need a Text or Numeric field displaying the difference in days. (Difference_in_Days)
    I've noticed the following code being pretty standard amongst other responses:
    var 
    Start_Date = new Date(Start_Date);
    var 
    Current_Date = new Date(Current_Date);
    var 
    nAgeMilliseconds = Current_Date.getTime() - Start_Date.getTime();
    var 
    nMilliSecondsPerYear = 365 * 24 * 60 * 60 * 1000;
    I know there's code missing, and the above code might not be correct.
    Please advise.

    Where "DateField01" = user entered date field
    Where "DateFiled02" = Current Date/Time (Runtime Property)
    where "Subform" = the subform containing DateField01
    My script now resembles:
    var oneDay = 24*60*60*1000;
    var firstDate = new Date(Subform.DateField01.rawValue);
    var secondDate = new Date(DateField02.rawValue);  
    (firstDate.getTime() - secondDate.getTime()) / oneDay;
    I tried adding:
    app.alert(String(diffDays));
    Although I assume the reason I didn't get an error is because DateField01 is empty when the form is opened.
    If I swap in actual dates instead of fields it works perfectly.
    When I use the fields I have no information populating after I enter a date in "DateField01"

  • How to calculate number of days between fixed date and hire date in schema

    i want to calculate the number of days between a fixed date en the hire date (infotype 041)
    If i read the documentation  right, i should use the following command:
    HRS=YDXA02
    Variable 3 (the x) is the fixed data.
    My question is how do i program the variable x?
    Let's say the date 01.01.2008
    Any help would be welcome

    I assume we are talking about PCR for payroll. In this case look at documentation for operation NUM:
    OOOOOO
    Xnnnnn
    X     Table
    nnnnn Table field
    nn    Date type 'nn' from the Date Specifications infotype (0041)
         Date types are taken from table T548Y.
    Your operation would look like:
      NUM=Faaesb
          F          Fixed indicator for deadline calculation
           aa           Date type: Start date of period to be
                        calculated; if the date type is not 'nn,'
                        the second position is left blank.
             e          Unit, in which the duration is calculated:
                        T = days
                        W = week
    s         End date of period to be calculated:
              (blank)  End of current payroll period
              J        End of current calendar year
              A        Start of current calendar year
              B        Start of WPBP period
             No other specifications are possible.
    So it depends on what kind of fixed date you need.
    Hope it helps,
    Carlos.

  • Calculate number of days between two festival dates in payroll schema

    Hi Experts,
    According to my company policy:
    If an empl termination happens before Eid festival of the current year, then the pro-rated Eid bonus (yearly) is based on:
    (The duration between Eid Festival date of previous year - Eid festival date of current year.)/350 days
    Time Evaluation is not implemented in the system. Only payroll is implemented.
    My question: How can I capture the number of days between (Eid Festival date of previous year - Eid festival date of current year) in the payroll schema ?
    Regards,
    Somdeb.

    Hi,
    You can try using date specification. If you update last year eed holiday date for the employee then you can use the operand F along with NUM to get days between last eed holiday and current payroll period last day.
    i.e NUM=FaaT 0
    F is operand to query date specification.
    aa is the date specification.
    T is for unit and it gives number of days
    next space is for current payroll period last day and 0 will reset alternate date to zero if no date specification is mentioned.
    Basically this will give number of days from date mentioned in date type aa to current period last day. For more info you can go through documentation on operand F for NUM.
    Let us know if this helps.
    Regards,
    Poorvi.

  • 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

  • Can you slow down a voicemail you are listening to? I can't understand what someone said to me because they spoke too fast. I just want to know how to slow down the voicemail so I can understand her if it's possible.

    How can you slow down a voicemail you are listening to? Someone sent me a voicemail and they spoke way too fast for me to understand them. I want to know how to slow down the voicemail so I can understand them if that is possible.

       I have to say, this is so weird because it's actually become this way in the past week maybe, and I'm sure me saying this here will be disputed, ridiculed and belittled, and that's absolutely fair, but I've got to say it... up until a few days ago, my iTunes sync/backup area really would keep more than one backup for my one, single iPhone. They were all timed and dated with a drop-down menu (which appeared if I clicked the restore button) to choose which one I wanted to roll back to, for example. Sure, I'd read about the three backups thing etc. etc. and perhaps I got that wrong or misunderstood, but I can honestly say, I only ever had at the most two backups, that I could drop-down and choose to restore from. They were there. Same iPhone. Different dates.
       At the time, when this seemed to be the case, the backing-up process took quite a while. I would leave it to progress while I went and made a cup of tea, come back and it still wasn't finished running. So, not really AGES, but a little while.
       Now, when I perform a backup, the progress bar almost shoots across to a finish. I'll 'fess up... that worries me most of all. I can live without the luxury of a menu of backups... but if it does it once, I'd like to be sure it does it at all.
    Much more importantly though, thank you for your replies so far.
    Any further input, even if it's to belittle or shame is totally welcome. I'd like to understand this a bit more.
    Thanks again.

  • Hi, I live in the UK and have an ipad2 with wifi (no 3G). I plan to visit Canada in the summer and want to know how i can access the internet while i'm away e.g. can i purchase a 3G attachment that will give me access on a pay as you go basis. Thanks.

    Hi, I live in the UK and have an ipad2 with wifi (no 3G). I plan to visit Canada in the summer and want to know how i can access the internet while i'm away e.g. can i purchase a 3G attachment that will give me access on a pay as you go basis. Thanks.

    You can get Mifi devices (the iPad connects to it via wifi and they then give access to 3G networks). I've never used one and I don't know if they are available in Canada (and if so how much they would cost). If you have an iPhone then you could also use that as a mobile hotspot

  • HT1918 I changed the card and i want to know how make to change the data from itunes. I tried and doesn't work.

    I changed the card and i want to know how make to change the data from itunes. I tried and doesn't work. Please, Help Me!

    No. That's not what I said. You can get an out of warranty replacement, which will be exactly the same model, color, and will be locked in exactly the same way the original is. If you want an iPhone 5, get the 4S replaced, sell it, and use the money to put toward a new phone.

Maybe you are looking for