Android 上的 DateFormat:无法解析的日期

发布于 2025-01-01 04:28:48 字数 1973 浏览 1 评论 0原文

我正在开发一个从字符串中提取日期并将其转换为日期的函数。 日期具有以下格式:

日-mmm-年

其中 mmm 是月份的 3 位数名称,全部小写。

这段代码如下:

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();
}                   

如果我在计算机 jvm 上尝试该代码,它工作正常,并且可以毫无问题地转换日期,但如果我尝试在 android 上运行它,则会出现以下错误:

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)

知道吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不打扰别人 2025-01-08 04:28:48

不是 100% 确定(我稍后会检查),但我认为问题是对于 MMM 你的月份也必须在正确的区域设置中。因此,如果您在 Android 上使用意大利语区域设置,则 PC 上的 Sep(可能区域设置为 EN)需要用意大利语表示。
你能尝试一下吗?

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?

飘逸的'云 2025-01-08 04:28:48

考虑到代码在你的计算机上运行良好,我怀疑你的输入字符串中存在不可打印的字符,这会导致 Android 上的解析问题。您可以尝试以下操作,从解析的字符串中删除控制字符。

expDate = df.parse(expString.replaceAll("\\p{Cntrl}", ""));

希望这有帮助。

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.

您的好友蓝忘机已上羡 2025-01-08 04:28:48

尝试在创建 SimpleDateFormat 时指定英语区域设置:

DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);

Try specifying an English locale when you create the SimpleDateFormat:

DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文