Description: The following method is supposed to get the date of the last inventory count from Firestore, and execute proceedWithInventoryMode() if it's been at least 24 hours.
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());
}
Problem: I'm getting an IllegalArgumentException. The Log statement in the onSuccess() statement prints: "lastInventoryEndDate:: 13/07/2022 09:57:11". But the error log is as follows:
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)
Any idea how I can solve this? Thank you.
You didn’t include the code which does the actual parsing but I’m assuming its got something to do with your date not being in ISO8601 format. You’ll have to supply a custom formatter that will be used to parse the string into an appropriate date and time object.
In your case, you can create a custom formatter
val formatter = DateTimeFormat.forPattern(“dd/MM/yyyy HH:mm:ss”)And use it to parse your date
val dateTime = LocalDateTime.parse(dateTimeString, formatter);