【问题标题】:How do you convert Timestamp to normal time?你如何将时间戳转换为正常时间?
【发布时间】:2021-07-06 18:20:32
【问题描述】:

我遇到了这个问题,当我发布我的时间戳时,会显示过多的信息,如图

但是我想知道如何将其转换为没有 GMT 或年份的时间和日期,例如 Sat Apr 03 14:00:00。

我的 add.java


    private void uploadData(String imageURL) {


        CollectionReference reference = FirebaseFirestore.getInstance().collection("Users")
                .document(user.getUid()).collection("Post Images");
        String id = reference.document().getId();
        String description = descET.getText().toString();

        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("description", description);
        map.put("imageUrl", imageURL);
        map.put("timestamp", FieldValue.serverTimestamp());


        map.put("name", user.getDisplayName());
        map.put("profileImage",String.valueOf(user.getPhotoUrl()));
        map.put("likeCount", 0);
        map.put("Comments", "");
        map.put("uid", user.getUid());

        reference.document(id).set(map)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if(task.isSuccessful()){
                            System.out.println();
                            Toast.makeText(getContext(), "Uploaded", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(getContext(), "Error: "+task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                        dialog.dismiss();
                    }
                });


    }

我的个人资料.java

    private void uploadImage(Uri uri) {
        StorageReference reference = FirebaseStorage.getInstance().getReference().child("Profile Images");

        reference.putFile(uri)
                .addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                        if (task.isSuccessful()) {
                            reference.getDownloadUrl()
                                    .addOnSuccessListener(new OnSuccessListener<Uri>() {
                                        @Override
                                        public void onSuccess(Uri uri) {
                                            String imageURL = uri.toString();
                                            UserProfileChangeRequest.Builder request = new UserProfileChangeRequest.Builder();
                                            request.setPhotoUri(uri);

                                            user.updateProfile(request.build());
                                            Map<String, Object> map = new HashMap<>();
                                            map.put("profileImage", imageURL);
                                            map.put("date", FieldValue.serverTimestamp());
                                            FirebaseFirestore.getInstance().collection("Users")
                                                    .document(user.getUid())
                                                    .update(map).addOnCompleteListener(new OnCompleteListener<Void>() {
                                                @Override
                                                public void onComplete(@NonNull Task<Void> task) {
                                                    if (task.isSuccessful())
                                                        Toast.makeText(getContext(), "Updated Successfully", Toast.LENGTH_SHORT).show();
                                                    else
                                                        Toast.makeText(getContext(), "Error: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                                                }
                                            });
                                        }
                                    });
                        } else {
                            Toast.makeText(getContext(), "Error: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

我们将不胜感激。

【问题讨论】:

标签: java android firebase firebase-realtime-database timestamp


【解决方案1】:

java.time

考虑使用现代 Java 日期和时间 API java.time。对于从一种字符串格式到另一种格式的转换,请使用两种格式化程序:

private static final DateTimeFormatter inputParser
        = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz uuuu", Locale.ENGLISH);
private static final DateTimeFormatter outputFormatter
        = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm", Locale.ENGLISH);

和他们一起做:

String givenStringDate = "Sat Apr 03 14:19:53 GMT+01:00 2021";
ZonedDateTime zdt = ZonedDateTime.parse(givenStringDate, inputParser);
String outputString = zdt.truncatedTo(ChronoUnit.HOURS).format(outputFormatter);
System.out.println(outputString);

输出是:

4 月 3 日星期六 14:00

现在我们开始了,考虑为相关受众使用 Java 的内置本地化格式,例如:

private static final DateTimeFormatter outputFormatter
        = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
                .withLocale(Locale.FRENCH);

03/04/21 14:00

您可以通过指定MEDIUMLONGFULL 来延长格式。

问题:java.time 不需要 Android API 26 级吗?

java.time 在较旧和较新的 Android 设备上都能很好地工作。它只需要至少 Java 6

  • 在 Java 8 及更高版本以及更新的 Android 设备(从 API 级别 26 起)中,现代 API 是内置的。
  • 在非 Android 的 Java 6 和 7 中,获取 ThreeTen Backport,这是现代类的后向端口(对于 JSR 310,ThreeTen;请参阅底部的链接)。
  • 在较旧的 Android 上,请使用脱糖或 Android 版本的 ThreeTen Backport。它被称为 ThreeTenABP。在后一种情况下,请确保从 org.threeten.bp 导入日期和时间类以及子包。

链接

【讨论】:

    【解决方案2】:

    你可以这样做:

    public class Main {
         public static void main(String[] args) {
              String normalTime = convertTimestampToNormalTime("Sat Apr 03 14:19:53 
              GMT+01:00 2021");
              System.out.println(normalTime);
         }
    
         public static String convertTimestampToNormalTime(String timestamp) {
              String[] arrOfTime = timestamp.split("GMT");
              return arrOfTime[0];
         }
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-15
      • 2013-09-01
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多