您的位置:首页 > 其它

类似新浪微博发布时间显示方式 比如 **分钟前 ** 小时前 昨天12:30 前天13:30等

2013-11-06 09:00 423 查看
public static String getDayTime(String daytime){ //daytime 格式为yyyy-MM-dd HH:mm:ss
int mimutes = 0;
int minute = 0;
int days = 0;
int hours = 0;
String timeStr = "";
String time = "";
Date nowdate = new Date();
Date createdate = null;
if (daytime != null && !"".equals(daytime) && daytime.length()==19) {
time = daytime.substring(10,16);
try {
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat format = new SimpleDateFormat(pattern);
createdate = format.parse(daytime);
} catch (ParseException e) {
e.printStackTrace();
}

if (nowdate.getDay()==createdate.getDay()) {
mimutes = TimeUtility.getMinutesDiff(nowdate,createdate);
if (mimutes > 0) {
hours = mimutes/24;
minute = mimutes%24;

if (hours == 0 && minute > 0) {
timeStr = String.valueOf(minute) + "分钟前";
}
if (hours > 0) {
if (hours <= 24 ) {
timeStr = String.valueOf(hours) + "小时前";
}
}
}
}else{
days = nowdate.getDay() - createdate.getDay();
if (days == 1) {
timeStr = "昨天" + time;
}else if (days == 2) {
timeStr = "前天" + time;
}else{
timeStr = daytime.substring(2,16);
}
}
}

return timeStr;

}

public static int getMinutesDiff(Date date1,Date date2){
Calendar c1 = Calendar.getInstance();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
c2.setTime(date2);
return (int) (c1.getTimeInMillis()-c2.getTimeInMillis()) / (60 * 1000);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  新浪微博
相关文章推荐