This code converts a 3-letter English month abbreviation to its numeric equivalent (as a string).
public static String numericMonth(String monthInFull) {
monthInFull = monthInFull.toUpperCase();
if (monthInFull.equals("JAN")) { return "01"; }
if (monthInFull.equals("FEB")) { return "02"; }
if (monthInFull.equals("MAR")) { return "03"; }
if (monthInFull.equals("APR")) { return "04"; }
if (monthInFull.equals("MAY")) { return "05"; }
if (monthInFull.equals("JUN")) { return "06"; }
if (monthInFull.equals("JUL")) { return "07"; }
if (monthInFull.equals("AUG")) { return "08"; }
if (monthInFull.equals("SEP")) { return "09"; }
if (monthInFull.equals("OCT")) { return "10"; }
if (monthInFull.equals("NOV")) { return "11"; }
if (monthInFull.equals("DEC")) { return "12"; }
return "";
}
Is it possible to create an array (or Enum?) listing the month names and their respective index (or index + 1) and avoid this excess of if?
switchstatement would remove much of the repetition in your code. Whether or not using anenumhere is beneficial or appropriate would require more context for how/where this code is being used. \$\endgroup\$monthInFullfor three-letter abbrev.s is kind of odd. That's not a static String, but a staticStringmethod.) Please mention that run time is absolutely no concern. \$\endgroup\$return switch (monthFull.toUpperCase()) { case "JAN" => "01"; case "FEB" => "02"; ... };}\$\endgroup\$