1 2 Previous Next 23 Replies Latest reply: Feb 1, 2009 10:57 PM by 807588 Go to original post RSS
      • 15. Re: Number of months between two dates
        796440
        sankar21 wrote:
        jverd wrote:
        sankar21 wrote:
        starting date is 02/11/2008
        ending date is 22/01/2009

        I need the difference between this two date and get the answer in month
        You STILL haven't made it clear enough. Do you only want full months, rounded down? Or do you want the total number of months "touched"? E.g., Is 1/31/08 to 2/1/09 2 months? One month? Zero months? What about 1/31/08 to 2/28/09?
        if starting date is 1/31/08 to 2/1/08 then answer is one month
        Okay, if starting date is 1/31/08, then what date does the end have to be to make it one month?

        Can you describe your month counting rules clearly and precisely?
        • 16. Re: Number of months between two dates
          807588
          Hi,

          I think the following code will solve your problem...


          import java.util.*;

          public class Main {
               
               public static void main (String args[]) {
                    
                    Date d1 = new GregorianCalendar(2010,01,01).getTime();

          /** Today's date */
          Date d2 = new GregorianCalendar(2009,05,25).getTime();

          float m = 0;
          int y1 = d1.getYear();
          int y2 = d2.getYear();
          int m1 = d1.getMonth();
          int m2 = d2.getMonth();
          int dy1 = d1.getDate();
          int dy2 = d2.getDate();


          // If d1>d2 perform d1-d2
          if(d1.compareTo(d2)>0)
          {
               m= (y1-y2)*12;
               
               if((m1-m2)<0)
               {
                    m = (y1-y2-1)*12 + m1-m2 +12;
               }
               else {
                    m= m + m1-m2;
               }
               
               if((dy1-dy2)<0)
               {
                    m = m-1 + (float)(dy1-dy2+30)/30;
               }
               else {
                    
                    m= m + (float)(dy1-dy2)/30;
               }
               
               System.out.println("date1 - date2 is "+ m + " months ");
          }
          // If d2>d1 perform d2-d1
          else {
               
               m= (y2-y1)*12;
               
               if((m2-m1)<0)
               {
                    m = (y2-y1-1)*12 + m2-m1 +12;
               }
               else {
                    m= m + m2-m1;
               }
               
               if((dy1-dy2)<0)
               {
                    m = m-1 + (float)(dy2-dy1+30)/30;
               }
               else {
                    
                    m= m + (float)(dy2-dy1)/30;
               }
               
               System.out.println("date1 - date2 is "+(-1)*m + " months ");
          }

               
               }
          }
          • 17. Re: Number of months between two dates
            807588
            Speed the plow! [JSR 310|http://today.java.net/pub/a/today/2008/09/18/jsr-310-new-java-date-time-api.html] (AKA JSR "Joda")
            • 18. Re: Number of months between two dates
              796440
              AnushaG wrote:
              Hi,

              I think the following code will solve your problem...
              1. That code is horrible.

              2. He hasn't even made his requirements clear yet.
              • 19. Re: Number of months between two dates
                807588
                jverd wrote:
                AnushaG wrote:
                Hi,

                I think the following code will solve your problem...
                1. That code is horrible.

                2. He hasn't even made his requirements clear yet.
                Did anyone asked you? I don't remember so keep your opinions for yourself!
                If not I will personally come and kick you.. you know what.
                • 20. Re: Number of months between two dates
                  796440
                  ValidSteve wrote:
                  jverd wrote:
                  AnushaG wrote:
                  Hi,

                  I think the following code will solve your problem...
                  1. That code is horrible.

                  2. He hasn't even made his requirements clear yet.
                  Did anyone asked you? I don't remember so keep your opinions for yourself!
                  You don't understand the way a public forum works, do you? You're posting in public so anyone is free to comment on what you post, even if you don't like what they say.

                  You think that if somebody posts garbage code, nobody should say anything, and the OP should be left thinking it's not garbage? If you go to a restaurant and they serve you feces in a bowl, wouldn't you want somebody to tell you it's feces?
                  If not I will personally come and kick you.. you know what.
                  Oh, golly, I'm trembling with fear.
                  • 21. Re: Number of months between two dates
                    796440
                    AnushaG wrote:
                    Hi,

                    I think the following code will solve your problem...
                    1. That code is overly complex and hard to follow.

                    2. There's no reason to use floating point numbers for getting a difference in months.

                    3. If you're going to use floating point number, about the only time you'd ever use float instead of double is if you're on an embedded device or a phone or something like that with very limited memory.

                    4. Repeating the whole calculation with the 1s and 2s flipped is ridiculous. Just flip the variables and have only one calculation block.

                    There's probably more, but I can't be bothered to look any closer.
                    • 22. Re: Number of months between two dates
                      807588
                      I got the exact output. Thanks
                      • 23. Re: Number of months between two dates
                        807588
                        I got the exact output from darth_code_r's code. Thanks for your comments
                        1 2 Previous Next