public static String ChangeTime(Date time) {
SimpleDateFormat dateFormat = new SimpleDateFormat();
String ftime = "";
Calendar cal = Calendar.getInstance(); // 判断是否是同一天
String curDate = dateFormat.format(cal.getTime());
String paramDate = dateFormat.format(time);
if (curDate.equals(paramDate)) {
int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
if (hour == 0)
ftime = Math.max(
(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
+ "分钟前";
else
ftime = hour + "小时前";
return ftime;
}
long lt = time.getTime() / 86400000;
long ct = cal.getTimeInMillis() / 86400000;
int days = (int) (ct - lt);
if (days == 0) {
int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
if (hour == 0)
ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1)
+ "分钟前";
else
ftime = hour + "小时前";
} else if (days == 1) {
ftime = "昨天";
} else if (days == 2) {
ftime = "前天 ";
} else if (days > 2 && days < 31) {
ftime = days + "天前";
} else if (days >= 31 && days <= 2 * 31) {
ftime = "一个月前";
} else if (days > 2 * 31 && days <= 3 * 31) {
ftime = "2个月前";
} else if (days > 3 * 31 && days <= 4 * 31) {
ftime = "3个月前";
} else {
ftime = dateFormat.format(time);
}
return ftime;
}
public static String friendly_time(String sdate) {
Date time = null;
SimpleDateFormat dateFormat = new SimpleDateFormat();
// if (TimeZoneUtil.isInEasternEightZones())
// time = toDate(sdate);
// else
// time = TimeZoneUtil.transformTime(toDate(sdate),
// TimeZone.getTimeZone("GMT+08"), TimeZone.getDefault());
//
// if (time == null) {
//
// return "Unknown";
// }
time = new Date(Long.valueOf(sdate));
String ftime = "";
Calendar cal = Calendar.getInstance(); // 判断是否是同一天
String curDate = dateFormat.format(cal.getTime());
String paramDate = dateFormat.format(time);
if (curDate.equals(paramDate)) {
int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
if (hour == 0)
ftime = Math.max(
(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
+ "分钟前";
else
ftime = hour + "小时前";
return ftime;
}
long lt = time.getTime() / 86400000;
long ct = cal.getTimeInMillis() / 86400000;
int days = (int) (ct - lt);
if (days == 0) {
int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
if (hour == 0)
ftime = Math.max(
(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
+ "分钟前";
else
ftime = hour + "小时前";
} else if (days == 1) {
ftime = "昨天";
} else if (days == 2) {
ftime = "前天 ";
} else if (days > 2 && days < 31) {
ftime = days + "天前";
} else if (days >= 31 && days <= 2 * 31) {
ftime = "一个月前";
} else if (days > 2 * 31 && days <= 3 * 31) {
ftime = "2个月前";
} else if (days > 3 * 31 && days <= 4 * 31) {
ftime = "3个月前";
} else {
ftime = dateFormat.format(time);
}
return ftime;
}
/*
* 将时间转换为时间戳
*/
public static String dateToStamp(String time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = simpleDateFormat.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
long ts = date.getTime();
return String.valueOf(ts);
}