如何将西方日期格式转换为日本日期格式?

我有一个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日


紫衣仙女
浏览 111回答 4
4回答

隔江千里

好的,这需要几个步骤......首先,将输入输入有效的日期容器(即LocalDate)String input = "2019-05-10";DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");LocalDate localDate = LocalDate.parse(input, inputFormatter);接下来,构造一个日期格式化程序,它将能够将日期容器格式化为我们想要的格式。请注意,我使用DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)而不是提供模式,因为它可以产生更好的结果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);这将产生2019年5月10日金曜日此外,对于它的价值,FormatStye.LONG生成2019年5月10日的似乎更符合您的目标异常

墨色风雨

您应该为日期使用正确的类,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,只需执行String myJapaneseDate = ldA.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.JAPAN));编辑:根据要求,这是一个使用方法 (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);}

慕标5832272

你可以用 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日

吃鸡游戏

我不知道其他解决方案,但我希望这段代码有所帮助。该方法只替换字符“-”,然后append()是最后一个。public String japaneseFormat(String date){   StringBuilder date =new StringBuilder(date);   date.setCharAt(4,'年');    date.setCharAt(7,'月');   date.append("日");   return date.toString();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java