java 對新浪微博微博的發表時間解析

新浪微博時間的返回格式 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);//將時間字符串 轉爲格式爲yyyyMMddHHmmss
		
		SimpleDateFormat 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 block
			e.printStackTrace();
		}
		
		return result;
	}

	/**
	 * 
	 *  根據時間差來判斷時間顯示的格式
	 * 
	 * 小於一分鐘:顯示多少秒之前發表
	 * 大於等於一分鐘 : 顯示多少分鐘之前發表
	 * 大於一小時 統一顯示顯示格式爲 yyyy-MM-dd
	 * @param temp
	 * @param createMillSeconds
	 * @return
	 */
	public static String getPublicTime(long temp,long createMillSeconds) {
		// TODO Auto-generated method stub
		String 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 stub
		String 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) {//月小於九的時候 拼接上一個0
					result = "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();是時間轉化的方法入口,只要調用這一個方法就可以了。
發佈了41 篇原創文章 · 獲贊 9 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章