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

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值