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

無法解析自定義日期格式.(爪哇)

Custom date format cannot be parsed. (Java)(無法解析自定義日期格式.(爪哇))
本文介紹了無法解析自定義日期格式.(爪哇)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我必須在 Java 中使用自定義日期格式.它包含微秒,盡管 Java 不提供對微秒的支持.因此,我用零填充了時間模式,這在格式化時可以正常工作,但我無法使用該模式解析日期字符串.

是否有簡單的解決方法或者我必須自己處理微秒(使用字符串函數)?

@Test公共無效 testDateFormat() 拋出 ParseException {DateFormat 格式 = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS000");String theDate = format.format(new Date());//這將失敗:format.parse(theDate);}

<塊引用>

java.text.ParseException:無法解析的日期:2010-01-25-12.40.35.769000"

解決方案

不幸的是,SimpleDateFormat 中使用的模式有不同的含義,具體取決于它是用作解析器還是格式化程序.作為格式化程序,您的模式會執行預期的操作,輸出將以毫秒值結尾,格式為三位數字后跟三個 0 字符,例如:

2010-01-25-14.17.47.307000

用作解析器時,SSS"模式將匹配任意數量的數字并將上述示例解析為 307000 毫秒.解析完 ms 字段后,解析器仍會查找000"子字符串并因異常而失敗,因為您已到達輸入字符串的末尾,而沒有滿足模式的要求.

由于 SimpleDateFormat 中沒有 μs 值的模式,因此您必須編寫自己的包裝器來去除最后三個 0 字符的輸入字符串,然后再將其提供給 SimpleDateFormat.

I have to use a custom date format in Java. It contains microseconds although Java doesn't provide support for microseconds. Because of that I filled the time pattern with zeroes, which work fine when formatting, but I cannot parse date-strings with that pattern.

Is there a simple workaround or must I handle microseconds on my own (with String functions)?

@Test
public void testDateFormat() throws ParseException {
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS000");
    String theDate = format.format(new Date());
    // this will fail:
    format.parse(theDate);
}

java.text.ParseException: Unparseable date: "2010-01-25-12.40.35.769000"

解決方案

Your problem is that the pattern used in SimpleDateFormat unfortunately have different meanings depending on whether it is used as a parser or as a formatter. As a formatter, your pattern does what is expected, the output will end with the millisecond value formatted as three digits followed by three 0 characters, e.g:

2010-01-25-14.17.47.307000

Used as a parser, the "SSS" pattern will however match an arbitrary number of digits and parse the above example as 307000 ms. After having parsed the ms field, the parser will still look for a "000" substring and fail with an exception, since you've reached the end of the input string, without fulfilling the requirements of the pattern.

Since there is no pattern for a μs value in SimpleDateFormat, you will have to write your own wrapper to strip the input string for the last three 0 characters, before feeding it to SimpleDateFormat.

這篇關于無法解析自定義日期格式.(爪哇)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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:獲取當前星期幾的值)
主站蜘蛛池模板: 欧美中文 | 日韩精品视频网 | 麻豆久久久久久久 | 欧美亚洲视频在线观看 | 久久国产一区二区 | 亚洲欧美一区二区三区国产精品 | 高清国产一区二区 | 久久久久久久久久久久久久av | 福利视频一区二区 | 麻豆hd| 日韩视频在线观看一区二区 | 免费成人在线网站 | 免费毛片网站在线观看 | 国产精品1区| 国产这里只有精品 | 亚洲精品小视频在线观看 | 免费a网| 香蕉视频1024 | 欧美性视频在线播放 | 综合精品 | 精品一区二区在线观看 | 男插女下体视频 | 影音先锋成人资源 | 免费国产黄网站在线观看视频 | 午夜日韩 | 中国大陆高清aⅴ毛片 | 国产美女一区二区 | 日韩欧美视频网站 | 尤物在线| 国产一区二 | 亚洲啪啪 | 国产精品久久久久久久久免费丝袜 | 日韩插插 | 中文字幕精品视频 | 免费黄色片在线观看 | 国产精品69av | 国产精品精品视频一区二区三区 | 欧美日韩在线播放 | 欧美久久精品一级黑人c片 91免费在线视频 | 一级黄a视频 | 91精品国产自产在线老师啪 |