sina微博时间转化问题

sina api提供的时间获取status.created_at,返回的是String类型,但是不是一般的形式,我试了几种方式,都无果。

给大家看下demo:Tue Dec 08 22:51:39 +0800 2015

百度了一下,貌似是GMT类型的时间格式,也尝试了度娘后的解析方法,仍无果。

这是我百度到的几个貌似靠谱的:

http://www.rigongyizu.com/java-timezone-time-issue-summary/

http://www.mamicode.com/info-detail-260330.html

http://blog.youkuaiyun.com/yanghua_kobe/article/details/7854753

http://blog.youkuaiyun.com/love__coder/article/details/6958427

但是都没有用,解析异常,后来我也试过自己强行解析

 //解析str
		 s = str.split(" ");
		 System.out.println("s []"+s.toString());
		 date = Integer.parseInt(s[2]);
		 str = s[1];
		 month_int = getMonth(str);
		 String time = s[3];
		 String[] str_Time = getIntTime(time);
		 hour = Integer.parseInt(str_Time[0]);
		 minute = Integer.parseInt(str_Time[1]);
		 year = Integer.parseInt(s[5]);
		 System.out.println("year"+year+",month"+month_int+",date"+date+",time"+time+",hour"+hour+",minute"+minute);
/**
	 * 将字母类型的月转化为数字
	 * 
	 * @param month
	 */
	private int getMonth(String month) {
		int i = 1;
		if (month.equals("Dec")) {
			i = 12;
		} else if ("Jan".equals(month)) {
			i = 1;
		} else if (month.equals("Feb")) {
			i = 2;
		} else if (month.equals("Mar")) {
			i = 3;
		} else if (month.equals("Apr")) {
			i = 4;
		} else if (month.equals("May")) {
			i = 5;
		} else if (month.equals("Jun")) {
			i = 6;
		} else if (month.equals("Jul")) {
			i = 7;
		} else if (month.equals("Aug")) {
			i = 8;
		} else if (month.equals("Sep")) {
			i = 9;
		} else if (month.equals("Oct")) {
			i = 10;
		} else if (month.equals("Nov")) {
			i = 11;
		}
		return i;
	}
但是由于要做到类似于sina的效果,需要计算时间差,如果再与当前时间做差的话,精度肯定有问题。

后来请教了个大牛,一句话解决。。。

<pre name="code" class="java">/**
	 * 获取日期
	 * 
	 * @param str
	 * @return
	 */
	private String getDate(String str) {
		currentTime = System.currentTimeMillis();// 获取当前的时间
		dt = new Date(status.created_at);
		String a = dt.toLocaleString();//就是这两句
		
		weiBoTime = dt.getTime();//获取到毫秒级
		System.out.println("a"+a+",str"+str+",weiboTime"+weiBoTime);
		 long timeInterval = (currentTime-weiBoTime)/1000;
		
		 if (timeInterval < 60) {
		 howlongtimeago = "刚刚";
		 }
		 else if((temp = timeInterval/60) <60){
		 howlongtimeago = temp +"分钟前";
		 }
		
		 else if((temp = temp/60) <24){
		 howlongtimeago = temp +"小时前";
		 }
		
		 else if((temp = temp/24) <30){
		 howlongtimeago = temp +"天前";
		 }
		
		 else if((temp = temp/30) <12){
		 howlongtimeago = temp +"月前";
		 }
		 else{
		 temp = temp/12;
		 howlongtimeago = temp +"年前";
		 }
		 
		 System.out.println(currentTime-weiBoTime+"------"+howlongtimeago);//输出输入时间与当前时间的时间差,结果是毫秒数
		 return howlongtimeago;
}


 
 哎,只怪我对api不熟,面壁思过中。。。 

其实这篇blog离目标很近,只怪我没看懂 http://blog.youkuaiyun.com/yanghua_kobe/article/details/7854753


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值