Android/Java仿微信按时间长短分类显示时间戳

    import java.text.DateFormat;  
    import java.text.SimpleDateFormat;  
    import java.util.Calendar;  
    import java.util.Date;  
      
    /** 
     * Created by xyz on 2017/1/9. 
     */  
      
    public class DateTime {  
        private int hour;  
        private int day;  
        private Date date;  
        private DateFormat df;  
        private String pointText;  
        private Long time;  
        private Long now;  
    //    private  
      
    public DateTime(){  
        hour=60*60*1000;  
        day=(60*60*24)*1000;  
        time=28800L;  
        now=new Date().getTime();  
        pointText="1970-01-01";  
      
      
        }  
      
      
      
        //获得当天0点时间  
        public static Long getTimesmorning(){  
            Calendar cal = Calendar.getInstance();  
            cal.set(Calendar.HOUR_OF_DAY, 0);  
            cal.set(Calendar.SECOND, 0);  
            cal.set(Calendar.MINUTE, 0);  
            cal.set(Calendar.MILLISECOND, 0);  
            return (Long) cal.getTimeInMillis();  
        }  
      
      
        //获取星期几  
        public static String getWeekOfDate(Date dt) {  
            String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};  
            Calendar cal = Calendar.getInstance();  
            cal.setTime(dt);  
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1;  
            if (w < 0)  
                w = 0;  
            return weekDays[w];  
        }  
      
        //获取时间点  
        public String getTimePoint(Long time) {  
            //现在时间  
            now = new Date().getTime();  
      
      
            //时间点比当天零点早  
            if (time <= now&&time!=null) {  
                date = new Date(time);  
                if (time < getTimesmorning()) {  
                    if (time >= getTimesmorning() - day) {//比昨天零点晚  
                        pointText ="昨天";  
                        return pointText;  
                    } else {//比昨天零点早  
                        if (time >= getTimesmorning() - 6 * day) {//比七天前的零点晚,显示星期几  
      
                            return getWeekOfDate(date);  
                        } else {//显示具体日期  
                            df = new SimpleDateFormat("yyyy-MM-dd");  
                            pointText = df.format(date);  
                            return pointText;  
                        }  
                    }  
      
                } else {//无日期时间,当天内具体时间  
                    df = new SimpleDateFormat("HH:mm");  
                    pointText = df.format(date);  
                    return pointText;  
      
                }  
      
            }  
      
            return pointText;  
        }  
      
      
      
    }  

一般外部调用getTimePoint(Long time) 传入Long 型时间戳即可

仿照微信的时间戳显示规则

当天的显示无日期具体时间;

昨天的显示为昨天;

比昨天早且在过去一周内的,显示星期几;

超过一周的时间,显示带有年月日的日期。

http://blog.csdn.net/uyy203/article/details/54441313


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值