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

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

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

問題描述

限時送ChatGPT賬號..

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

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

@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 中使用的模式有不同的含義,具體取決于它是用作解析器還是格式化程序.作為格式化程序,您的模式會執(zhí)行預(yù)期的操作,輸出將以毫秒值結(jié)尾,格式為三位數(shù)字后跟三個 0 字符,例如:

2010-01-25-14.17.47.307000

用作解析器時,SSS"模式將匹配任意數(shù)量的數(shù)字并將上述示例解析為 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.

這篇關(guān)于無法解析自定義日期格式.(爪哇)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
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 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 国产99久久久国产精品 | 亚洲视频一区在线观看 | 日韩免费视频 | 成人在线免费 | 99久久精品免费看国产免费软件 | 中文字幕一区二区三区在线乱码 | 成人免费影院 | 亚洲精品久久久久中文字幕欢迎你 | 亚洲成人自拍网 | 一区二区国产精品 | 国产精品成人一区二区三区夜夜夜 | 看亚洲a级一级毛片 | 国产视频一区二区 | av片免费观看 | 91高清在线 | 一区中文字幕 | 久久av资源网 | 久久精品在线 | 免费观看一级特黄欧美大片 | 国产成人自拍一区 | 成人在线精品视频 | 美女一级a毛片免费观看97 | 成人毛片网 | 日韩久久久久久久 | 91看片官网 | 亚洲精品视频在线观看免费 | 日本成人在线播放 | 九九热在线观看视频 | 精品亚洲91| 欧美日韩国产一区 | 国产欧美日韩在线 | 亚洲成人网在线播放 | 欧美精品一区二区三区四区 在线 | 中文字幕国产第一页 | 免费在线观看av网址 | 草草精品 | 欧美日韩国产三级 | 2019天天操 | 久久综合九九 | 精品欧美在线观看 | 正在播放国产精品 |