总结一下java时间戳和Android 微博时间戳 的转换问题:
总结一下java时间戳和Android 微博时间戳 的转换问题:
由于精度不同,导致长度不一致,直接转换错误。
JAVA时间戳长度是13位,如:1294890876859
PHP时间戳长度是10位, 如:1294890859
主要最后三位的不同,JAVA时间戳在PHP中使用,去掉后三位,如:1294890876859-> 1294890876 结果:2011-01-13 11:54:36
- echo date('Y-m-d H:i:s','1294890876');
Android 微博时间戳在JAVA中使用,最后加三位,用000补充,如:1294890859->1294890859000
结果:2011-01-13 11:54:19
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateTime = df.format(1294890859000L);
- System.out.println(df);
@SuppressWarnings("deprecation")
public static String getDistanceTime(long time2) {
Date now = new Date();
long day = 0;//天数
long hour = 0;//小时
long min = 0;//分钟
long sec = 0;//秒
try {
long time1 = now.getTime();
time2 = time2*1000l;
long diff ;
if(time1<time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000));
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
} catch (Exception e) {
e.printStackTrace();
}
String rs="";
if (hour==0) {
rs=min+"分钟前";
return rs;
}
if (day==0&&hour<=4) {
rs=hour+"小时前";
return rs;
}
SimpleDateFormat format = new SimpleDateFormat( "MM-dd HH:mm" );//
String d = format.format(time2);
Date date = null;
try {
date = format.parse(d);//把字符类型的转换成日期类型的!
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (now.getDate()-date.getDate()==0) {//当前时间和时间戳转换来的时间的天数对比
DateFormat df2 = new SimpleDateFormat("HH:mm");
rs="今天 "+df2.format(time2);
return rs;
} else if (now.getDate()-date.getDate()==1) {
DateFormat df2 = new SimpleDateFormat("HH:mm");
rs="昨天 "+df2.format(time2);
return rs;
} else {
DateFormat df2 = new SimpleDateFormat("MM-dd HH:mm");
rs=df2.format(time2);
return rs;
}
}
来源:oschina
链接:https://my.oschina.net/u/1016295/blog/175746