Hi guys am trying to create a calender that can output the date in three multiple formats In the first case the constructor should receive three integer values. In the
second case it should receive a String and two integer values. In the third case it should
receive two integer values, the first of which represents the day number in the year. I have an error on the line highlighted in red and I am not able to build my program successfully any help will be much appreciated?

// 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

   private int theDay;
   private String theMonth;

   // 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

   // To do - write two more constructors
    public Date( String theMonth, int theDay, int theYear )
             {
      month = if (checkMonth.equals(theMonth)); // validate month
      year = theYear; // could validate year
      day = checkDay( theDay ); // validate day

      System.out.printf(
         "Date object constructor for date %s\n", this );
   } // en
    public Date( int theDayOfYear, int theYear )
             {
      day = checkDay( theDay ); // validate day
      year = theYear; // could validate year
     

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

   
   // 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
   // To do - modify the toUniversalString to print out different date format
   public String toUniversalString()
   { 
      return String.format( "%d/%d/%d", month, day, year ); 
   } // end method toUniversalString

   // To do - modify the toString to print out different date format
    public String toString()
   {
      return String.format( "%d:%02d:%02d", day, month,year);
    }
    public static void main( String[] args )
   {
           Date date = new Date( 7, 2, 1949 );
	  // To do - write code for the constructor and then call them here
	   Date date1 = new Date("January", 12, 1988);
	   Date date2 = new Date(188, 2001);

          System.out.println( date.toUniversalString() );
	  System.out.println( date1 );
	  System.out.println( date2 );
   } // end main
} // end class Date

Recommended Answers

All 2 Replies

how are you, what did you last week, your last thread largely dissipated, last two weeks shows a few threads about (except yours) java.util.Date and how to test/convert/return Month in String as Month Integer

hmmm back to ... your syntax isn't valid for java.util.Date, near to

new GergorianCalendar(2011, Calendar.MARCH, 11)
new GergorianCalendar(2011, 3, 11)

i began the whole project over again this time I got it

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.