Home > php教程 > PHP开发 > body text

Detailed explanation of WeChat and QQ time format template examples

高洛峰
Release: 2016-12-09 13:28:21
Original
1396 people have browsed it

Go directly to the code, there are comments in the code, please take a look!

/**
   * 将一个时间戳转换成提示性时间字符串,如
   * 2分钟内 无显示
   * 2分钟-24小时 HH:mm
   * 昨天 昨天 HH:mm
   * 前天 前天 HH:mm
   * 一年内 MM:DD HH:mm
   * 去年 去年 MM:DD HH:mm
   * 前年 前年 MM:DD HH:mm
   * 更远 yyyy:MM:DD HH:mm
   * 毫秒计算
   * @param charttime
   * @return
   */
  public static String convertChatDetailTimeFormat(long charttime) {
  
    long curTime = System.currentTimeMillis() ;
    long time = curTime - charttime;
  
    XCApplication.base_log.i(XCConfig.TAG_SYSTEM_OUT, time + "---时间差" + time/ 1000/ 60 + "分钟");
    XCApplication.base_log.i(XCConfig.TAG_SYSTEM_OUT, curTime + "---当前时间" + format(new Date(curTime), FORMAT_LONG_CN_1));
    XCApplication.base_log.i(XCConfig.TAG_SYSTEM_OUT, charttime + "---chartTime" + format(new Date(charttime), FORMAT_LONG_CN_1));
  
    if (time < 120 * 1000 && time >= 0) {
      return "刚刚";
    } else if (time >= 120 *1000 && time < 3600 * 24 * 1000) {
  
      return format(new Date(charttime), FORMAT_HH_MM);
  
    } else if (time >= 3600 * 24 * 1 * 1000 && time < 3600 * 24 * 2 * 1000) {
  
      return "昨天" + format(new Date(charttime), FORMAT_HH_MM);
  
    } else if (time >= 3600 * 24 * 2 * 1000 && time < 3600 * 24 * 3 * 1000) {
  
      return "前天" + format(new Date(charttime), FORMAT_HH_MM);
    } else if (time >= 3600 * 24 * 3 * 1000 && time < 3600 * 24 * 365 * 1 * 1000) {
  
      return format(new Date(charttime), FORMAT_MM_DD_HH_MM);
    } else if (time >= 3600 * 24 * 365 * 1 * 1000 && time < 3600 * 24 * 365 * 2 * 1000) {
  
      return "去年" + format(new Date(charttime), FORMAT_MM_DD_HH_MM);
    } else if (time >= 3600 * 24 * 365 * 2 * 1000 && time < 3600 * 24 * 365 * 3 * 1000) {
  
      return "前年" + format(new Date(charttime), FORMAT_MM_DD_HH_MM);
    } else if (time >= 3600 * 24 * 365 * 3 * 1000) {
  
      return format(new Date(charttime), FORMAT_LONG_CN_1);
    } else {
      return "刚刚";
    }
  }
Copy after login

There is a small problem here, that is, the natural day time spans the actual day time. It is possible that yesterday's time does not display yesterday, but displays HH:mm, so the test came to the door and asked to change it. The condition of 2 minutes-24 hours is changed to 2 minutes-within today.改 The demand here is changed to

* 2 minutes without display

* 2 minutes-today HH: MM

* Yesterday yesterday yesterday hh: mm
* the day before yesterday hh: mm
* this year mm: dd HH: mm
* MM:DD HH:mm
last year * MM:DD HH:mm
the year before last year * Further yyyy:MM:DD HH:mm


This is not a big problem, the question is what will happen during New Year's Eve? , the message received in the first three minutes of 2015-01-01 00:01.001, that is, whether 2014-12-31 should be displayed as yesterday or last year. How to display the message if the reception time is greater than the time.


After a lot of arguing, it was finally finalized. Here we will modify the product again and require the product to be certified as the final version.

