第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將月份的字符串(例如“一月”、“二月”)轉(zhuǎn)換為整數(shù)值?

如何將月份的字符串(例如“一月”、“二月”)轉(zhuǎn)換為整數(shù)值?

慕容3067478 2021-12-01 18:52:41
我有一個(gè)名為 Incidents 的項(xiàng)目,它具有以下變量、值、月、年和郵政編碼。由于偏好,我選擇編寫(xiě) String 類(lèi)型的月份變量,但我知道有些人更喜歡將其寫(xiě)為整數(shù)。我想知道我會(huì)怎么寫(xiě)?目前,我對(duì)每個(gè)實(shí)例只有簡(jiǎn)單的訪問(wèn)器和修改器方法,所以我需要編寫(xiě)一個(gè)新方法來(lái)轉(zhuǎn)換它嗎?我會(huì)使用 parseInt,語(yǔ)法如何工作,還是使用 java 庫(kù)?我的主要問(wèn)題是弄清楚語(yǔ)法是如何工作的。
查看完整描述

3 回答

?
慕桂英4014372

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


查看完整回答
反對(duì) 回復(fù) 2021-12-01
?
MYYA

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);


查看完整回答
反對(duì) 回復(fù) 2021-12-01
?
紫衣仙女

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());


    }

}


查看完整回答
反對(duì) 回復(fù) 2021-12-01
  • 3 回答
  • 0 關(guān)注
  • 305 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)