Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with Date change

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Date change

    I need help. "The maximum value of day depends on the month for the date. The maximum day for the month of February also depends on the year value for the ExtendedDate object (i.e. is it a leap year or not?)". How do i go about setting this up within my code?


    package date;
     
     
    public class ExtendedDate extends Date {
     
     
          public int newMonth;
          public int newDay;
          public int newYear;
     
     
     	// Constructor that accepts parameters
    	public ExtendedDate(int newMonth, int newDay, int newYear) {
     
     
                    newMonth = 1;
    		newDay = 1;
    		newYear = 1900;
     
     
     
     
    	}
     
    	// Method to set the date – override inherited setDate method
    	// If data is invalid, do not change the receiver
        /**
         *
         * @param monthInt
         * @param dayInt
         * @param yearInt
         */
        @Override
    	public void setDate(int newMonth, int newDay, int newYear) {
     
     
     
     
                    this.newMonth = newMonth;
    		this.newDay = newDay;
    		this.newYear = newYear;
     
     
     
    			}
     
    	// Method to set month ensuring that only valid changes are made
    	/**
         *
         * @param newMonth
         */
        public void setMonth(int newMonth) {
     
            if(isValidDate(newMonth, newDay, newYear)){
     
     
            this.newMonth = newMonth;
     
        }
     
        else {
     
           this.newMonth =(1); 
     
     
     
            }
     
     
     
      }
     
     
     
     
    	// Method to set day ensuring that only valid changes are made
    	public void setDay(int newDay) {
     
     
            if(isValidDate(newMonth, newDay, newYear)){  
     
     
           this.newDay = newDay;
     
     
            }
     
     
    	}
     
    // Method to set year ensuring that only valid changes are made
    	public void setYear(int newYear) {
     
     
     
            if(isValidDate(newMonth, newDay, newYear)){
     
     
           this.newYear = newYear;
     
     
            }
     
           }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    // New method to verify a date
    	// Valid range is 1-1-1900 through 12-31-2999
     
     
     
             /**
         *
         * @param newMonth
         * @param newDay
         * @param newYear
         * @return
         */
        public static boolean isValidDate(int newMonth, int newDay, int newYear) {
     
     
     
               if ( newMonth < 13 ||  newMonth > 0){
     
     
     
                                      return true;
     
     
     
                                }
     
               if (newDay < 31 || newDay >0){
     
               return true;
     
     
               }
     
              if (newYear > 1899 || newYear < 2999) {
     
     
              return true;
     
     
              }
     
     
     
     
            return false;
     
     
     
     
     
     
     
     
             }
     
     
     
     
     
     
     
    // Method to test for a leap year
    	public static boolean isLeapYear(int newYear) {
     
                if (newYear == newYear*4){
     
    		return true;
     
                }
                return false;
    	}
     
    // Instance method version of the above
    	public boolean isLeapYear() {
    		return true;
    	}
     
    // Return number of days in a month
    	public int daysInMonth() {
    		return 0;
    	}
     
        // Return number of days passed in year (includes current day)
    	public int daysToDate() {
    		return 0;
    	}
     
            // Return number of days remaining in year
        /**
         *
         * @return
         */
        @Override
    	public int daysRemainingInYear() {
    		return 0;
    	}
     
     
     
     
     
     
     
     
    }


    --- Update ---

    Here's an updated code

    package date;
     
     
    public class ExtendedDate extends Date {
     
     
          public int newMonth;
          public int newDay;
          public int newYear;
     
     
     	// Constructor that accepts parameters
    	public ExtendedDate(int newMonth, int newDay, int newYear) {
     
     
                    newMonth = 1;
    		newDay = 1;
    		newYear = 1900;
     
     
     
     
    	}
     
    	// Method to set the date – override inherited setDate method
    	// If data is invalid, do not change the receiver
        /**
         *
         * @param monthInt
         * @param dayInt
         * @param yearInt
         */
        @Override
    	public void setDate(int newMonth, int newDay, int newYear) {
     
     
     
     
                    this.newMonth = newMonth;
    		this.newDay = newDay;
    		this.newYear = newYear;
     
     
     
    			}
     
    	// Method to set month ensuring that only valid changes are made
    	/**
         *
         * @param newMonth
         */
        public void setMonth(int newMonth) {
     
            if(isValidDate(newMonth, newDay, newYear)){
     
     
            this.newMonth = newMonth;
     
     
     
     
     
     
        }
     
        else {
     
           this.newMonth =(1); 
     
     
     
            }
     
     
     
      }
     
     
     
     
    	// Method to set day ensuring that only valid changes are made
    	public void setDay(int newDay) {
     
     
            if(isValidDate(newMonth, newDay, newYear)){  
     
     
           this.newDay = newDay;
     
           if(newDay == 30 && newMonth == 2 ){
     
            newMonth = 3;
     
            }
           if(newDay == 31 && newMonth == 2 ){
     
            newMonth = 3;
     
            }
     
     
     
     
           if(newDay == 31 && newMonth == 9 ){
     
            newMonth = 10;
     
            }
     
           if(newDay == 31 && newMonth == 4 ){
     
            newMonth = 5;
     
            }
     
           if(newDay == 31 && newMonth == 6 ){
     
            newMonth = 7;
     
            }
     
           if(newDay == 31 && newMonth == 11 ){
     
            newMonth = 12;
     
            }
     
     
     
     
     
     
            }else {
     
           this.newDay =(1); 
     
     
     
            }
     
     
     
     
     
    	}
     
    // Method to set year ensuring that only valid changes are made
    	public void setYear(int newYear) {
     
     
     
            if(isValidDate(newMonth, newDay, newYear)){
     
     
           this.newYear = newYear;
     
     
            } else {
     
           this.newYear =(1); 
     
     
     
            }
     
     
     
     
     
           }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    // New method to verify a date
    	// Valid range is 1-1-1900 through 12-31-2999
     
     
     
             /**
         *
         * @param newMonth
         * @param newDay
         * @param newYear
         * @return
         */
        public static boolean isValidDate(int newMonth, int newDay, int newYear) {
     
     
     
               if ( newMonth < 13 ||  newMonth > 0){
     
     
     
                                      return true;
     
     
     
                                }
     
               if (newDay < 31 || newDay >0){
     
               return true;
     
     
               }
     
              if (newYear > 1899 || newYear < 2999) {
     
     
              return true;
     
     
              }
     
     
     
     
            return false;
     
     
     
     
     
     
     
     
             }
     
     
     
     
     
     
     
    // Method to test for a leap year
    	public static boolean isLeapYear(int newYear) {
     
                if (newYear == newYear*4){
     
    		return true;
     
                }
                return false;
    	}
     
    // Instance method version of the above
    	public boolean isLeapYear() {
    		return true;
    	}
     
    // Return number of days in a month
    	public int daysInMonth() {
    		return 0;
    	}
     
        // Return number of days passed in year (includes current day)
    	public int daysToDate() {
    		return 0;
    	}
     
            // Return number of days remaining in year
        /**
         *
         * @return
         */
        @Override
    	public int daysRemainingInYear() {
    		return 0;
    	}
     
     
     
     
     
     
     
     
    }


    --- Update ---

    Here's another update.


    package date;
     
     
    public class ExtendedDate extends Date {
     
     
          public int newMonth;
          public int newDay;
          public int newYear;
     
     
     	// Constructor that accepts parameters
    	public ExtendedDate(int newMonth, int newDay, int newYear) {
     
     
                    newMonth = 1;
    		newDay = 1;
    		newYear = 1900;
     
     
     
     
    	}
     
    	// Method to set the date – override inherited setDate method
    	// If data is invalid, do not change the receiver
        /**
         *
         * @param monthInt
         * @param dayInt
         * @param yearInt
         */
        @Override
    	public void setDate(int newMonth, int newDay, int newYear) {
     
     
     
     
                    this.newMonth = newMonth;
    		this.newDay = newDay;
    		this.newYear = newYear;
     
     
     
    			}
     
    	// Method to set month ensuring that only valid changes are made
    	/**
         *
         * @param newMonth
         */
        public void setMonth(int newMonth) {
     
            if(isValidDate(newMonth, newDay, newYear)){
     
     
            this.newMonth = newMonth;
     
     
     
     
     
     
        }
     
        else {
     
           this.newMonth =(1); 
     
     
     
            }
     
     
     
      }
     
     
     
     
    	// Method to set day ensuring that only valid changes are made
    	public void setDay(int newDay) {
     
     
     
     
                 if(isLeapYear () && newMonth == 2 && newDay > 29){
     
     
                  newDay = 30;  
     
     
     
                }
     
     
     
            if(isValidDate(newMonth, newDay, newYear)){  
     
     
           this.newDay = newDay;
     
           if(newDay == 30 && newMonth == 2 ){
     
            newMonth = 3;
     
            }
     
           if(newDay == 31 && newMonth == 2 ){
     
            newMonth = 3;
     
            }
     
           if (newDay > 29 && newYear %4 == 0 && newMonth == 2){
     
           newDay = 30;
     
     
     
           }
     
     
     
           if(newDay == 31 && newMonth == 9 ){
     
            newMonth = 10;
     
            }
     
           if(newDay == 31 && newMonth == 4 ){
     
            newMonth = 5;
     
            }
     
           if(newDay == 31 && newMonth == 6 ){
     
            newMonth = 7;
     
            }
     
           if(newDay == 31 && newMonth == 11 ){
     
            newMonth = 12;
     
            }
     
     
     
     
     
     
            }else {
     
           this.newDay =(1); 
     
     
     
            }
     
     
     
     
     
    	}
     
    // Method to set year ensuring that only valid changes are made
    	public void setYear(int newYear) {
     
     
     
     
     
            if(isValidDate(newMonth, newDay, newYear)){
     
     
     
     
     
           this.newYear = newYear;
     
     
            } else {
     
           this.newYear =(1); 
     
     
     
            }
     
     
     
     
     
           }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    // New method to verify a date
    	// Valid range is 1-1-1900 through 12-31-2999
     
     
     
             /**
         *
         * @param newMonth
         * @param newDay
         * @param newYear
         * @return
         */
        public static boolean isValidDate(int newMonth, int newDay, int newYear) {
     
     
     
               if ( newMonth < 13 ||  newMonth > 0){
     
     
     
                                      return true;
     
     
     
                                }
     
               if (newDay < 31 || newDay >0){
     
               return true;
     
     
               }
     
              if (newYear > 1899 || newYear < 2999) {
     
     
              return true;
     
     
              }
     
     
     
     
            return false;
     
     
     
     
     
     
     
     
             }
     
     
     
     
     
     
     
    // Method to test for a leap year
    	public static boolean isLeapYear(int newYear) {
     
     
     
                if (newYear % 4 == 0){
     
     
    		return true;
     
                }
                return false;
    	}
     
    // Instance method version of the above
    	public boolean isLeapYear() {
    		return true;
    	}
     
    // Return number of days in a month
    	public int daysInMonth() {
    		return 0;
    	}
     
        // Return number of days passed in year (includes current day)
    	public int daysToDate() {
    		return 0;
    	}
     
            // Return number of days remaining in year
        /**
         *
         * @return
         */
        @Override
    	public int daysRemainingInYear() {
    		return 0;
    	}
     
     
     
     
     
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help with Date change

    How do i go about setting this up within my code?
    Can you explain what you are trying to do? For example given a date, what results do you want?
    What parts of the date would you use to obtain those results?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. change DATE
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2012, 11:44 AM
  2. Java Date Format in Date Object
    By Ashr Raza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 13th, 2012, 10:47 AM
  3. Parsing a full date/time/timezone date to "yyyy-MM-dd"
    By Occidentally in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 4th, 2012, 08:57 AM
  4. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM
  5. same date should entered in another date field
    By shashib09 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 14th, 2011, 08:42 AM