问题
I have a string that contains a date, in the following format:
dd-mm-yyyy
with the month that is all lowercased. For example: 25-aug-2019 Now i tried to use SimpleDateFormat to convert my string to a Date, but i have the following exception:
java.text.ParseException: Unparseable date: "25-aug-2019"
at java.text.DateFormat.parse(Unknown Source)
at org.whoislibrary.servers.WhoisCom.parseResponse(WhoisCom.java:37)
at org.whoislibrary.WhoisAbstract.executeQuery(WhoisAbstract.java:44)
at org.whoislibrary.WhoisCommand.executeQuery(WhoisCommand.java:69)
at org.whoislibrary.WhoisMain.main(WhoisMain.java:10)
This is the code that i used:
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
try {
Date expDate = df.parse(dateString).trim());
System.out.println(expDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
I think the problem is that MM refers to month name that start with a capital letter (Aug, Jul etc). There is an option, or a class like SimpleDateFormat that help me to convert that string into a Date. or it must be done Manually?
回答1:
Well in your code you have:
new SimpleDateFormat("dd-MM-yyyy")
Which should be:
new SimpleDateFormat("dd-MMM-yyyy")
Since the month part in your string has 3 letters (MMM) and not 2 (MM)
回答2:
use dd-MMM-yyyy
回答3:
Your date "25-aug-2019" is in "dd-MMM-yyyy"
format not "dd-MM-yyyy"
. So you get parse error. You should use "dd-MMM-yyyy"
while creating SimpleDateFormat object.
回答4:
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
try {
Date expDate = df.parse("25-aug-2019");
System.out.println(expDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
回答5:
Refer javaDocs java.text.SimpleDateFormat
来源:https://stackoverflow.com/questions/8907659/string-to-date-with-month-all-lowercase-in-java