从firebase数据库检索到错误的时间戳

时间:2017-06-26 14:45:48

标签: android firebase firebase-realtime-database

我成功地以某种方式从firebase数据库中检索时间戳,但现在它显示错误的时间戳。它显示的内容如下:01-01-1970 05:30。令人惊讶的是,该值显示在所有帖子中。时间戳甚至没有改变。

博客查看持有人:

    public static class BlogViewHolder extends RecyclerView.ViewHolder {
            View mview;


            public BlogViewHolder(View itemView) {
                super(itemView);
                mview = itemView;
            }

            public void setTitle(String title) {
                TextView post_title = (TextView) mview.findViewById(R.id.blog_title);
                post_title.setText(title);

            }

            public void setDesp(String desp) {
                TextView post_desp = (TextView) mview.findViewById(R.id.blog_desp);
                post_desp.setText(desp);
            }

            public void setTimestampCreated(long timestamp) {
               TextView show_ts = (TextView) mview.findViewById(blog_timestamp);
               SimpleDateFormat sfd=new SimpleDateFormat("dd-MM-yyyy  HH:mm");


                Date date=new Date(timestamp);
                show_ts.setText(sfd.format(date));
            }


 FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
                    Blog.class,
                    R.layout.blog_row,
                    BlogViewHolder.class, mDatabase

            ) {
                @Override
                protected void populateViewHolder(final BlogViewHolder viewHolder,final Blog model, int position) {
                    viewHolder.setTitle(model.getTitle());
                    viewHolder.setDesp(model.getDesp());
                    viewHolder.setTimestampCreated(model.getTimestampCreated());
                }
            };

Blog.java

public class Blog {

    String title;
    String desp;
long timestamp;

 public Blog() {
    }

    public Blog(String title, String desp, long timestamp) {
        this.title = title;
        this.desp = desp;
        this.timestamp=timestamp;
}

 public String getDesp() {
        return desp;
    }

    public void setDesp(String desp) {
        this.desp = desp;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public  long getTimestampCreated() {


            return timestamp;


    }

    public void setTimestampCreated(long timestamp) {
        this.timestamp = timestamp;
    }

数据库参考:

public void startPosting() {


        final DatabaseReference newPost = databaseReference.push();
 String title_val = title.getText().toString().trim();
                 String desp_val = desp.getText().toString().trim();

 newPost.child("title").setValue(title_val);
                    newPost.child("desp").setValue(desp_val);
newPost.child("timestamp").setValue(ServerValue.TIMESTAMP);
}

2 个答案:

答案 0 :(得分:0)

要从Firebase数据库获取TIMESTAMP,我会重新尝试以下方法:

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

希望它有所帮助。

答案 1 :(得分:0)

问题是timestamp的getter / setter名称不遵循命名约定而无法识别。如果仔细查看logcat输出,您应该看到此警告消息:

W/ClassMapper: No setter/field for timestamp found on class com.xxx.Blog

最简单的修复可能是重命名getter / setter:

public long getTimestamp() {
    return timestamp;
}

public void setTimestamp(long timestamp) {
    this.timestamp = timestamp;
}

还有其他解决方案,一个是@PropertyName注释,但我认为修复getter / setter名称可能是最简单的。