Click here to Skip to main content
15,357,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following coding

import java.util.Calendar;
/**/
public class Date {
  /**/
  private int month, day, year;
  /**/
  public Date () {
    /**/
    init();
  }
  /*
   *  0 ... 11
   */
  public int getMonth () {
    return month;
  }
  /*
   *  1 ... 31
   */
  public int getDay () {
    return day;
  }
  /**/
  public int getYear () {
    return year;
  }
  /**/
  public String toString () {
    /**/
    return i2s(month+1) + "/"
         + i2s(day)     + "/"
         + year;
  }
  /*
   *  i = 0 ... 31
   *  return "00" ... "31"
   */
  public static String i2s ( int i ) {
    return String.format("%02d",i);
  }
  /**/
  private void init () {
    /**/
    Calendar c;
    /**/
    c=Calendar.getInstance();
    month=c.get(Calendar.MONTH);
    day=c.get(Calendar.DAY_OF_MONTH);
    year=c.get(Calendar.YEAR);
    /**/
    return;
  }
}

And I have this one
public class RunDate {
  /**/
  public static void main ( String [] arg ) {
    /**/
    int option;
    Date d;
    /**/
    option=option();
    if ( option == 1 ) d=new Date();
    else               d=new MilitaryDate();
    /**/
    System.out.println();
    System.out.println(d);
    /**/
    return;
  }
  /**/
  private static int option () {
    /**/
    int rv;
    ConsoleInput ci;
    /**/
    ci=new ConsoleInput();
    System.out.println();
    System.out.println("Enter option:");
    System.out.println(" Standard = 1");
    System.out.println(" Military = 2");
    while ( true ) {
      rv=ci.readInt("          ? ");
      if ( ( rv == 1 ) || ( rv == 2 ) ) break;
    }
    ci.close();
    /**/
    return rv;
  }
}

This one just runs this file

What I have tried:

public class MilitaryDate extends Date {
       private static String []        =new String [] {
          "Jan" , "Feb" , "March" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep"             
          ,"Oct"  "Nov" , "Dec"
        };
        public MilitaryDate() {
           super();
        }
        @Override
        public String toString () {
          return Date.i2s(getDay()) + "/" Date.i2s(getMonth+1) + "/"
         + Date.i2s(getYear())    
      }


Basically I don't know how to properly write this code so the month changes to written instead of number form. What I believe is writting a String and somehow connect it to the return statement. But I don't know how. Any ideas?
Posted
Updated 16-May-22 18:58pm

1 solution

The first thing to do is get your code to compile ... which this won't:
Java
private static String []        =new String [] {
          "Jan" , "Feb" , "March" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep"             
          ,"Oct"  "Nov" , "Dec"
        };
Remember back to your early lessons in Java, and you'll recall that the format for declaring a variable is pretty simple:
Java
<access modifier> <static if needed> <type> <variable name> = <initial value>;
For example:
Java
public int maxItems = 10;
Or
Java
private static string prompt = "Enter your name: ";
Your line is missing the <variable name> part between the "[]" and the "=".
You also need a comma between "Oct" and "Nov"
Try this:
Java
private static String [] monthNames = new String [] {
          "Jan", "Feb", "March", "Apr", "May", "Jun", "Jul", "Aug", "Sep"             
          , "Oct", "Nov", "Dec"
        };
And it should at least compile.
Then all you need to do is access the names using the month number (starting with January as zero) like any other array.
Java
string textMonth = monthNames[Date.i2s(getMonth)];
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900