Firebase时间戳无法检索SimpleDateFormat中的时间戳

时间:2019-12-15 21:34:31

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

从事此timestamp的工作已有很长一段时间,并使用StackOverflow上的大量帖子来帮助我解决这个问题。最终timestamp与所有其他子项一起保存在Firebase中,以保存图片,但我无法在TextView的应用程序中找到它。

因此,在我的PostActivity.java类中,我曾使用Maptimestamp保存到Firebase,然后在我的PostAdapter.java类中,我有了ViewHolders它出现在我的RecyclerView中的HomeFragment中,但它是作为Long41526276373出现的,而不是应该出现在SimpleDateFormat中的。

有人可以帮我吗?

下面有我的PostActivity.java类和PostAdapter.java类。

PostActivity.java

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");

                        String postid = reference.push().getKey();

                        HashMap<String, Object> hashMap = new HashMap<>();
                        hashMap.put("postid", postid);
                        hashMap.put("postimage", myUrl);
                        hashMap.put("description", txt_description.getText().toString());
                        hashMap.put("text_event", txt_event.getText().toString());
                        hashMap.put("text_location", txt_location.getText().toString());
                        hashMap.put("text_date_time", txt_date_time.getText().toString());
                        hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());
                        hashMap.put("timestamp", ServerValue.TIMESTAMP);

                        reference.child(postid).setValue(hashMap);

                        //Do I have to put here reference.updateChildren(hashmap)?

                        progressDialog.dismiss();

                        startActivity(new Intent(PostActivity.this, MainActivity.class));
                        finish();
                    } else {
                        Toast.makeText(PostActivity.this, "Unsuccessful. Try again", Toast.LENGTH_SHORT).show();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(PostActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            Toast.makeText(this, "No Image Selected", Toast.LENGTH_SHORT).show();
        }
    }

    public static String getTimeDate(long timestamp) {
        try {
            DateFormat dateFormat = DateFormat.getDateTimeInstance();
            Date netDate = (new Date(timestamp));
            return dateFormat.format(netDate);
        } catch (Exception e){
            return "timestamp";
        }
    }

PostAdapter.java

 @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.post_item, parent, false);
        return new PostAdapter.ViewHolder(view);

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

    firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    final Post post = mPost.get(position);

    Glide.with(mContext).load(post.getPostimage())
            .apply(new RequestOptions().placeholder(R.drawable.placeholderimg))
            .into(holder.post_image);

    if ("".equals(post.getTimestamp())) {
        holder.timestamp.setVisibility(View.GONE);
    } else {
        if (post.getPostid() != null) {
            holder.timestamp.setVisibility(View.VISIBLE);
            holder.timestamp.setText(post.getTimestamp().toString());
        }
    }

PostAdapter.java已更新

if ("".equals(post.getTimestamp())) {
            holder.timestamp.setVisibility(View.GONE);
        } else {
            if (post.getPostid() != null) {
                holder.timestamp.setVisibility(View.VISIBLE);
               ***holder.timestamp.setText(post.getTimeDate());***
            }
        }

***getTimeDate(post.getPostid(), holder.timestamp);***

public String getTimeDate(long timestamp) {
        try {
            DateFormat dateFormat = DateFormat.getDateTimeInstance();
            Date netDate = (new Date(this.timestamp));
            return dateFormat.format(netDate);
        } catch (Exception e) {
            throw e;
        }
    }

1 个答案:

答案 0 :(得分:2)

写下以下答案后,我突然发现您似乎没有从视图持有者的任何地方调用getTimeDate帮助方法,这可以解释为什么时间戳显示为数字。


在Firebase实时数据库中保存ServerValue.TIMESTAMP时,它会存储一个值,该值计算自该时期以来已过去的毫秒数。因此,这确实是一个数值(而且相当大)。

要将其显示为日期,请将其传递到new Date(...)

holder.timestamp.setText(new Date(post.getTimestamp()));

在许多情况下,我已经看到开发人员在其POJO Java类中添加了一种便捷方法,例如您的案例中的Post类。像这样:

class Post .... {
    ...

    public String getTimeDate(long timestamp) {
        try {
            DateFormat dateFormat = DateFormat.getDateTimeInstance();
            Date netDate = (new Date(this.timestamp));
            return dateFormat.format(netDate);
        } catch (Exception e){
            return new RuntimeException(e);
        }
    }

}

然后在您的视图持有人中将其用作:

holder.timestamp.setText(post.getTimeDate());

另请参阅: