2016-03-14 34 views
0

我越來越非法字符模式 'O'

java.lang.IllegalArgumentException異常:非法字符模式 'O' java.text.SimpleDateFormat.compile(SimpleDateFormat.java:845)

執行此代碼時:

DateFormat format = new SimpleDateFormat("DD-MON-YY"); 

我該如何解決這個問題?

回答

2

"DD-MON-YY"不是有效的日期格式。請嘗試:

new SimpleDateFormat("dd-MMM-yy") 

有關有效格式的詳細信息,請參閱the documentation

1

你的日誌是自explainatory,

java.lang.IllegalArgumentException: Illegal pattern character 'O' 
java.text.SimpleDateFormat.compile(SimpleDateFormat.java:845) 
java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:659) 

閱讀第一線本身。 SimplDateFormat接受模式以下是一些示例。

new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z"); 
new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); 
new SimpleDateFormat("yyyy/MM/dd"); 

參考 - https://examples.javacodegeeks.com/core-java/text/java-dateformat-example/

0

你不給一個正確的日期格式。例如,你可以這樣做:

new SimpleDateFormat("dd-MMM-yy") 

但不

new SimpleDateFormat("DD-MON-YY");