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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將西方日期格式轉(zhuǎn)換為日本日期格式?

如何將西方日期格式轉(zhuǎn)換為日本日期格式?

紫衣仙女 2023-04-13 14:20:27
我有一個String日期。這個日期是西方日期格式。我想要的是日文格式。String a = "2019-05-10";DateTimeFormatter timeFormatter = new DateTimeFormatterBuilder()            .appendPattern("YYYY-MM-DD")            .toFormatter(Locale.JAPAN);String aa = LocalTime.parse(a, timeFormatter)            .format(DateTimeFormatter.ofPattern("YYYY年MM月DD日", Locale.JAPAN));System.out.println(aa);當前錯誤:Exception in thread "main" java.time.format.DateTimeParseException: Text '2019-05-10' could not be parsed: Unable to obtain LocalTime from TemporalAccessor: {DayOfYear=10, MonthOfYear=5, WeekBasedYear[WeekFields[SUNDAY,1]]=2019},ISO of type java.time.format.Parsed    at java.time.format.DateTimeFormatter.createError(Unknown Source)    at java.time.format.DateTimeFormatter.parse(Unknown Source)    at java.time.LocalTime.parse(Unknown Source)    at testing.DatumTypeDecimal.main(DatumTypeDecimal.java:223)Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {DayOfYear=10, MonthOfYear=5, WeekBasedYear[WeekFields[SUNDAY,1]]=2019},ISO of type java.time.format.Parsed    at java.time.LocalTime.from(Unknown Source)    at java.time.format.Parsed.query(Unknown Source)    ... 3 more預期輸出:2019年05月10日
查看完整描述

4 回答

?
隔江千里

TA貢獻1906條經(jīng)驗 獲得超10個贊

好的,這需要幾個步驟......


首先,將輸入輸入有效的日期容器(即LocalDate)


String input = "2019-05-10";

DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

LocalDate localDate = LocalDate.parse(input, inputFormatter);

接下來,構(gòu)造一個日期格式化程序,它將能夠?qū)⑷掌谌萜鞲袷交癁槲覀兿胍母袷?。請注意,我使用DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)而不是提供模式,因為它可以產(chǎn)生更好的結(jié)果


DateTimeFormatter outputFormatter = new DateTimeFormatterBuilder()

        .append(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL))

//                .appendPattern("yyyy年MM月dd日")

        .toFormatter(Locale.JAPAN);

最后,格式化日期容器...


String output = outputFormatter.format(localDate);

System.out.println(output);

這將產(chǎn)生2019年5月10日金曜日


此外,對于它的價值,F(xiàn)ormatStye.LONG生成2019年5月10日的似乎更符合您的目標異常


查看完整回答
反對 回復 2023-04-13
?
墨色風雨

TA貢獻1853條經(jīng)驗 獲得超6個贊

您應該為日期使用正確的類,LocalTime但不是LocalDate。此外,由于大寫Y和 ,您的模式是錯誤的D。


我嘗試過以下方式:


public static void main(String args[]) throws Exception {

    String a = "2019-05-10";    // ISO date format

    String b = "2019-5-10";     // date format with only a single digit for month

    LocalDate ldA = LocalDate.parse(a, DateTimeFormatter.ISO_DATE);

    LocalDate ldB = LocalDate.parse(b, DateTimeFormatter.ofPattern("yyyy-M-dd"));

    // note the lower case year and day, month stays upper case

    System.out.println(ldA.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN)));

    System.out.println(ldB.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN)));

}

并且輸出


2019年05月10日

為了將其存儲在 a 中String,只需執(zhí)行


String myJapaneseDate = ldA.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN));

編輯:


根據(jù)要求,這是一個使用方法 (1) 使給定的String有效 ISO 格式和 (2) 返回LocalDate從中解析的方法的示例。


public static void main(String args[]) throws Exception {

    String a = "2019-05-10";    // ISO date format

    String b = "2019-5-10";     // date format with only a single digit for month

    LocalDate ldA = makeItIsoFormat(a);

    LocalDate ldB = makeItIsoFormat(b);

    // note the lower case year and day, month stays upper case

    System.out.println(ldA.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN)));

    System.out.println(ldB.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN)));

}


public static LocalDate makeItIsoFormat(String date) {

    // first, split the input String by a hyphon

    String[] splitDate = date.split("-");


    // check its length, it has to have 3 parts (year, month, day)

    if (splitDate.length != 3) {

        // if it has more or less parts, the date is considered invalid

        System.err.println("invalid date String provided");

    } else {

        // otherwise, check the amount of digits it has for month

        if (splitDate[1].length() == 1) {

            // and if it is just one, add a leading zero

            System.out.println("The given date \"" 

                    + date 

                    + "\" has a single digit for month, add leading zero");

            splitDate[1] = "0" + splitDate[1];

        }

    }


    /*

     * CONSIDER ADDING A CHECK FOR SINGLE-DIGIT DAYS, it is not yet included

     */


    // recreate the date String

    String isoDate = splitDate[0] + "-" + splitDate[1] + "-" + splitDate[2];

    // and return the parsed LocalDate

    return LocalDate.parse(isoDate, DateTimeFormatter.ISO_DATE);

}


查看完整回答
反對 回復 2023-04-13
?
慕標5832272

TA貢獻1966條經(jīng)驗 獲得超4個贊

你可以用 2 SimpleDateFormat


例子:


String input = "2019-05-10";

Date date = new SimpleDateFormat("yyyy-MM-dd").parse(input);

System.out.println(new SimpleDateFormat("yyyy年MM月dd日").format(date));

輸出 :


2019年05月10日


查看完整回答
反對 回復 2023-04-13
?
吃雞游戲

TA貢獻1829條經(jīng)驗 獲得超7個贊

我不知道其他解決方案,但我希望這段代碼有所幫助。該方法只替換字符“-”,然后append()是最后一個。


public String japaneseFormat(String date){

   StringBuilder date =new StringBuilder(date);


   date.setCharAt(4,'年'); 

   date.setCharAt(7,'月');

   date.append("日");

   return date.toString();

}


查看完整回答
反對 回復 2023-04-13
  • 4 回答
  • 0 關注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號