Java 从月份名称获取月份编号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24355504/
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
Get month number from month name
提问by user3765475
I have this.
我有这个。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String monthName = br.readLine();
How to get month number which contain in monthName variable? Thanks!
如何获取包含在monthName变量中的月份数?谢谢!
采纳答案by Matt3o12
Use Java's Calendarclass. It can parse any given string into a valid calendar instance. Here is an example (assuming that the month is in english).
使用 Java 的Calendar类。它可以将任何给定的字符串解析为有效的日历实例。这是一个例子(假设月份是英文的)。
Date date = new SimpleDateFormat("MMMM").parse(monthName);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
println(cal.get(Calendar.MONTH));
You can specify the language in SimpleDateFormat:
您可以在SimpleDateFormat 中指定语言:
String monthName = "M?rz"; // German for march
Date date = new SimpleDateFormat("MMMM", Locale.GERMAN).parse(monthName);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
println(cal.get(Calendar.MONTH));
By default, Java uses the user's local to parse the string.
默认情况下,Java 使用用户的本地来解析字符串。
Keep in mind that a computer starts counting at 0. So, January will be 0. If you want a human readable date, you should format the calendar instance:
请记住,计算机从 0 开始计数。因此,一月将为 0。如果您想要一个人类可读的日期,您应该格式化日历实例:
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMM");
Calendar cal = Calendar.getInstance();
cal.setTime(inputFormat.parse(monthName));
SimpleDateFormat outputFormat = new SimpleDateFormat("MM"); // 01-12
println(outputFormat.format(cal.getTime()));
回答by Meno Hochschild
Another approach using java.text.DateFormatSymbols
is this:
另一种使用方法java.text.DateFormatSymbols
是:
public static int monthAsNumber(
String month,
Locale locale,
boolean abbreviated,
boolean caseInsensitive
) {
DateFormatSymbols dfs = new DateFormatSymbols(locale);
String[] months = (abbreviated ? dfs.getShortMonths() : dfs.getMonths());
if (caseInsensitive) {
for (int i = 0; i < 12; i++) {
if (months[i].equalsIgnoreCase(month)) {
return i; // month index is zero-based as usual in old JDK pre 8!
}
}
} else {
for (int i = 0; i < 12; i++) {
if (months[i].equals(month)) {
return i; // month index is zero-based as usual in old JDK pre 8!
}
}
}
return -1; // no match
}
The proposed signature of seach method illustrates the many possible variations. Example:
建议的搜索方法签名说明了许多可能的变化。例子:
System.out.println(monthAsNumber("M?RZ", Locale.GERMANY, false, true));
// output: 2 (zero-based!)
If you want a month number starting with 1 then just add 1 to the result (more intuitive and also my recommendation).
如果你想要一个以 1 开头的月份数字,那么只需在结果中加 1(更直观,也是我的建议)。
Starting with Java 8you have a new variation, too, namely stand-alone months. While in English these month names are identical in other languages they are not always identical (for example in Czech language "leden" (January) instead of "ledna"). To achieve these stand-alone forms you can use Month.getDisplayName(...)(not tested):
从Java 8开始,您还有一个新的变体,即独立月份。虽然在英语中这些月份名称在其他语言中是相同的,但它们并不总是相同的(例如在捷克语中“leden”(一月)而不是“ledna”)。要实现这些独立表单,您可以使用Month.getDisplayName(...)(未测试):
public static int monthAsNumber(
String month,
Locale locale,
boolean abbreviated,
boolean caseInsensitive,
boolean standAlone
) {
TextStyle style;
Month[] months = Month.values[];
if (abbreviated) {
style = standAlone ? TextStyle.SHORT_STANDALONE : TextStyle.SHORT;
} else {
style = standAlone ? TextStyle.FULL_STANDALONE : TextStyle.FULL;
}
if (caseInsensitive) {
for (int i = 0; i < 12; i++) {
if (months[i].getDisplayName(style, locale).equalsIgnoreCase(month)) {
return i; // month index is zero-based as usual in old JDK pre 8!
}
}
} else {
for (int i = 0; i < 12; i++) {
if (months[i].getDisplayName(style, locale).equals(month)) {
return i; // month index is zero-based as usual in old JDK pre 8!
}
}
}
return -1; // no match
}
回答by Prince
If you have month name and you want integer number corresponding to that month try this
如果您有月份名称并且想要与该月份对应的整数,请尝试此操作
try{
Date date = new SimpleDateFormat("MMM").parse(monthName);//put your month name here
Calendar cal = Calendar.getInstance();
cal.setTime(date);
monthNumber=cal.get(Calendar.MONTH);
System.out.println(monthNumber);
}
catch(Exception e)
{
e.printStackTrace();
}
回答by Yuriy Tumakha
Solution in Java 8 for English month names.
Java 8 中英文月份名称的解决方案。
private int getMonthNumber(String monthName) {
return Month.valueOf(monthName.toUpperCase()).getValue();
}