3 回答

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊
在 Java 8+ 中,您可以使用Month枚舉。
不幸的是,Month它parse不像大多數(shù)其他java.time類(lèi)那樣提供方法,但是,如果您查看該getDisplayName(TextStyle style, Locale locale)方法的實(shí)現(xiàn),您會(huì)發(fā)現(xiàn):
public String getDisplayName(TextStyle style, Locale locale) {
return new DateTimeFormatterBuilder()
.appendText(MONTH_OF_YEAR, style)
.toFormatter(locale)
.format(this);
}
所以為了解析我們可以創(chuàng)建我們自己的方法,使用相同類(lèi)型的格式化程序,如下所示:
public static Month parseMonth(CharSequence text, TextStyle style, Locale locale) {
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendText(ChronoField.MONTH_OF_YEAR, style)
.toFormatter(locale);
return Month.from(fmt.parse(text));
}
例子
String s = Month.OCTOBER.getDisplayName(TextStyle.FULL, Locale.US);
System.out.println(s); // prints: October
Month month = parseMonth("October", TextStyle.FULL, Locale.US);
System.out.println(month); // prints: OCTOBER
System.out.println(month.getValue()); // prints: 10
在 Java <8 中,您可以使用SimpleDateFormat解析文本:
public static int parseMonth(String text) {
SimpleDateFormat fmt = new SimpleDateFormat("MMMM", Locale.US);
try {
Calendar cal = Calendar.getInstance();
cal.setTime(fmt.parse(text));
return cal.get(Calendar.MONTH) + 1;
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid month: " + text);
}
}
例子
int month = parseMonth("October");
System.out.println(month); // prints: 10

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
順便說(shuō)一句,但如果您使用枚舉,您還可以根據(jù)枚舉的名稱(chēng)進(jìn)行查找。
例如,一個(gè)枚舉像
public enum MyMonth {January, February, March, April, May }
你可以這樣做
MyMonth month = MyMonth.valueOf("February");
獲取名為“二月”的月份的枚舉。
要獲取“月數(shù)”,您可以使用以下ordinal()方法:
MyMonth month = MyMonth.valueOf("February");
System.out.println( month.ordinal() );
由于 Java 枚舉是從零開(kāi)始的,因此二月打印“1”。如果您希望一月從一個(gè)開(kāi)始,您必須記住添加一個(gè)。
使用整數(shù)可以方便地迭代您的所有月份(或任何其他枚舉),但使用values()靜態(tài)方法您不需要:
for( MyMonth m : MyMonth.values() )
System.out.println( m );
這些代碼行將打印MyMonth枚舉中定義的所有月份名稱(chēng)。
這是完整文檔的鏈接。很多人不知道這一點(diǎn),因?yàn)樗[藏在 JLS 的一個(gè)有點(diǎn)晦澀的部分中,并且沒(méi)有在 API 文檔中直接引用。
https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9
In addition, if E is the name of an enum type,
then that type has the following implicitly declared
static methods:
/**
* Returns an array containing the constants of this enum
* type, in the order they're declared. This method may be
* used to iterate over the constants as follows:
*
* for(E c : E.values())
* System.out.println(c);
*
* @return an array containing the constants of this enum
* type, in the order they're declared
*/
public static E[] values();
/**
* Returns the enum constant of this type with the specified
* name.
* The string must match exactly an identifier used to declare
* an enum constant in this type. (Extraneous whitespace
* characters are not permitted.)
*
* @return the enum constant with the specified name
* @throws IllegalArgumentException if this enum type has no
* constant with the specified name
*/
public static E valueOf(String name);

TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊
Java 有一個(gè)內(nèi)置程序enum可以在java.time包中為您處理這個(gè)問(wèn)題。
你可以像這樣使用它:
import java.time.Month;
public class Main {
public static void main(String[] args) {
// Use sample month of April
Month month = Month.APRIL;
// Get the month number
System.out.println("Month #: " + month.getValue());
}
}
添加回答
舉報(bào)