Android Textview implements time difference small label

Effect picture:
Insert picture description here
Please refer to the specific time difference code: https://blog.csdn.net/weixin_43477545/article/details/109096335

There may be a problem of current time mismatch in android, just set the time zone to solve it

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));

Get the time difference between the current time and a certain time

/**
     * 获取输入时间与现在时间差
     * @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;
    }

Add here where needed in the textview to change the color and content

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];
    }

Finally, the background of the textview in the layout is represented by the xml in the drawable directory

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 设置圆角-->
    <corners android:radius="8dp" />
    <solid android:color="@color/blue"/>
</shape>

Note that
the background xml used by textview should not be used by other controls, because it will affect the normal display of the background of other controls.
Please refer to the specific time difference code: https://blog.csdn.net/weixin_43477545/article/details/109096335

Guess you like

Origin blog.csdn.net/weixin_43477545/article/details/109096494