/**
   * 终极方法
   * 将一个时间戳转换成提示性时间字符串,如
   * 2分钟内 无显示
   * 2分钟-今天 2分钟-今天 HH:mm
   * 昨天 昨天 HH:mm
   * 前天 前天 HH:mm
   * 今年 MM:DD HH:mm
   * 去年 去年 MM:DD HH:mm
   * 前年 前年 MM:DD HH:mm
   * 更远 yyyy:MM:DD HH:mm
   * 毫秒计算
   * @param time
   * @return
   */
  public static String convertWEChartTimeFormatFinalMethed(long time) {
    long curTime = System.currentTimeMillis() ;
    String showTimeFormat = "";
  
    long temp = curTime - time;
    if (temp < 120 * 1000 && temp >= 0) {
      showTimeFormat = "";
      return showTimeFormat;
    }
    Date mayTime = new Date(time);
  
//    Date today = UtilDate.parse("2015-01-01 02:02:02.001", UtilDate.FORMAT_FULL);
    Date today = new Date();
    //时间值
    String mayTime_FORMAT_SHORT = format(mayTime, FORMAT_SHORT);
    String mayTime_FORMAT_SHORT_YEAR = getYear(mayTime);
  
    if(mayTime.after(today)){
      //除此以外
      showTimeFormat = format(mayTime, FORMAT_LONG_CN_1);
  
    } else {
      if(mayTime_FORMAT_SHORT != null && !mayTime_FORMAT_SHORT.trim().toString().equals("")){
        //今天的时间yyyy-MM-dd
        String today_str = format(today, FORMAT_SHORT);
        String thisYear_str = getYear(today);
  
        //昨天的时间 yyyy-MM-dd
        Calendar calLastDay = Calendar.getInstance();
        calLastDay.setTime(today);
        calLastDay.add(Calendar.DAY_OF_YEAR, -1);
        System.out.println("昨天:" + format(calLastDay.getTime(), FORMAT_SHORT));
        String lastDay = format(calLastDay.getTime(), FORMAT_SHORT);
  
        //前天的时间 yyyy-MM-dd
        Calendar calPreviousDay = Calendar.getInstance();
        calPreviousDay.setTime(today);
        calPreviousDay.add(Calendar.DAY_OF_YEAR, -2);
        System.out.println("前天:" + format(calPreviousDay.getTime(), FORMAT_SHORT));
        String previousDay = format(calPreviousDay.getTime(), FORMAT_SHORT);
  
        //去年的时间 yyyy
        Calendar calLastYear = Calendar.getInstance();
        calLastYear.setTime(today);
        calLastYear.add(Calendar.YEAR, -1);
        String lastYear = getYear(calLastYear.getTime());
        System.out.println("去年:" + format(calLastYear.getTime(), FORMAT_SHORT));
  
        //前年的时间 yyyy
        Calendar calPreviousYear = Calendar.getInstance();
        calPreviousYear.setTime(today);
        calPreviousYear.add(Calendar.YEAR, -2);
        String previousYear = getYear(calPreviousYear.getTime());
        System.out.println("前年:" + format(calPreviousYear.getTime(), FORMAT_SHORT));
  
        //首先判断是否是今天
        if(mayTime_FORMAT_SHORT.equals(today_str)){
          //今天,则显示为 13:12
          showTimeFormat = format(mayTime, FORMAT_HH_MM);
        } else if(mayTime_FORMAT_SHORT.equals(lastDay)){
          //昨天
          showTimeFormat = "昨天 " + format(mayTime,FORMAT_HH_MM);
  
        } else if(mayTime_FORMAT_SHORT.equals(previousDay)){
          //昨天
          showTimeFormat = "前天 " + format(mayTime,FORMAT_HH_MM);
  
        } else if(mayTime_FORMAT_SHORT_YEAR.equals(thisYear_str)){
          //今年
          showTimeFormat = format(mayTime, FORMAT_MM_DD_HH_MM);
        } else if(mayTime_FORMAT_SHORT_YEAR.equals(lastYear)){
          //去年
          showTimeFormat = "去年 " + format(mayTime, FORMAT_MM_DD_HH_MM);
        } else if(mayTime_FORMAT_SHORT_YEAR.equals(previousYear)){
          //前年
          showTimeFormat = "前年 " + format(mayTime, FORMAT_MM_DD_HH_MM);
        } else {
          //除此以外
          showTimeFormat = format(mayTime, FORMAT_LONG_CN_1);
        }
  
      }
    }
  
  
    return showTimeFormat;
  }
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!