zoukankan      html  css  js  c++  java
  • 聊天的时间显示

    之前很是头疼聊天显示时间的问题,什么上午、下午、晚上、凌晨,昨天,两天前,三天前的,因此整理一个工具类分享下:

      1 package com.mogujie.tt.utils;
      2 
      3 import java.text.ParseException;
      4 import java.text.SimpleDateFormat;
      5 import java.util.Calendar;
      6 import java.util.Date;
      7 import java.util.Locale;
      8 
      9 import android.annotation.SuppressLint;
     10 
     11 /**
     12  * @Description
     13  * @author Nana
     14  * @date 2014-4-10
     15  */
     16 public class DateUtil {
     17 
     18     private static Date preRefreshDateTime;
     19 
     20     public static boolean toRefresh() {
     21         Date curdate = new Date();
     22         if (null == preRefreshDateTime) {
     23             preRefreshDateTime = curdate;
     24             return true;
     25         }
     26 
     27         long timediff = (curdate.getTime() - preRefreshDateTime.getTime());
     28         preRefreshDateTime = curdate;
     29         return (timediff >= 60 * 60 * 1000);
     30     }
     31 
     32     @SuppressLint("SimpleDateFormat")
     33     public static Date getDate(Long dateLong) {
     34         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     35         String d = format.format(dateLong);
     36         Date date;
     37         try {
     38             date = format.parse(d);
     39         } catch (ParseException e) {
     40             e.printStackTrace();
     41             return null;
     42         }
     43         return date;
     44     }
     45 
     46     public static boolean needDisplayTime(Date predate, Date curdate) {
     47         if (predate == null || curdate == null) {
     48             return true;
     49         }
     50 
     51         long timediff = (curdate.getTime() - predate.getTime());
     52         return (timediff >= 5 * 60 * 1000);
     53     }
     54 
     55     public static String getTimeDiffDesc(Date date) {
     56 
     57         if (date == null) {
     58             return null;
     59         }
     60 
     61         String strDesc = null;
     62         Calendar curCalendar = Calendar.getInstance();
     63         Date curDate = new Date();
     64         curCalendar.setTime(curDate);
     65         Calendar thenCalendar = Calendar.getInstance();
     66         thenCalendar.setTime(date);
     67 
     68         String[] weekDays = {
     69                 "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
     70         };
     71         int w = thenCalendar.get(Calendar.DAY_OF_WEEK) - 1;
     72         if (w < 0)
     73             w = 0;
     74         // SimpleDateFormat format = new
     75         // SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     76         Calendar current = Calendar.getInstance();
     77         Calendar today = Calendar.getInstance(); // 今天
     78         today.set(Calendar.YEAR, current.get(Calendar.YEAR));
     79         today.set(Calendar.MONTH, current.get(Calendar.MONTH));
     80         today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
     81         today.set(Calendar.HOUR_OF_DAY, 0);
     82         today.set(Calendar.MINUTE, 0);
     83         today.set(Calendar.SECOND, 0);
     84         // Date datetoday = today.getTime();
     85         // System.out.println(format.format(datetoday));
     86 
     87         Calendar yesterday = Calendar.getInstance(); // 昨天
     88         yesterday.setTime(curDate);
     89         yesterday.add(Calendar.DATE, -1);
     90         yesterday.set(Calendar.HOUR_OF_DAY, 0);
     91         yesterday.set(Calendar.MINUTE, 0);
     92         yesterday.set(Calendar.SECOND, 0);
     93         // Date dateyestoday = yesterday.getTime();
     94         // System.out.println(format.format(dateyestoday));
     95 
     96         Calendar sevendaysago = Calendar.getInstance(); // 7天
     97         sevendaysago.setTime(curDate);
     98         sevendaysago.add(Calendar.DATE, -7);
     99         sevendaysago.set(Calendar.HOUR_OF_DAY, 0);
    100         sevendaysago.set(Calendar.MINUTE, 0);
    101         sevendaysago.set(Calendar.SECOND, 0);
    102         // Date datesevenago = sevendaysago.getTime();
    103         // System.out.println(format.format(datesevenago));
    104         /*
    105          * Date tasktime = yesterday.getTime(); SimpleDateFormat df=new
    106          * SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    107          * System.out.println(df.format(tasktime));
    108          */
    109 
    110         int thenMonth = thenCalendar.get(Calendar.MONTH);
    111         int thenDay = thenCalendar.get(Calendar.DAY_OF_MONTH);
    112         int h = thenCalendar.get(Calendar.HOUR_OF_DAY);
    113         int m = thenCalendar.get(Calendar.MINUTE);
    114         String sh = "", sm = "";
    115         if (h < 10)
    116             sh = "0";
    117 
    118         if (m < 10)
    119             sm = "0";
    120         if (thenCalendar.after(today))// today
    121         {
    122             if (h < 6) {
    123                 strDesc = "凌晨 " + sh + h + " : " + sm + m;
    124             } else if (h < 12) {
    125                 strDesc = "上午 " + sh + h + " : " + sm + m;
    126             } else if (h < 13) {
    127                 strDesc = "下午 " + h + " : " + sm + m;
    128             } else if (h < 19) {
    129                 strDesc = "下午 " + (h - 12) + " : " + sm + m;
    130             } else {
    131                 strDesc = "晚上 " + (h - 12) + " : " + sm + m;
    132             }
    133         } else if (thenCalendar.before(today) && thenCalendar.after(yesterday)) {// yestoday
    134             // System.out.println("yestoday");
    135             if (h < 6) {
    136                 strDesc = "昨天凌晨 " + sh + h + " : " + sm + m;
    137             } else if (h < 12) {
    138                 strDesc = "昨天上午 " + sh + h + " : " + sm + m;
    139             } else if (h < 13) {
    140                 strDesc = "昨天下午 " + h + " : " + sm + m;
    141             } else if (h < 19) {
    142                 strDesc = "昨天下午 " + (h - 12) + " : " + sm + m;
    143             } else {
    144                 strDesc = "昨天晚上 " + (h - 12) + " : " + sm + m;
    145             }
    146         } else if (thenCalendar.before(yesterday)
    147                 && thenCalendar.after(sevendaysago)) {// 2 ~ 7days ago
    148             // System.out.println("2~7");
    149             if (h < 6) {
    150                 strDesc = weekDays[w] + "凌晨 " + sh + h + " : " + sm + m;
    151             } else if (h < 12) {
    152                 strDesc = weekDays[w] + "上午 " + sh + h + " : " + sm + m;
    153             } else if (h < 13) {
    154                 strDesc = weekDays[w] + "下午 " + h + " : " + sm + m;
    155             } else if (h < 19) {
    156                 strDesc = weekDays[w] + "下午 " + (h - 12) + " : " + sm + m;
    157             } else {
    158                 strDesc = weekDays[w] + "晚上 " + (h - 12) + " : " + sm + m;
    159             }
    160         } else {
    161             // System.out.println("7~");
    162             if (h < 6) {
    163                 strDesc = (thenMonth + 1) + "月" + thenDay + "日" + "凌晨 " + sh
    164                         + h + " : " + sm + m;
    165             } else if (h < 12) {
    166                 strDesc = (thenMonth + 1) + "月" + thenDay + "日" + "上午 " + sh
    167                         + h + " : " + sm + m;
    168             } else if (h < 13) {
    169                 strDesc = (thenMonth + 1) + "月" + thenDay + "日" + "下午 " + h
    170                         + " : " + sm + m;
    171             } else if (h < 19) {
    172                 strDesc = (thenMonth + 1) + "月" + thenDay + "日" + "下午 "
    173                         + (h - 12) + " : " + sm + m;
    174             } else {
    175                 strDesc = (thenMonth + 1) + "月" + thenDay + "日" + "晚上 "
    176                         + (h - 12) + " : " + sm + m;
    177             }
    178         }
    179         // System.out.println(strDesc);
    180         return strDesc;
    181     }
    182 
    183     public static String getTimeDisplay(Date date) {
    184 
    185         if (date == null) {
    186             return null;
    187         }
    188 
    189         String strDesc = null;
    190         Calendar curCalendar = Calendar.getInstance();
    191         Date curDate = new Date();
    192         curCalendar.setTime(curDate);
    193         Calendar thenCalendar = Calendar.getInstance();
    194         thenCalendar.setTime(date);
    195 
    196         String[] weekDays = {
    197                 "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
    198         };
    199         int w = thenCalendar.get(Calendar.DAY_OF_WEEK) - 1;
    200         if (w < 0)
    201             w = 0;
    202         // SimpleDateFormat format = new
    203         // SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    204         Calendar current = Calendar.getInstance();
    205         Calendar today = Calendar.getInstance(); // 今天
    206         today.set(Calendar.YEAR, current.get(Calendar.YEAR));
    207         today.set(Calendar.MONTH, current.get(Calendar.MONTH));
    208         today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
    209         today.set(Calendar.HOUR_OF_DAY, 0);
    210         today.set(Calendar.MINUTE, 0);
    211         today.set(Calendar.SECOND, 0);
    212 
    213         Calendar yesterday = Calendar.getInstance(); // 昨天
    214         yesterday.setTime(curDate);
    215         yesterday.add(Calendar.DATE, -1);
    216         yesterday.set(Calendar.HOUR_OF_DAY, 0);
    217         yesterday.set(Calendar.MINUTE, 0);
    218         yesterday.set(Calendar.SECOND, 0);
    219 
    220         Calendar sevendaysago = Calendar.getInstance(); // 7天
    221         sevendaysago.setTime(curDate);
    222         sevendaysago.add(Calendar.DATE, -7);
    223         sevendaysago.set(Calendar.HOUR_OF_DAY, 0);
    224         sevendaysago.set(Calendar.MINUTE, 0);
    225         sevendaysago.set(Calendar.SECOND, 0);
    226 
    227         long timediff = (curDate.getTime() - date.getTime());
    228         int theYear = thenCalendar.get(Calendar.YEAR);
    229         int thenMonth = thenCalendar.get(Calendar.MONTH);
    230         int thenDay = thenCalendar.get(Calendar.DAY_OF_MONTH);
    231         if (thenCalendar.after(today))// today
    232         {
    233             if (timediff < 5 * 60 * 1000) {
    234                 strDesc = "刚刚";
    235             } else if (timediff < 60 * 60 * 1000) {
    236                 strDesc = (timediff / 60 / 1000) + "分钟之前";
    237             } else {
    238                 strDesc = (timediff / 3600 / 1000) + "小时之前";
    239             }
    240         } else if (thenCalendar.before(today) && thenCalendar.after(yesterday)) {// yestoday
    241             strDesc = "昨天";
    242         } else if (thenCalendar.before(yesterday)
    243                 && thenCalendar.after(sevendaysago)) {// 2 ~ 7days ago
    244             strDesc = weekDays[w];
    245         } else {
    246             strDesc = "" + (theYear - 2000) + "-" + (thenMonth + 1) + "-"
    247                     + thenDay;
    248         }
    249         return strDesc;
    250     }
    251 
    252     public static String getTime4TimeTitle(Date date) {
    253         // M:月 d:天 a:上午或下午 h:12小时制的小时 m:分钟
    254         SimpleDateFormat format = new SimpleDateFormat("MM-dd a hh:mm",
    255                 Locale.CHINA);
    256         return format.format(date);
    257     }
    258 
    259     public static int getCurTimeStamp() {
    260 
    261         return (int) (System.currentTimeMillis() / 1000);
    262 
    263     }
    264 }
  • 相关阅读:
    CodeBlocks "no such file or directory" 错误解决方案(创建类找不到头文件)
    WCF配置文件与文件下载之坎坷路
    使用Visual Studio 2010打造C语言编译器
    一个小程序引发的思考
    在C#使用文件监控对象FileSystemWatcher 实现数据同步
    C 语言 static、extern与指针函数介绍
    检测端口是否被占用
    C# ini文件读写类
    C学习笔记(2)--指针
    plsql auto 常用语法
  • 原文地址:https://www.cnblogs.com/dongweiq/p/4142743.html
Copyright © 2011-2022 走看看