gpt4 book ai didi

java - 如何解决时间戳java + firestore问题?

转载 作者:行者123 更新时间:2023-12-02 01:24:37 24 4
gpt4 key购买 nike

我正在创建一个基于 firestore 和 java android 的聊天应用程序,根据 https://firebase.google.com/docs/reference/android/com/google/firebase/Timestamp 按时间戳对聊天进行排序

但是排序每次都取决于设备时间,即使设备上的时间是错误的,这让我很失望,请帮助!!

<小时/>

我的上传时间戳代码:

Timestamp dd= Timestamp.now();
long tt=dd.getSeconds();
hashMap.put("ServerTimes",tt);

我的下载时间戳代码:

@Override
public void onBindViewHolder(@NonNull final MessageAdapter.ViewHolder holder, int position) {

Chat chat = mChat.get(position);
long date = (long) chat.getServerTimes();

Date date3 = new Date ();
date3.setTime(date*1000);


SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy EEE");
SimpleDateFormat format2 = new SimpleDateFormat("h:mm:s a");

String dateDisplay1 = format1.format(date3);
String dateDisplay2 = format2.format(date3);
String DateTime= dateDisplay1+" -- "+dateDisplay2;

我的排序代码:

  private void readMessages(final String myEmail, final String OtherUserEmail, final String imageUrl) {
final List<Chat> mChat = new ArrayList<>();
referenceContent.orderBy("ServerTimes", Query.Direction.ASCENDING) .addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
................

最佳答案

如果您不想依赖客户端时间戳,可以告诉 Firestore write a server-side timestamp 。从该文档链接:

// Update the timestamp field with the value from the server
Map<String,Object> updates = new HashMap<>();
updates.put("timestamp", FieldValue.serverTimestamp());

docRef.update(updates).addOnCompleteListener(new OnCompleteListener<Void>() {
...

关于java - 如何解决时间戳java + firestore问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57128844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com