Home > Enterprise >  Problem with being able to connect coding
Problem with being able to connect coding

Time:05-17

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("d",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 writing a String and somehow connect it to the return statement. But I don't know how. Any ideas?

CodePudding user response:

First up, you need to give that array of String objects a name, for example

private static String []  monthNames =new String [] {
      "Jan" , "Feb" , "March" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep"             
      ,"Oct"  "Nov" , "Dec"
    };

Then, you'll have to refer to it by name when you want to use it.

return i2s(getDay())   "/"   monthNames[getMonth() -1]   "/"   i2s(getYear());

You don't need Date. in front of the calls to i2s when you call it from a subclass of the class where it's defined.

CodePudding user response:

The Answer by Kareem is correct.

java.time

Moreover, all of your code is superfluous. You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310. These modern classes provide all the functionality you are writing.

No need for your array of month names. You can get the localized name of month from the Month enum.

String output = Month.DECEMBER.getDisplayName( TextStyle.SHORT , Locale.US ) ; 

Dec

For a date-only value, without time of day, and without time zone, use LocalDate.

LocalDate ld = LocalDate.of ( 2022 , Month.MAY , 23 ) ;

Or use month number. Note that, unlike the legacy classes, the month numbering is sane: 1-12 for January-December.

LocalDate ld = LocalDate.of ( 2022 , 9 , 23 ) ;  // 9 = September.

Generate text in standard ISO 8601 format, YYYY-MM-DD.

String output = ld.toString() ;

Generate text in automatically localized format.

Locale locale = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofLocalized( FormatStyle.SHORT ).withLocale( locale ) ;
String output = ld.format( f ) ;

Or you can specify a custom formatter using DateTimeFormatter or DateTimeFormatterBuilder.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-uuuu" ).withLocale( Locale.US ) ;
String output = ld.format(f ) ;

See this code run live at Ideone.com.

27-Sep-2022

Search to learn more, as all this has been covered many times already.

  •  Tags:  
  • java
  • Related