java 对新浪微博微博的发表时间解析

来源:互联网 发布:mac怎么创建压缩文件夹 编辑:程序博客网 时间:2024/06/01 10:43
新浪微博时间的返回格式 Thu Aug 13 18:03:49 +0800 2015,Thu和+0800我也不晓得是什么,就没管这两个。我将微博的发表时间先统一格式化为20150813180349的格式,然后获取系统时间也格式化为这个形式,得到两个时间的毫秒差值,根据差值来确定时间的显示格式。虽然下面的代码是针对新浪微博的进行编写的,不过我觉得以后可能还会在其他地方用得着,索性就记录下来。上代码。
package com.lql.tools;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;/** *  * 时间格式为:Thu Aug 13 18:03:49 +0800 2015 时间转化的工具类 *  * @author Administrator *  */public class TimeTools {/** * @param timeString * @return */public static String getCreatedTime(String timeString) {String result = "";String tmp = formatDate(timeString);//将时间字符串 转为格式为yyyyMMddHHmmssSimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");try {long millSeconds = sdf.parse(tmp).getTime();//发表时间的毫秒数,是距离1970年1月1日的毫秒数String currentTime= sdf.format(new Date());//系统当前时间long nowMillSeconds = sdf.parse(currentTime).getTime();//系统当前时间距离1970年1月1日的毫秒数long temp = nowMillSeconds - millSeconds;//两者之间的时间差result = getPublicTime(temp,millSeconds);//根据时间差的多少来判断时间的最终显示格式} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}/** *  *  根据时间差来判断时间显示的格式 *  * 小于一分钟:显示多少秒之前发表 * 大于等于一分钟 : 显示多少分钟之前发表 * 大于一小时 统一显示显示格式为 yyyy-MM-dd * @param temp * @param createMillSeconds * @return */public static String getPublicTime(long temp,long createMillSeconds) {// TODO Auto-generated method stubString result = "";long millSecondsOfDay = 86400000;//一天的毫秒数long millSecondsOfHour = 3600000;//一个小时的毫秒数long millSecondOfMinute = 60000;//一分钟的毫秒数if(temp<0){//如果差是负数就直接显示微博发表日期(负数说明系统时间存在问题,就直接显示发表日期)result = parseMillSecondsToDate(createMillSeconds,2);}else if(temp < millSecondOfMinute && temp >= 0){//小于一分钟就显示 多少秒前result = Math.ceil(temp / 1000) + "秒前";//上取整}else if(temp >= millSecondOfMinute && temp < millSecondsOfHour){result = Math.ceil(temp / 1000 / 1000) +"分钟前";//小于一个小时大于一分钟就显示 多少分钟前}else if(temp >= millSecondsOfHour && temp < millSecondsOfDay){result = "今天  "+parseMillSecondsToDate(createMillSeconds,1);//大于一小时就显示今天几点的几点}else{result = parseMillSecondsToDate(createMillSeconds,2);//大于一天就是发表日期}return result;}/** *  * 毫秒转化为时间 * @param createMillSeconds * @return */public static String parseMillSecondsToDate(long createMillSeconds,int flag) {// TODO Auto-generated method stubString result = "";SimpleDateFormat sdf;Date date = new Date(createMillSeconds); //将毫秒转为时间转化为    if(flag == 2){sdf = new SimpleDateFormat("yyyy-MM-dd"); result = sdf.format(date);    }else if(flag == 1){    sdf = new SimpleDateFormat("HH:mm");    result = sdf.format(date);    }else{    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     result = sdf.format(date);    }return result;}/** *  * 将英文格式的月转化为数字格式月 *  * @param englishFormatMonth *            英文月简写 Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, *            Dec * @return */public static String getNumFormatMonth(String englishFormatMonth) {String result = "";String[] month = new String[] { "", "Jan", "Feb", "Mar", "Apr", "May","Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };for (int i = 1; i < 13; i++) {if (englishFormatMonth.equals(month[i])) {if (i <= 9) {//月小于九的时候 拼接上一个0result = "0" + String.valueOf(i);}else{result = String.valueOf(i);}break;}}return result;}/** * 将18:12:23 转化为181223的格式 *  * @param timeStr * @return */public static String formatTime(String timeStr) {StringBuffer buffer = new StringBuffer(timeStr);buffer.deleteCharAt(2);buffer.deleteCharAt(4);return buffer.toString();}/** * 将Thu Aug 13 18:03:49 +0800 2015的格式 转为201408 *  * @param tmp * @return */public static String formatDate(String tmp) {StringBuffer result = new StringBuffer("");String[] splitedStr = tmp.split(" ");// 月String month = getNumFormatMonth(splitedStr[1]);// 日String day = splitedStr[2];// 时间String time = formatTime(splitedStr[3]);// 年String year = splitedStr[5];result.append(year);result.append(month);result.append(day);result.append(time);return result.toString();}}
上面的getCreatedTime();是时间转化的方法入口,只要调用这一个方法就可以了。
0 0
原创粉丝点击