java 微博发布时间_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();是时间转化的方法入口,只要调用这一个方法就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值