在 Java 中将月份字符串转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2268969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Month String to Integer in Java
提问by Dylan Cali
Given a month string such as:
给定一个月份字符串,例如:
"Feb"
or
"February"
Is there any core Java or third party library functionality that would allow you to convert this string to the corresponding month number in a locale agnostic way?
是否有任何核心 Java 或第三方库功能允许您以与语言环境无关的方式将此字符串转换为相应的月份数?
采纳答案by Dylan Cali
An alternative to SimpleDateFormat using Joda time:
使用 Joda 时间的 SimpleDateFormat 的替代方法:
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
...
// if default locale is ok simply omit '.withLocale(...)'
DateTimeFormatter format = DateTimeFormat.forPattern("MMM");
DateTime instance = format.withLocale(Locale.FRENCH).parseDateTime("ao?t");
int month_number = instance.getMonthOfYear();
String month_text = instance.monthOfYear().getAsText(Locale.ENGLISH);
System.out.println( "Month Number: " + month_number );
System.out.println( "Month Text: " + month_text );
OUTPUT:
Month Number: 8
Month Text: August
回答by Paul Tomblin
回答by Bart Kiers
You could parse the month using SimpleDateFormat:
您可以使用 SimpleDateFormat 解析月份:
Date date = new SimpleDateFormat("MMM", Locale.ENGLISH).parse("Feb");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
System.out.println(month == Calendar.FEBRUARY);
Be careful comparing int month
to an int (it does notequal 2!). Safest is to compare them using Calendar
's static fields (like Calendar.FEBRUARY
).
小心比较int month
为int(它不等于2!)。最安全的是使用Calendar
的静态字段(如Calendar.FEBRUARY
)比较它们。
回答by mindas
Java 8 solution:
Java 8 解决方案:
DateTimeFormatter parser = DateTimeFormatter.ofPattern("MMM")
.withLocale(Locale.ENGLISH);
TemporalAccessor accessor = parser.parse("Feb");
System.out.println(accessor.get(ChronoField.MONTH_OF_YEAR)); // prints 2
回答by Carson Graham
you could just set up a switch statment, something like this (below). I'm posting this in case anyone wants a simple, easy to understand solution I know I would have wanted it before I typed this up:
你可以设置一个 switch 语句,像这样(如下)。我发布这个以防有人想要一个简单易懂的解决方案我知道在我输入之前我会想要它:
switch(input2) {
case "january":
case "jan":
input2 = "1";
break;
case "febuary":
case "feb":
input2 = "2";
break;
case "march":
case "mar":
input2 = "3";
break;
case "april":
case "apr":
input2 = "4";
break;
case "may":
input2 = "5";
break;
case "june":
case "jun":
input2 = "6";
break;
case "july":
case "jul":
input2 = "7";
break;
case "august":
case "aug":
input2 = "8";
break;
case "september":
case "sep":
case "sept":
input2 = "9";
break;
case "october":
case "oct":
input2 = "10";
break;
case "november":
case "nov":
input2 = "11";
break;
case "december":
case "dec":
input2 = "12";
break;
}