Android Textview implementa la etiqueta pequeña de diferencia horaria

Imagen del efecto:
Inserte la descripción de la imagen aquí
consulte el código de diferencia horaria específico: https://blog.csdn.net/weixin_43477545/article/details/109096335

Puede haber un problema de falta de coincidencia de la hora actual en Android, simplemente configure la zona horaria para resolverlo

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

Obtenga la diferencia horaria entre la hora actual y una hora determinada

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

Agregue aquí donde sea necesario en la vista de texto para cambiar el color y el contenido

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

Finalmente, el fondo de la vista de texto en el diseño está representado por el xml en el directorio dibujable

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

Tenga en cuenta que
el xml de fondo que usa textview no debe ser usado por otros controles, ya que afectará la visualización normal del fondo de otros controles.
Consulte el código de diferencia horaria específico: https://blog.csdn.net/weixin_43477545/article/details/109096335

Supongo que te gusta

Origin blog.csdn.net/weixin_43477545/article/details/109096494
Recomendado
Clasificación