将字符串月份转换为整数,即将“may”转换为5,在Android Studio中。

huangapple go评论88阅读模式
英文:

Converting a string month in to an integer aka may to 5 within android studio

问题

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;
英文:

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.

答案1

得分: 4

给定输入的格式是ISO MMM,就使用它

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

很容易得到月份的数字(2),但更好的方法是获得Month枚举,因为这鼓励您继续使用java.time API 来解决日历问题!

英文:

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!

答案2

得分: 1

你可以创建一个从字符串到月份值的映射,其中字符串是你要查找的键,而Month是相对于该键的月份。

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

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

if (month == null) {
    throw new IllegalStateException("unexpected date key");
}
int monthValue = month.getValue();
英文:

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&lt;String, Month&gt; monthsByDisplay = Stream.of(Month.values())
        .collect(Collectors.toMap(m -&gt; m.getDisplayName(TextStyle.SHORT, Locale.ENGLISH), Function.identitity());
    

Month month = monthsByDisplay.get(&quot;Jan&quot;);

if (month == null) {
    throw new IllegalStateException(&quot;unexpected date key&quot;);
}
int monthValue = month.getValue();

答案3

得分: 1

你应该使用java.time.Month枚举(请参阅此处的javadoc)。getValue()方法将为您提供月份的数值,范围为1-12。

英文:

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

huangapple
  • 本文由 发表于 2020年8月25日 20:23:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63578792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定