Reputation: 4234
I'm developing a function that extracts a date from a String and convert it to a Date. The date have the following format:
dd-mmm-yyyy
where mmm is the month 3-digit name all lowercased.
The piece of code is the following:
if(queryLine.contains("Expiration Date:")){
String expString = queryLine.replace("Expiration Date:", "").trim();
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
try {
log.info("Exp: " + queryLine.replace("Expiration Date:", "").trim());
expDate = df.parse(expString);
} catch (ParseException e) {
e.printStackTrace();
}
If i try that code on a computer jvm it works fine and it converts the date without problem, but if i try to run it on android i have the following error:
java.text.ParseException: Unparseable date: "14-sep-2020"
at org.whoislibrary.servers.WhoisCom.parseResponse(WhoisCom.java:39)
at java.text.DateFormat.parse(DateFormat.java:626)
at org.whoislibrary.WhoisAbstract.executeQuery(WhoisAbstract.java:47)
at org.whoislibrary.WhoisCommand.executeQuery(WhoisCommand.java:72)
at org.HttpTest.HttpTestActivity.onCreate(HttpTestActivity.java:35)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3647)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Any idea?
Upvotes: 2
Views: 1761
Reputation: 60681
Try specifying an English locale when you create the SimpleDateFormat
:
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Upvotes: 1
Reputation: 3685
Not 100% sure (I will check in a moment) but I think the problem is that for MMM your month must be in proper locale too. So Sep on your PC (presumably locale set to EN) would need to be expressed in Italian if you use Italian locale on Android. Could you try it?
Upvotes: 5
Reputation: 11308
Considering that the code works fine on your computer I would suspect unprintable characters in your input string which cause parse problem on Android. Can you try the following which removes control characters from the parsed string.
expDate = df.parse(expString.replaceAll("\\p{Cntrl}", ""));
Hope this helps.
Upvotes: 1