sina微博时间转化问题

来源:互联网 发布:软件技术支持岗位职责 编辑:程序博客网 时间:2024/05/29 18:05

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.csdn.net/yanghua_kobe/article/details/7854753

http://blog.csdn.net/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.csdn.net/yanghua_kobe/article/details/7854753


0 0