久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 DateTimeFormatter 解析非標準月份名稱

How to parse non-standard month names with DateTimeFormatter(如何使用 DateTimeFormatter 解析非標準月份名稱)
本文介紹了如何使用 DateTimeFormatter 解析非標準月份名稱的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我需要解析以下形式的(德語)日期:

I need to parse (German) dates that come in the following form:

10. Jan. 18:14
8. Feb. 19:02
1. M?r. 19:40
4. Apr. 18:55
2. Mai 21:55
5. Juni 08:25
5. Juli 20:09
1. Aug. 13:42
[...]

如您所見,如果月份的字符數超過 4 個,則月份名稱會被刪減.更奇怪的是,不要問我為什么,盡管全名是 M?rz,但 3 月份縮短為 M?r..如何用 java.time 解析這個?(日期是根據創建日期列表的 android 設備的本地化格式化的.但是,我不會在 Android 上解析它)

As you can see, the month names are cut if the month has more than 4 characters. Even weirder, don't aks me why, the month of March is shortened to M?r. although the whole name is M?rz. How can I parse this with java.time? (The dates are formatted based on the localization of the android device that creates the list of dates. However, I'm not parsing it on Android)

我的方法是像這樣創建一個 DateTimeFormatter:

My approach was to create a DateTimeFormatter like this:

DateTimeFormatter.ofPattern("d. MMMM HH:mm").withLocale(Locale.GERMAN);
// or
DateTimeFormatter.ofPattern("d. MMMMM HH:mm").withLocale(Locale.GERMAN);

但是 MMMMMMMMM 模式都不適合縮短的日期.當然,我可以使用以下模式 d.嗯.HH:mm 來匹配縮短的月份,但是我無法匹配 3 和 4 個字符的月份.我知道我可以有兩個格式化程序(MMM. 和 MMMMM),但我寧愿有一個解決方案,我只有一個格式化程序,可能還有一個自定義語言環境或類似的東西.

But neither the MMMM nor the MMMMM pattern fit the dates that are shortened. I can, of course, have the following pattern d. MMM. HH:mm to match the shortened months, but then I can't match the 3 and 4 characters months. I am aware that I can have two formatters (MMM. and MMMMM) but I would rather have a solution where I have only one formatter and possibly a custom locale or something like this.

推薦答案

問題的答案是DateTimeFormatterBuilder 類和 appendText(TemporalField, Map) 方法.它允許任何文本在格式化或解析時與一個值關聯,從而有效而優雅地解決問題:

The answer to the problem is the DateTimeFormatterBuilder class and the appendText(TemporalField, Map) method. It allows any text to be associated with a value when formatting or parsing, which solves the problem effectively and elegantly:

Map<Long, String> monthNameMap = new HashMap<>();
monthNameMap.put(1L, "Jan.");
monthNameMap.put(2L, "Feb.");
monthNameMap.put(3L, "Mar.");
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
    .appendPattern("d. ")
    .appendText(ChronoField.MONTH_OF_YEAR, monthNameMap)
    .appendPattern(" HH:mm")
    .parseDefaulting(ChronoField.YEAR, 2016)
    .toFormatter();

System.out.println(LocalDateTime.parse("10. Jan. 18:14", fmt));
System.out.println(LocalDateTime.parse("8. Feb. 19:02", fmt));

一些注意事項:

  • monthNameMap 必須填寫所有 12 個月
  • 格式化程序通常應分配給靜態最終常量,而不是一直創建
  • 已添加 parseDefaulting(YEAR, 2016),以便可以直接使用 LocalDateTime.parse(String, DateTimeFormatter).沒有它,就沒有年份,因此只能解析 TemporalAccessor (年份必須是閏年,以防解析 2 月 29 日)
  • The monthNameMap must be populated with all 12 months
  • The formatter should normally be assigned to a static final constant, rather than being created all the time
  • The parseDefaulting(YEAR, 2016) has been added so that LocalDateTime.parse(String, DateTimeFormatter) can be used directly. Without it, there would be no year, and thus nothing more than a TemporalAccessor could be parsed (the year must be a leap year, in case 29th Feb is being parsed)

這篇關于如何使用 DateTimeFormatter 解析非標準月份名稱的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 久久综合狠狠综合久久综合88 | 伊伊综合网 | 97精品一区二区 | 亚洲精品大片 | 国产特级毛片 | 性高湖久久久久久久久 | 久草资源在线 | 亚洲成人黄色 | 99九九视频| 91在线精品播放 | 欧美最猛性xxxxx亚洲精品 | 国产精品视频免费观看 | 欧美人人 | 免费精品视频在线观看 | 一区二区在线免费观看 | 国产精品久久久久久高潮 | 中文字幕第100页 | 国产精品一区一区三区 | 久久不卡日韩美女 | 欧美一级片在线观看 | 九九99精品 | 一区在线播放 | 男女国产视频 | 国户精品久久久久久久久久久不卡 | 欧美一级片在线看 | 一区二区在线 | 久久久亚洲精品视频 | 免费成人在线网站 | 天天躁日日躁狠狠的躁天龙影院 | 亚洲欧美日韩中文字幕一区二区三区 | 国产乱码精品一区二区三区忘忧草 | 一级大片网站 | 国产精品一区二区av | 一级黄色播放 | 99精品欧美一区二区三区 | 欧美特级黄色 | 国产一区亚洲 | 国产精品免费观看视频 | 国产乱码精品1区2区3区 | 欧美日韩一区二区三区四区 | 亚洲一区二区在线 |