android的Joda时间导致IllegalArgumentException



说明:下面的方法应该从Firestore获取最后库存计数的日期,并执行procedwithinventorymode ()如果已经超过24小时

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()中的日志语句语句打印:"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)

知道怎么解决这个问题吗?谢谢你。

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

在您的例子中,您可以创建一个自定义格式化器

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

用它来解析你的日期

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

最新更新