设定日期和时间

时间:2018-08-23 15:37:57

标签: android datetime

我想在每次输入并显示它时设置一个DateTime。我无法显示正确的DateTime。它向我显示1970年1月1日+ HH MM和SS。 这个想法是每次我输入一个条目时,它将像历史记录一样打印该条目创建的DateTime。

这是我的代码:

   @Override
public void onBindViewHolder(ViewHolder holder, int position) { 
    FirebaseEntry xx= mDataset.get(position);
    Date date = new Date(Long.parseLong(xx.getDate()));
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    String dateInCorrectFormat = (dateFormat.format(date));
    holder.mDate.setText(dateInCorrectFormat);
}

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

尝试一下

Long current = System.currentTimeMillis();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String date = df.format(current);

现在将此日期添加到您的数据库中。并使用它。显示详细信息时,请不要获取当前日期。而是显示存储在数据库中的日期。

答案 1 :(得分:0)

根本原因:我认为xx.getDate()方法返回以秒为单位的时间戳,这就是为什么您的应用始终显示1970年代的原因。

解决方案::在格式化日期之前将秒转换为毫秒。

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    FirebaseEntry xx = mDataset.get(position);
    long timeInMilliseconds = Long.parseLong(xx.getDate()) * 1000;
    Date date = new Date(timeInMilliseconds);
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    String dateInCorrectFormat = (dateFormat.format(date));
    holder.mDate.setText(dateInCorrectFormat);
}

答案 2 :(得分:0)

它显示的是第一个日期:1970年1月1日+ HH MM和SS,因为时间有问题,Java期望以毫秒为单位的long值,并且您可能以秒为单位。

发生在我身上,请尝试执行此操作或确保您有毫秒的时间:

Long miliseconds = Long.parseLong(md.getDate());
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String dateInCorrectFormat = dateFormat.format(miliseconds*1000);
相关问题