猿问

如何将月份的字符串(例如“一月”、“二月”)转换为整数值?

我有一个名为 Incidents 的项目,它具有以下变量、值、月、年和邮政编码。由于偏好,我选择编写 String 类型的月份变量,但我知道有些人更喜欢将其写为整数。我想知道我会怎么写?目前,我对每个实例只有简单的访问器和修改器方法,所以我需要编写一个新方法来转换它吗?我会使用 parseInt,语法如何工作,还是使用 java 库?我的主要问题是弄清楚语法是如何工作的。


慕容3067478
浏览 282回答 3
3回答

慕桂英4014372

在 Java 8+ 中,您可以使用Month枚举。不幸的是,Month它parse不像大多数其他java.time类那样提供方法,但是,如果您查看该getDisplayName(TextStyle style, Locale locale)方法的实现,您会发现:public String getDisplayName(TextStyle style, Locale locale) {&nbsp; &nbsp; return new DateTimeFormatterBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .appendText(MONTH_OF_YEAR, style)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toFormatter(locale)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .format(this);}所以为了解析我们可以创建我们自己的方法,使用相同类型的格式化程序,如下所示:public static Month parseMonth(CharSequence text, TextStyle style, Locale locale) {&nbsp; &nbsp; DateTimeFormatter fmt = new DateTimeFormatterBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .appendText(ChronoField.MONTH_OF_YEAR, style)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toFormatter(locale);&nbsp; &nbsp; return Month.from(fmt.parse(text));}例子String s = Month.OCTOBER.getDisplayName(TextStyle.FULL, Locale.US);System.out.println(s); // prints: OctoberMonth month = parseMonth("October", TextStyle.FULL, Locale.US);System.out.println(month); // prints: OCTOBERSystem.out.println(month.getValue()); // prints: 10在 Java <8 中,您可以使用SimpleDateFormat解析文本:public static int parseMonth(String text) {&nbsp; &nbsp; SimpleDateFormat fmt = new SimpleDateFormat("MMMM", Locale.US);&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; Calendar cal = Calendar.getInstance();&nbsp; &nbsp; &nbsp; &nbsp; cal.setTime(fmt.parse(text));&nbsp; &nbsp; &nbsp; &nbsp; return cal.get(Calendar.MONTH) + 1;&nbsp; &nbsp; } catch (ParseException e) {&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Invalid month: " + text);&nbsp; &nbsp; }}例子int month = parseMonth("October");System.out.println(month); // prints: 10

MYYA

顺便说一句,但如果您使用枚举,您还可以根据枚举的名称进行查找。例如,一个枚举像public enum MyMonth {January, February, March, April, May }你可以这样做MyMonth month = MyMonth.valueOf("February");获取名为“二月”的月份的枚举。要获取“月数”,您可以使用以下ordinal()方法:&nbsp; MyMonth month = MyMonth.valueOf("February");&nbsp; System.out.println( month.ordinal() );由于 Java 枚举是从零开始的,因此二月打印“1”。如果您希望一月从一个开始,您必须记住添加一个。使用整数可以方便地迭代您的所有月份(或任何其他枚举),但使用values()静态方法您不需要:&nbsp;for( MyMonth m : MyMonth.values() )&nbsp;&nbsp; &nbsp; System.out.println( m );这些代码行将打印MyMonth枚举中定义的所有月份名称。这是完整文档的链接。很多人不知道这一点,因为它隐藏在 JLS 的一个有点晦涩的部分中,并且没有在 API 文档中直接引用。https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9In addition, if E is the name of an enum type,&nbsp;then that type has the following implicitly declared&nbsp;static methods:/*** Returns an array containing the constants of this enum&nbsp;* type, in the order they're declared.&nbsp; This method may be* used to iterate over the constants as follows:**&nbsp; &nbsp; for(E c : E.values())*&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(c);** @return an array containing the constants of this enum&nbsp;* 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.&nbsp; (Extraneous whitespace&nbsp;* characters are not permitted.)*&nbsp;* @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);

紫衣仙女

Java 有一个内置程序enum可以在java.time包中为您处理这个问题。你可以像这样使用它:import java.time.Month;public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // Use sample month of April&nbsp; &nbsp; &nbsp; &nbsp; Month month = Month.APRIL;&nbsp; &nbsp; &nbsp; &nbsp; // Get the month number&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Month #: " + month.getValue());&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答