java - 时间戳保存的是 map 而不是实际时间戳

标签 java android date google-cloud-firestore

当我保存使用 Timestamp.now() 得到的日期时间戳时,它会保存这个奇怪的 map 而不是实际时间。我应该只获取位于数据/时间中的时间戳,而不是整个 map 。奇怪的是,我实际上向 Firestore 保存了一个名为日期的时间戳,而不是 map 。 这是图片:

这是我写的保存两次到firestore的函数(两次是一个发明的名字,它实际上意味着注释)


    private void saveTwice() {
        String title = mTitle.getText().toString();
        String description = mDescription.getText().toString();
        Timestamp date = Timestamp.now();

        Log.d(TAG, "saveTwice: " + date.toString());

        if (title.trim().isEmpty() || description.trim().isEmpty()) {
            Toast.makeText(this, "Please insert a title and description", Toast.LENGTH_SHORT).show();
            return;
        }

        CollectionReference twiceRef = FirebaseFirestore.getInstance()
                .collection("Twices");
        twiceRef.add(new Twice(title, description, date));
        Toast.makeText(this, "Twice added", Toast.LENGTH_SHORT).show();
        Intent back = new Intent(AddTwice.this, MainActivity.class);
        startActivity(back);
    }
}

还有 Twice.java。

package com.example.twicev11;

import android.text.format.DateFormat;

import com.google.firebase.Timestamp;
import com.google.firebase.firestore.FieldValue;

import java.util.Calendar;
import java.util.Locale;


public class Twice {
    private String title, description;
    private Timestamp date;

    public Twice() {
        //empty constructor needed
    }

    public Twice(String title, String description, Timestamp date) {
        this.title = title;
        this.description = description;
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public Calendar getDate() {
        Calendar cal = Calendar.getInstance(Locale.getDefault());
        cal.setTimeInMillis(date.toDate().getTime());

        return cal;
    }


}

我希望得到这个,而不是那张丑陋的 map

最佳答案

您正在存储一个Calendar 对象,它包含的信息不仅仅是日期。如果您只想存储日期,请存储 calendar.getTime()

关于java - 时间戳保存的是 map 而不是实际时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57046951/

相关文章:

java - 禁用谷歌云存储上的文件缓存(灵活的应用程序引擎java)

java - 无法在 Tomcat 中部署使用 MYSQL 的 Web 服务

android - 苹果操作系统 : cordova platform add android

android - 使用 NoActionBar 主题时显示操作栏

javascript - Javascript date toLocaleString 是否考虑 DST?

java - 丢弃字符的前导和尾随系列,但保留相同的字符

java - 如何从 JSON 添加项目到 ListView ?

android - WhatsApp - 编写克隆(iphone、android、wp7)

php - 当日期为年/月/日时按日期(年/月/日)查询并分组 H :i:s format

java - 这是二月还是三月的日期?校准集 (2010, 1, 10)