0

How can I simply convert a string containing a variable of a month to a integer value?

I can do something like below but it seems like there should be a more efficient method to do so.


int month_int;
        switch (month) {//3 letter string input
            case "Jan":  month_int= 1; break;
            case "Feb":  month_int= 2; break;
            case "Mar":  month_int= 3; break;
            case "Apr":  month_int= 4; break;
            case "May":  month_int= 5; break;
            case "Jun":  month_int= 6; break;
            case "Jul":  month_int= 7; break;
            case "Sep":  month_int= 9; break;
            case "Oct":  month_int= 10;break;
            case "Nov":  month_int= 11;break;
            case "Dec":  month_int= 12;break;
            default:   month_int= 0; break;//Error
        }
        return month_int;

My input is a three letter string which I need to be converted to an int. Aka 1-12.

Thomas Morris
  • 794
  • 5
  • 26
  • how about: case "Jan" ... you already have what you want, what more do you need? – Stultuske Aug 25 '20 at 12:25
  • 1
    Is that `String` lower-case or upper-case or just the first letter upper-case? How about involving `java.time.Month`? What is the purpose of this code snippet? Is that an entire method? Where and why are you using it? I bet you will never get a `4` returned when you have a `case "Arp"` in this `switch`... – deHaar Aug 25 '20 at 12:26
  • @ADM yes but I am already using the calendar library and using an sdf already but can't change the data as that is how I receive it. Unless there is a way to use the calendar format to convert from one from format to another. – Thomas Morris Aug 25 '20 at 12:27
  • Are you receiving the date in some complete format? ISO? or you have just the month? – Markiian Benovskyi Aug 25 '20 at 12:29
  • Please consider using `java.time` instead of the outdated `java.util.Calendar` and `java.text.SimpleDateFormat`. – deHaar Aug 25 '20 at 12:29
  • Why do you think this code snippet is not efficient? – Henry Aug 25 '20 at 12:30
  • @MarkiianBenovskyi I have the complete format it is just the month I want to convert as the rest I can just parse differently. – Thomas Morris Aug 25 '20 at 12:35

3 Answers3

4

Given the incoming format is ISO MMM, go with that

private static final DateTimeFormatter MONTH_FORMAT = DateTimeFormatter.ofPattern("MMM", Locale.ENGLISH); // supports Jan, Feb etc
MONTH_FORMAT.parse("Feb").get(ChronoField.MONTH_OF_YEAR); // 2
Month month = Month.from(MONTH_FORMAT.parse("Feb")); // java.time.Month.FEBRUARY

It's easy to get the month number (2) but likely better to get the Month enum as that encourages you to continue using java.time API solve calendar problems!

drekbour
  • 2,895
  • 18
  • 28
  • 1
    This looks like a reasonnable answer on principle (if your statement that the input is ISO MMM holds). But the locale here plays a crucial role.Trying to recognize "Feb" may fail (it would work in spanish and english, but not in french, nor obviously, in any non latin language). And you should check for exceptions. – GPI Aug 25 '20 at 12:54
  • Yes, @GPI is right... Just add the `Locale` to the call to `ofPattern` and this will be the best answer imho. Do `DateTimeFormatter.ofPattern("MMM", Locale.ENGLISH)`... – deHaar Aug 25 '20 at 12:55
  • Updated, thanks - I did think about Locale but skipped over it! – drekbour Aug 25 '20 at 13:01
1

You could create a Map of String to Month values where String is the key you're looking for and Month is the relative month to the key.

Map<String, Month> monthsByDisplay = Stream.of(Month.values())
        .collect(Collectors.toMap(m -> m.getDisplayName(TextStyle.SHORT, Locale.ENGLISH), Function.identitity());
    

Month month = monthsByDisplay.get("Jan");

if (month == null) {
    throw new IllegalStateException("unexpected date key");
}
int monthValue = month.getValue();
Jason
  • 5,154
  • 2
  • 12
  • 22
1

You should use java.time.Month Enum (See javadoc here). method getValue() will give you your numeric value of the month 1 - 12

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36