8 Replies Latest reply on Nov 12, 2007 7:40 PM by 807600

    difference between dates with leap year

    807600
      hi i am trying to calculate the difference between two dates taking in consideration a leap year.

      this is my code but seem that doesn t work .it doesnt compile and i am stucked.

      could you help me please
      import java.util.*;
      
      public class RentOrder 
      {
          
          //attributes
          public static final int UnderAge =25;
          public static final int OverAge=55;
          protected Date pickUpDate;
          private Date returnDate;
          private double amount;
          private Date rentalPeriod;
          int numberOfDays;
          
          public int CalculateNumberOfDays(Date pickUpDate, Date returnDate) 
          {
              String pickUpDateText="12-32-1999";
              String returnDateText="23-23-2000";
              
              try
               {
                Calendar pickUpDate = new GregorianCalendar();
                Calendar returnDate = new GregorianCalendar();
                numberOfdays= 0;
                
                Date pickUpDate = new SimpleDateFormat("dd-MM-yyyy").parse(pickUpDateText);
                Date returndate = new SimpleDateFormat("dd-MM-yyyy").parse(returnDateText);
      
                pickUp.setTime(pickUpDate);
                today.setTime(returnDate);
               
                if(pickUpDate(Calendar.Year)%4 && pickUpDate(Calendar.Year)%100 && pickUpDate(Calendar.Year)%400 ||
                        pickUpDate(Calendar.Year)%4 
                        &&  pickUpDate(Calendar.Year)%!100 &&  pickUpDate(Calendar.Year)==Calendar.FEBRUARY 
                        || pickUpDate(Calendar.Year)<Calendar.FEBRUARY(Calendar.DATE(28)) &&
                        returnDate(Calendar.Year)>Calendar.FEBRUARY(Calendar.DATE(28)))
                    
                        {
                      
                                numberOfDays =returnDate.get(Calendar.DATE)- pickUp.get(Calendar.DATE)+1; 
                        }
                    
               
                        
                            numberOfDays =returnDate.get(Calendar.DATE)- pickUp.get(Calendar.DATE);
              }     
          }
      }  
        • 1. Re: difference between dates with leap year
          807600
          First, you have a try {} without a following catch, so add a catch statement after the try block.
          try {
           ...
          }
          catch (Exception e) {
            e.printStackTrace();
          }
          Second, your method says it returns an int, but you don't have any return statement (probably with numberOfDays). Return this outside your try catch (or have one return in each), as the method must always return a value regardless if it throws an Exception or not.
          • 2. Re: difference between dates with leap year
            807600
            still does not compile

            i don t understand why
            import java.util.*;
            
            public class RentOrder 
            {
                
                //attributes
                public static final int UnderAge =25;
                public static final int OverAge=55;
                protected Date pickUpDate;
                private Date returnDate;
                private double amount;
                private Date rentalPeriod;
                int numberOfDays;
                
                public static void main(String[] args)
                {
                    String pickUpDateText=" ";
                    String returnDateText=" ";
                    try {
                      Calendar pickUpDate = new GregorianCalendar();
                      Calendar returnDate = new GregorianCalendar();
                      numberOfdays= 0;
                      
                      Date pickUpDate = new SimpleDateFormat("dd-MM-yyyy").parse(pickUpDateText);
                      Date returndate = new SimpleDateFormat("dd-MM-yyyy").parse(returnDateText);
            
                      pickUp.setTime(pickUpDate);
                      today.setTime(returnDate);
                     
                      if(pickUpDate(Calendar.Year)%4 && pickUpDate(Calendar.Year)%100 && pickUpDate(Calendar.Year)%400 ||
                              pickUpDate(Calendar.Year)%4 
                              &&  pickUpDate(Calendar.Year)%!100 &&  pickUpDate(Calendar.Year)==Calendar.FEBRUARY 
                              || pickUpDate(Calendar.Year)<Calendar.FEBRUARY(Calendar.DATE(28)) &&
                              returnDate(Calendar.Year)>Calendar.FEBRUARY(Calendar.DATE(28)))
                          
                              {
                            
                                      numberOfDays =returnDate.get(Calendar.DATE)- pickUp.get(Calendar.DATE)+1; 
                              }
                          
                     
                              
                                  numberOfDays =returnDate.get(Calendar.DATE)- pickUp.get(Calendar.DATE);
                    }   
                    catch (Exception e)
                    {
                     e.printStackTrace();
                        } return numberOfDays;
                     
                }      
            }
               
            • 3. Re: difference between dates with leap year
              807600
              helppppppppppppppppp
              • 4. Re: difference between dates with leap year
                807600
                gigapr wrote:
                still does not compile
                Well do you get any errors when you try and compile? or are there any before you compile it? if so post them
                • 5. Re: difference between dates with leap year
                  807600
                  gigapr wrote:
                  still does not compile

                  i don t understand why
                  Um, well to begin you are now trying to return an in when the main() is static so that wont help at all...and check your numberOfDays calculation...not sure what the try catch are for haven't messed with them yet, but anyway, you have the pickUp sitting there, but you never declared a pickUp Object...you should change that to pickUpDate.get(xxx); I see what you are trying to do, but go through the compiler and understand what your errors are... I have seen many more errors than that on my programs and they can still be fixed...you just need to go through one at a time and find out why it is an error.

                  Can someone explain the try catch stuff haven't messed with it yet, but shouldn't you have an else statement after the if numberOfDays calculation? Looks like you are doing a numberOfDays calc for a leap year and the going out of the if statement just to do another calculation which cancels out the first one. Looks like there is a bit of an easier way to do this as well...try setting a private leapYR = 1804; and run a for statement that adds 4 checking up to say 3000...if (pickUpDate == i) then do leap calc...else do non leap calc. I don't think anyone is going to input anything below 1804 and you have quite some time before the year 3000.

                  By all means guys correct me if I am wrong...I am still new to Java and need some learnedness (technical term) all the time.

                  Best Intentions,
                  George
                  • 6. Re: difference between dates with leap year
                    807600
                    gheib1 wrote:
                    Um, well to begin you are now trying to return an in when the main() is static so that wont help at all...
                    No the reason you can't put a return statement there is that yes it is the main() which is the executable but it's declared a void so you can't return anything static means nothing you can declare something public static int and return an integer.
                    Can someone explain the try catch stuff haven't messed with it yet, but shouldn't you have an else statement after the if >numberOfDays calculation?
                    Try catch is used to handel exceptions that are thrown... it goes through the lines in the try part and if any exceptions are thrown then it breaks and goes to the catch statement where you usually print out an error code.
                    Looks like there is a bit of an easier way to do this as well...try setting a private leapYR = 1804; and run a for statement >that adds 4 checking up to say 3000...if (pickUpDate == i) then do leap calc...else do non leap calc. I don't think >anyone is going to input anything below 1804 and you have quite some time before the year 3000.
                    but what if they do go before or after there? then the program isn't effective and neither is that way of doing it. Assuming you are using a Gregorian calender you can do
                    public static final boolean isLeapViaPopeGregory ( int yyyy )
                    
                       {
                       if ( yyyy < 0 ) return( yyyy + 1 ) % 4 == 0;
                       if ( yyyy < 1582 ) return yyyy % 4 == 0;
                       if ( yyyy % 4 != 0 ) return false;
                       if ( yyyy % 100 != 0 ) return true;
                       if ( yyyy % 400 != 0 ) return false;
                       return true;
                       }
                    For other calendar calculations of leap years check out this website

                    http://mindprod.com/jgloss/leapyear.html
                    • 7. Re: difference between dates with leap year
                      807600
                      Nice reply Mark thanks for the input...looks a bit easier to do a try catch as well will have to start using that, and understand with the Gregorian calendar that without it the program isn't as effective...better to include all just in case...thanks again for the good info.
                      • 8. Re: difference between dates with leap year
                        807600
                        gheib1 wrote:
                        Nice reply Mark thanks for the input...looks a bit easier to do a try catch as well will have to start using that, and >understand with the Gregorian calendar that without it the program isn't as effective...better to include all just in >case...thanks again for the good info.
                        No problem ya try catch is a much more effective way of displaying exact exceptions when you have the possibility of having many being thrown and you want to handle each one different. Much more effective than slapping a throws IOException on the method