Android 上的 Joda Time 导致 IllegalArgumentException

Joda Time on android causing IllegalArgumentException

提问人:Alula48 提问时间:7/13/2022 更新时间:7/15/2022 访问量:167

问:

说明:以下方法应该从 Firestore 获取上次库存盘点的日期,如果至少 24 小时,则执行 proceedWithInventoryMode()。

private void checkIf24HourReached() {
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime _24hrsBefore = now.minusDays(1);

    db.collection("Project").document(Prefs.getString("ManageID",
            GlobalObject._FIRESTORE_ID))
            .collection("inventoryMode")
            .document(GlobalObject._INVENTORY_MODE_DATA_ID)
            .get()
            .addOnSuccessListener(documentSnapshot -> {
                String lastInventoryEndDate = documentSnapshot.getString("date");
                Log.d("lastInventoryEndDate: ", lastInventoryEndDate);
                if (lastInventoryEndDate == null) {
                    proceedWithInventoryMode();
                } else {
                    if (lastInventoryEndDate.isEmpty()) {
                        proceedWithInventoryMode();
                    }
                    LocalDateTime lastInventoryDate = new LocalDateTime(lastInventoryEndDate);
                    if (_24hrsBefore.isAfter(lastInventoryDate)) {
                        proceedWithInventoryMode();
                    } else {
                        Toast.makeText(getContext(),
                                "Last Inventory ended less than a day ago.",
                                Toast.LENGTH_LONG).show();
                        Toast.makeText(getContext(),
                                "Last date: " + lastInventoryEndDate,
                                Toast.LENGTH_LONG).show();
                    }
                }
            })
            .addOnFailureListener(e -> Toast.makeText(getContext(),
                    "Date couldn't be read.",
                    Toast.LENGTH_LONG).show());

}

问题:我收到 IllegalArgumentExceptiononSuccess() 语句中的 Log 语句打印:“lastInventoryEndDate:: 13/07/2022 09:57:11”。但错误日志如下:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nati.mvp.flowiusmanage, PID: 18644
java.lang.IllegalArgumentException: Invalid format: "13/07/2022 09:57:11" is malformed at "/07/2022 09:57:11"
    at org.joda.time.format.DateTimeParserBucket.doParseMillis(DateTimeParserBucket.java:187)
    at org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:826)
    at org.joda.time.convert.StringConverter.getPartialValues(StringConverter.java:87)
    at org.joda.time.LocalDateTime.<init>(LocalDateTime.java:414)
    at org.joda.time.LocalDateTime.<init>(LocalDateTime.java:358)
    at com.nati.mvp.flowiusmanage.fragments.inventoryFragments.InventoryModeFragment.lambda$checkIf24HourReached$2$InventoryModeFragment(InventoryModeFragment.java:157)
    at com.nati.mvp.flowiusmanage.fragments.inventoryFragments.-$$Lambda$InventoryModeFragment$_kMo6tPuE8p1PpNkIRJgZvJYet8.onSuccess(Unknown Source:6)
    at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.1:1)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:264)
    at android.app.ActivityThread.main(ActivityThread.java:7581)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)

知道我该如何解决这个问题吗?谢谢。

Java 谷歌云 Firestore Android-JodaTime

评论


答:

1赞 Rafsanjani 7/13/2022 #1

您没有包含进行实际解析的代码,但我假设它与您的日期不是ISO8601格式有关。您必须提供一个自定义格式化程序,该格式化程序将用于将字符串解析为适当的日期和时间对象。

在您的例子中,您可以创建自定义格式化程序

val formatter = DateTimeFormat.forPattern(“dd/MM/yyyy HH:mm:ss”)

并用它来解析你的日期

val dateTime = LocalDateTime.parse(dateTimeString, formatter);

评论

0赞 Anonymous 7/15/2022
我认为解析隐含在其中 a with value 的行中。好的解决方案是以预期的 ISO 8601 格式存储最后的日期和时间。LocalDateTime lastInventoryDate = new LocalDateTime(lastInventoryEndDate);lastInventoryEndDateString13/07/2022 09:57:11LocalDateTime
1赞 Rafsanjani 7/15/2022
@OleV.V.隐式解析仅在日期格式正确ISO8601时才有效。如果没有,那么如果没有自定义格式化程序,它将失败。
1赞 Anonymous 7/15/2022
完全。您的答案是切中要害,除了您希望在格式模式字符串中为月份大写。MM