新浪微博时间转换工具类

来源:互联网 发布:bose蓝牙音响 知乎 编辑:程序博客网 时间:2024/04/30 11:50

写了一个类似新浪微博的时间转换工具类


import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;/** * 微博时间转换工具 * Created by mo on 3/7/16. */public class TimeUtil {    public static void main(String[] args){        formatTime("Mon Dec 07 19:00:03 +0800 2015");        formatTime("Mon Mar 07 19:00:03 +0800 2016");        formatTime("Mon Mar 07 00:00:03 +0800 2016");        formatTime("Mon Mar 07 18:00:03 +0800 2016");        formatTime("Sun Mar 06 23:59:59 +0800 2016");        formatTime("Sun Mar 06 20:00:03 +0800 2016");        formatTime("Sat Mar 05 20:00:03 +0800 2016");    }    //Sun Jan 02 2011 00:00:00 GMT+0800    public static String formatTime(String time){        String mTime = "--:--:--"; //默认返回值        String timeFormat = "EEE MMM dd HH:mm:ss Z yyyy";        boolean isToday = false;        boolean isYesterday = false;        boolean isThisYear = false;        try {            SimpleDateFormat format = new SimpleDateFormat(timeFormat);            Date oDate = format.parse(time);            Calendar oCalendar = Calendar.getInstance();            oCalendar.setTime(oDate);            int oYear = oCalendar.get(Calendar.YEAR);            int oMonth = oCalendar.get(Calendar.MONTH);            int oDay = oCalendar.get(Calendar.DAY_OF_MONTH);            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");            String oDateStr = sdf.format(oDate);            Date nowDate = new Date();            Calendar nCalendar = Calendar.getInstance();            nCalendar.setTime(nowDate);            int nYear = nCalendar.get(Calendar.YEAR);            int nMonth = nCalendar.get(Calendar.MONTH);            int nDay = nCalendar.get(Calendar.DAY_OF_MONTH);            if(oYear==nYear && oMonth==nMonth && oDay==nDay){                isToday = true;            }            if (oYear==nYear && oMonth==nMonth && nDay-oDay==1){                isYesterday = true;            }            if(oYear==nYear){                isThisYear = true;            }            long strat = oDate.getTime();            long now = nowDate.getTime();            long seconds = (now - strat)/1000;//距离现在多少秒            long minute = seconds/60; //距离现在多少分钟            long hour = seconds/3600; //距离现在多少小时            if(isThisYear){                if(isYesterday){                    //昨天的时间显示                    mTime = "昨天"+oDateStr.substring(oDateStr.indexOf(" "));                } else {                    if(isToday){                        //今天的时间显示                        if(seconds<60){                            mTime = "1分钟";                        } else if(seconds>=60 && seconds<3600){                            mTime = minute+"分钟前";                        } else if(seconds>3600 && seconds<86400 ){                            mTime = hour + "小时前";                        }                    } else {                        //今年以内的时间显示                        mTime = oDateStr.substring(5);                    }                }            } else {                mTime = oDateStr;            }            System.out.println(mTime);        } catch (ParseException e) {            e.printStackTrace();        }        return mTime;    }}



0 0
原创粉丝点击