英文:
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("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
return sfd.format(netDate);
} catch(Exception e) {
return "date";
}
}
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<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());
I am trying to upload the timestamp like:
hashMap.put("date_time", getTimeDate() );
But when I hover over the parenthesis next to `getTimeDate()` it says `Expected 1 arguments but found 0`
so I'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("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
return sfd.format(netDate);
} catch(Exception e) {
return "date";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论