如何在Firebase和Android Studio中以有序的方式保存日期和时间。

huangapple go评论79阅读模式
英文:

How to save date and time in an orderly format Firebase & Android Studio

问题

我正在创建一个社交应用希望在有人上传帖子时保存帖子的日期和时间我希望将其保存为类似于`dd-MM-yyyy HH:mm:ss`的格式目前它的显示方式如下图所示

![当前日期和时间戳][1]

以下是我在一个方法中的代码

```java
public static String getTimeDate(long timestamp){
    try{
        Date netDate = (new Date(timestamp));
        SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
        return sfd.format(netDate);
    } catch(Exception e) {
        return "日期";
    }
}

我希望它能以我想要的格式保存上传帖子的日期和时间。但是我的问题是如何在 Firebase 数据库中正确上传它。以下是我上传当前字段的方式:

HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("postid", postid);
hashMap.put("postimage", myUrl);
hashMap.put("description", description.getText().toString());
hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());

我试图上传时间戳,如下所示:

hashMap.put("date_time", getTimeDate(timestamp));

但是当我悬停在getTimeDate()旁边的括号上时,它显示期望 1 个参数,但找到 0 个参数,所以我猜我必须使用字符串而不是方法名。有人可以帮我解决该问题吗?


<details>
<summary>英文:</summary>

I am creating a social app and I want to save the date and time of a post when someone uploads it. I want it to be saved for example as `dd-MM-yyyy HH:mm:ss`. Currently this is how it looks 

![Current date and time stamp][1]


  [1]: https://i.stack.imgur.com/a1nVs.png

So this is the code that I have in a method.

    public static String getTimeDate(long timestamp){
        try{
            Date netDate = (new Date(timestamp));
            SimpleDateFormat sfd = new SimpleDateFormat(&quot;dd-MM-yyyy HH:mm:ss&quot;, Locale.getDefault());
            return sfd.format(netDate);
        } catch(Exception e) {
            return &quot;date&quot;;
        }
    }

I expect it to save the date and time that the post was uploaded in the format I want. But my problem is uploading it correctly in the Firebase DB. This is how I am uploading my current fields.

    HashMap&lt;String, Object&gt; hashMap = new HashMap&lt;&gt;();
    hashMap.put(&quot;postid&quot;, postid);
    hashMap.put(&quot;postimage&quot;, myUrl);
    hashMap.put(&quot;description&quot;, description.getText().toString());
    hashMap.put(&quot;publisher&quot;, FirebaseAuth.getInstance().getCurrentUser().getUid());

I am trying to upload the timestamp like:

    hashMap.put(&quot;date_time&quot;, getTimeDate() );

But when I hover over the parenthesis next to `getTimeDate()` it says `Expected 1 arguments but found 0`
so I&#39;m guessing I have to use a string instead of the method name. Can someone help me on what to do ?

</details>


# 答案1
**得分**: 0

以下是翻译好的内容:

获取数据上传时的当前时间,您可以使用`new Date()`,并且在不需要参数的情况下更改您的方法。

```java
public static String getTimeDate() { // 不需要参数
    try{
        Date netDate = new Date(); // 从这里获取当前时间
        SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
        return sfd.format(netDate);
    } catch(Exception e) {
        return "日期";
    }
}
英文:

To get current time when someone upload the data, you can from new Date() and change your method without having a parameter argument

public static String getTimeDate() { // without parameter argument
    try{
        Date netDate = new Date(); // current time from here
        SimpleDateFormat sfd = new SimpleDateFormat(&quot;dd-MM-yyyy HH:mm:ss&quot;, Locale.getDefault());
        return sfd.format(netDate);
    } catch(Exception e) {
        return &quot;date&quot;;
    }
}

huangapple
  • 本文由 发表于 2020年10月7日 09:11:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64235822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定