Skip to main content
Possible suggestion of what could be done
Source Link
ARNON
  • 441
  • 3
  • 21

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?

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 "";
}

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?

Adjust to Code Review standards
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

Code cleanup for months of the year in a Java file Convert month name to numeric string

Given this static String: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 "";
}

I'm not very familiar with Java, but I do know that it's possible to create an array (or Enum?) listing the month names and their respective index (or index + 1) and avoid this excess of if.

How would you do it?

Code cleanup for months of the year in a Java file

Given this static 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 "";
}

I'm not very familiar with Java, but I do know that it's possible to create an array (or Enum?) listing the month names and their respective index (or index + 1) and avoid this excess of if.

How would you do it?

Convert month name to numeric string

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 "";
}
Source Link
ARNON
  • 441
  • 3
  • 21

Code cleanup for months of the year in a Java file

Given this static 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 "";
}

I'm not very familiar with Java, but I do know that it's possible to create an array (or Enum?) listing the month names and their respective index (or index + 1) and avoid this excess of if.

How would you do it?