Hey Guys, I am working on an assignment where we are supposed to output the date in different formats for example, month, date, year or date,month year, I have implemented a method to change for the arrangement of dates, It builds successfully but has errors when it runs plus it does not output the info in a different format. I have attached the application folder if you need to refer to other classes.


here is my code

// Fig. 8.7: Date.java 
// Date class declaration.

public class Date 
{
   private int month; // 1-12
   private int day; // 1-31 based on month
   private int year; // any year

   // constructor: call checkMonth to confirm proper value for month; 
   // call checkDay to confirm proper value for day
   public Date( int theMonth, int theDay, int theYear )
   {
      month = checkMonth( theMonth ); // validate month
      year = theYear; // could validate year
      day = checkDay( theDay ); // validate day

      System.out.printf( 
         "Date object constructor for date %s\n", this );
   } // end Date constructor

   // utility method to confirm proper month value
   private int checkMonth( int testMonth )
   {
      if ( testMonth > 0 && testMonth <= 12 ) // validate month
         return testMonth;
      else // month is invalid 
      { 
         System.out.printf( 
            "Invalid month (%d) set to 1.", testMonth );
         return 1; // maintain object in consistent state
      } // end else
   } // end method checkMonth

   // utility method to confirm proper day value based on month and year
   private int checkDay( int testDay )
   {
      int[] daysPerMonth = 
         { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
   
      // check if day in range for month
      if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
         return testDay;
   
      // check for leap year
      if ( month == 2 && testDay == 29 && ( year % 400 == 0 || 
           ( year % 4 == 0 && year % 100 != 0 ) ) )
         return testDay;
   
      System.out.printf( "Invalid day (%d) set to 1.", testDay );
      return 1;  // maintain object in consistent state
   } // end method checkDay
   
   // return a String of the form month/day/year
   public String toUniversalString()
   { 
      return String.format( "%d/%d/%d", month, day, year ); 
   } // end method toUniversalString
    @Override
    public String toString()
   {
      return String.format( "%d:%02d:%02d %s", day, month,year);
    }

} // end class Date

cant u just use the default date class? or Calendar class? (or both together?)

cant u just use the default date class? or Calendar class? (or both together?)

just the default date class

search for SimpleDateFormat (returns String)

"has errors when it runs" is not enough info for anybody to conclude anything. You must be specific. What errors? If an Exception give whole message and line number(s). If incorrect output give expected and actual results.
ps mKorbel - I'm assuming that this is a training exercise so SimpleDateFormat wouldn't be acceptable, despite the fact that that is what you would use in real life.

@ JamesCherrill

yes you are right, there are exist one, two, wait sec finally three methods, hmmm just only one has NumberInstance (and also empty 13. month) but errrghh "Urgent flag" don't allows me continue with discusions about that in this nice thread :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.