> php教程 > PHP开发 > 본문

WeChat 및 QQ 시간 형식 템플릿 예에 대한 자세한 설명

高洛峰
풀어 주다: 2016-12-09 13:28:21
원래의
1396명이 탐색했습니다.

코드에 바로가서 댓글도 있으니 봐주세요!

/**
   * 将一个时间戳转换成提示性时间字符串,如
   * 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 "刚刚";
    }
  }
로그인 후 복사

여기에는 작은 문제가 있습니다. 즉, 자연적인 낮 시간이 실제 낮 시간에 걸쳐 표시되지 않을 수 있습니다. 어제는 HH:mm으로 표시되서 테스터가 문을 찾아와서 2분-24시간-을 오늘 내로 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

문제는 설날에 무엇을 하느냐가 첫 3분 만에 들어온 소식이다. 2015-01-01 00:01.001은 2014-12 -31입니다. 표시가 어제여야 하는지 아니면 작년인지 확인하세요. 수신 시간이 시간보다 큰 경우 메시지를 표시하는 방법입니다.

많은 논쟁 끝에 최종 확정되었습니다. 여기서 제품을 다시 수정하여 최종 버전으로 인증을 받아야 합니다.

/**
   * 终极方法
   * 将一个时间戳转换成提示性时间字符串,如
   * 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;
  }
로그인 후 복사


관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!