Android 获取当前日期时间并在 Firestore 中搜索
Android get current datetime and search in Firestore
以上是我的firestore数据库。现在我想执行搜索以检查我是否有在当前日期制作的任何记录。下面是我用来搜索是否有任何相关数据的代码。但这是行不通的...它没有向我提供任何记录...有人知道为什么吗?
Date TodayDateTime = Calendar.getInstance(TimeZone.getTimeZone(timezoneS)).getTime();
SimpleDateFormat dateTime = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = dateTime.format(TodayDateTime);
CollectionReference doc1 = firebaseFirestore.collection("TransactionRecord");
Query query = doc1.whereEqualTo("userId", user.getStudentID())
.whereEqualTo("timeStamp", "18/5/2022");
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
if (error != null) {
Toast.makeText(ConfirmPaymentActivity.this, "Error while loading ... ! " + error.toString(), Toast.LENGTH_SHORT).show();
Log.d("Problem", error.toString());
return;
}
for (DocumentChange dc : value.getDocumentChanges()) {
if (dc.getType() == DocumentChange.Type.ADDED) {
list = dc.getDocument().toObject(TransactionRecordClass.class);
transactionlist.add(list);
TotalSpented += list.totalPrice;
}
}
if (transactionlist.isEmpty()) {
TotalBudgetAmount.setText("RM " + df.format(user.getBudgetControl()));
} else if (TotalSpented >user.getBudgetControl()) {
TotalBudgetAmount.setText("RM " + df.format(user.getBudgetControl()-TotalSpented));
TotalBudgetAmount.setTextColor(Color.parseColor("#FF0000"));
} else if (TotalSpented < user.getBudgetControl()){
TotalBudgetAmount.setText("RM " + df.format(user.getBudgetControl()-TotalSpented));
TotalBudgetAmount.setTextColor(Color.parseColor("#00FF00"));
}
}
问题出在您发送到数据库的查询中:
Query query = doc1.whereEqualTo("userId", user.getStudentID())
.whereEqualTo("timeStamp", "18/5/2022");
此代码将您存储在数据库中的日期/Timestamp
与字符串值 "18/5/2022"
进行比较。虽然这个字符串值对您来说可能读起来像数据,但对数据库来说却不是,所以它 return 没有文档,因为没有文档有一个名为 timeStamp
的字段,字符串值为 "18/5/2022"
.
如果要在 date/timestamp 上进行过滤,通常需要在查询中包含两个条件:要返回的范围的开始时间戳和结束时间戳。
因此,如果您想 return 一整天,您需要从一天开始的时间戳开始,并以一天结束的时间戳结束(或者,在第二天开始的时间戳之前结束。
所以在代码中可能是这样的:
// Create the start date from your literal values (month #4 is May)
Date startAt = new GregorianCalendar(2022, 4, 18).getTime();
// Create the end date by adding one day worth of milliseconds
Date endBefore = new Date(Date.getTime() + 24*60*60*1000);
Query query = doc1.whereEqualTo("userId", user.getStudentID())
.whereEqualToOrGreaterThan("timeStamp", startAt);
.whereLessThan("timeStamp", endBefore);
以上是我的firestore数据库。现在我想执行搜索以检查我是否有在当前日期制作的任何记录。下面是我用来搜索是否有任何相关数据的代码。但这是行不通的...它没有向我提供任何记录...有人知道为什么吗?
Date TodayDateTime = Calendar.getInstance(TimeZone.getTimeZone(timezoneS)).getTime();
SimpleDateFormat dateTime = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = dateTime.format(TodayDateTime);
CollectionReference doc1 = firebaseFirestore.collection("TransactionRecord");
Query query = doc1.whereEqualTo("userId", user.getStudentID())
.whereEqualTo("timeStamp", "18/5/2022");
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
if (error != null) {
Toast.makeText(ConfirmPaymentActivity.this, "Error while loading ... ! " + error.toString(), Toast.LENGTH_SHORT).show();
Log.d("Problem", error.toString());
return;
}
for (DocumentChange dc : value.getDocumentChanges()) {
if (dc.getType() == DocumentChange.Type.ADDED) {
list = dc.getDocument().toObject(TransactionRecordClass.class);
transactionlist.add(list);
TotalSpented += list.totalPrice;
}
}
if (transactionlist.isEmpty()) {
TotalBudgetAmount.setText("RM " + df.format(user.getBudgetControl()));
} else if (TotalSpented >user.getBudgetControl()) {
TotalBudgetAmount.setText("RM " + df.format(user.getBudgetControl()-TotalSpented));
TotalBudgetAmount.setTextColor(Color.parseColor("#FF0000"));
} else if (TotalSpented < user.getBudgetControl()){
TotalBudgetAmount.setText("RM " + df.format(user.getBudgetControl()-TotalSpented));
TotalBudgetAmount.setTextColor(Color.parseColor("#00FF00"));
}
}
问题出在您发送到数据库的查询中:
Query query = doc1.whereEqualTo("userId", user.getStudentID())
.whereEqualTo("timeStamp", "18/5/2022");
此代码将您存储在数据库中的日期/Timestamp
与字符串值 "18/5/2022"
进行比较。虽然这个字符串值对您来说可能读起来像数据,但对数据库来说却不是,所以它 return 没有文档,因为没有文档有一个名为 timeStamp
的字段,字符串值为 "18/5/2022"
.
如果要在 date/timestamp 上进行过滤,通常需要在查询中包含两个条件:要返回的范围的开始时间戳和结束时间戳。
因此,如果您想 return 一整天,您需要从一天开始的时间戳开始,并以一天结束的时间戳结束(或者,在第二天开始的时间戳之前结束。
所以在代码中可能是这样的:
// Create the start date from your literal values (month #4 is May)
Date startAt = new GregorianCalendar(2022, 4, 18).getTime();
// Create the end date by adding one day worth of milliseconds
Date endBefore = new Date(Date.getTime() + 24*60*60*1000);
Query query = doc1.whereEqualTo("userId", user.getStudentID())
.whereEqualToOrGreaterThan("timeStamp", startAt);
.whereLessThan("timeStamp", endBefore);