效果图:
具体时间差代码请参考:https://blog.csdn.net/weixin_43477545/article/details/109096335
android中可能会出现当前时间不匹配的问题,只需要设定时区即可解决
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
获取当前时间与某一时间的时间差
/**
* 获取输入时间与现在时间差
* @param log_time 日志时间 格式为2020-6-1 10:00:00
* @return
*/
public static String getTime(String log_time){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
Date time_now = new Date();
long diff = 0;
String CountTime = "";
int year=0,month=0,day=0;
long hours=0,minutes=0,s=0;
try {
Date time_ago = df.parse(log_time);
diff = time_now.getTime() - time_ago.getTime();
Calendar currentTimes =dataToCalendar(time_now);//当前系统时间转Calendar类型
Calendar pastTimes =dataToCalendar(time_ago);//查询的数据时间转Calendar类型
year = currentTimes.get(Calendar.YEAR) - pastTimes.get(Calendar.YEAR);//获取年
month = currentTimes.get(Calendar.MONTH) - pastTimes.get(Calendar.MONTH);
day = currentTimes.get(Calendar.DAY_OF_MONTH) - pastTimes.get(Calendar.DAY_OF_MONTH);
if (month < 0) {
month = (month + 12) % 12;//获取月
year--;
}
if (day < 0) {
month -= 1;
currentTimes.add(Calendar.MONTH, -1);
day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);//获取日
}
long days = diff / (1000 * 60 * 60 * 24);
hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60); //获取时
minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60); //获取分钟
s=(diff/1000-days*24*60*60-hours*60*60-minutes*60);//获取秒
CountTime=""+year+"年"+month+"月"+day+"天"+hours+"小时"+minutes+"分"+s+"秒";
} catch (ParseException e) {
e.printStackTrace();
}
return year != 0 ? ""+year+"|"+0 :
month != 0 ? ""+month+"|"+1 :
day != 0 ? ""+day+"|"+2 :
hours != 0 ? ""+hours+"|"+3 :
minutes != 0 ? ""+minutes+"|"+4 : ""+s+"|"+5 ;
}
public static Calendar dataToCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.setTime(date);
return calendar;
}
此处添加在textview需要的地方,用于变换颜色和内容
String time_group = getTime("2020-10-1 10:30:27" );
int[] time_array = handle_time(time_group);
GradientDrawable gd = (GradientDrawable) holder.tv_log_tag.getBackground();
gd.setColor(view.getResources().getColor(getColor(time_array[1])));
textview.setText(parse_time(time_array));
/*--------------------------------以下用于设置日志小标签2min前--------------------------------*/
private int[] handle_time(String input_time){
StringTokenizer fenxi = new StringTokenizer(input_time,"|");
int[] result = new int[2];
int i = 0;
while (fenxi.hasMoreTokens()){
result[i] = Integer.parseInt(fenxi.nextToken());
i++;
}
return result;
}
private String parse_time(int[] input_array){
String[] time_list = {
"年","月","天","小时","分钟","秒"};
if (input_array[1] == 5)
return "刚刚";
else
return ""+input_array[0]+time_list[input_array[1]]+"前";
}
private int getColor(int time_index){
int[] color = {
R.color.darkblue,R.color.red,R.color.search_opaque,R.color.orchid,R.color.medium_spring_green,R.color.medium_purple};
if (time_index>=0 && time_index<=5)
return color[time_index];
return color[0];
}
最后,layout中的textview的背景用drawable目录下的xml表示
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置圆角-->
<corners android:radius="8dp" />
<solid android:color="@color/blue"/>
</shape>
注意
textview用的背景xml不要让其他控件使用,因为会影响其他控件背景的正常显示。
具体时间差代码请参考:https://blog.csdn.net/weixin_43477545/article/details/109096335