java - 如何将日期和时间字符串转换为时间前类型?
问题描述
我也是firebase和android的新手。在我的应用程序中,学生可以分享他关于教学大纲的问题和答案。为此,我在 firebase 数据库中设置时间和日期字符串,并通过 POJO 类以字符串格式检索日期和时间,例如日期:2019 年 4 月 6 日和时间:01:38:24。到目前为止,我通过 model.getTime 和 model.getDate 在 TextView 中使用了 setText。所以这就像一个更新(TextView)2019 年 4 月 6 日 01:38:24 现在我想像几分钟前、一小时前、一天前、一个月前、一年前一样按 Timeago 更改它
抱歉英语不好。并提前感谢
protected void onBindViewHolder(@NonNull UserViewHolder holder, int position, @NonNull final question model)
{
final String PostKey=getRef(position).getKey();//get key by this line
holder.userfullname.setText(model.getFirstname()+" "+model.getLastname());
holder.time.setText(" "+model.getTime());//***TextView 06-April-2019***
holder.date.setText(" "+model.getDate());//*TextView 01:38:24*
holder.description.setText(model.getDescription());
}
解决方案
使用 System.currentTimeMillis(); 你得到他们自 1970 年 1 月 1 日以来经过的毫秒数。
因此,您希望我们可以在您的适配器中使用另外 2 个自定义功能来实现。
第一个获取您的日期和时间并将其转换为毫秒。
private long millisFromDateAndTime(String date, String time){
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMMM-yyyy");
SimpleDateFormat timeFormat = new SimpleDateFormat("kk:mm:ss");
try
{
Date mDate = dateFormat.parse(date);
Date mTime = timeFormat.parse(time);
long dateInMilliseconds = mDate.getTime();
long timeInMilliseconds = mTime.getTime();
return timeInMilliseconds + dateInMilliseconds;
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
第二个函数从发布帖子开始获取这个毫秒数,并返回一个包含多少时间之前的字符串。
private String timeAgo(long startTime){
long currentTime = System.currentTimeMillis();
long timeDifference = (currentTime - startTime) / 1000;
Log.d("!!!!DIFF!!!!!!", String.valueOf(timeDifference));
long timeUnit;
if(timeDifference < 60){
timeUnit = DateUtils.SECOND_IN_MILLIS;
} else if(timeDifference < 3600){
timeUnit = DateUtils.MINUTE_IN_MILLIS;
} else if (timeDifference < 86400){
timeUnit = DateUtils.HOUR_IN_MILLIS;
}else if (timeDifference < 31536000){
timeUnit = DateUtils.DAY_IN_MILLIS;
} else {
timeUnit = DateUtils.YEAR_IN_MILLIS;
}
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(startTime, currentTime,
timeUnit, DateUtils.FORMAT_ABBREV_RELATIVE);
return String.valueOf(timeAgo);
}
您可以将这两个功能放在适配器的底部。
最后,您将在 onBindViewholder 中使用此函数。
protected void onBindViewHolder(@NonNull UserViewHolder holder, int position, @NonNull final question model)
{
final String PostKey=getRef(position).getKey();//get key by this line
holder.userfullname.setText(model.getFirstname()+" "+model.getLastname());
//Convert your Date and Time to milliseconds
long startTime = millisFromDateAndTime(model.getDate(), model.getTime);
//Set the timeAgo text in the Textview of your choice
holder.time.setText(timeAgo(startTime));
holder.description.setText(model.getDescription());
}
祝你好运,告诉我它是否有效。
推荐阅读
- elasticsearch - ElasticSearch / Solr dis_max 查询跳过匹配子句
- java - Java JDBC - PreparedStatement.executeUpdate() 总是返回 0
- graphviz - 错误:pydotplus.graphviz.InvocationException:未找到 GraphViz 的可执行文件
- php - 为什么 $ 字符是 PHP 中 parse_ini_file 函数的问题
- oauth-2.0 - 如何在 SpringBoot 应用程序中获取授权码
- css - 如何使用 CSS 移动 Squarespace 产品页面上的“添加到购物车”按钮
- c - 如何防止终止直到 SIGTERM 和 SIGINT?
- javascript - 我被困在我的反应项目中如何从 API 访问名称元素,我已经给出了 API 的快照,我的代码如下,任何人都可以帮助我
- c++ - 为什么这不工作类和对象
- cypress - 如何遍历多个元素并将内容添加到字符串