package test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by luan on 2015/9/15.
*/
public class PrettyTime{
private final static long minute = 60 * 1000;// 1分钟
private final static long hour = 60 * minute;// 1小时
private final static long day = 24 * hour;// 1天
private final static long month = 31 * day;// 月
private final static long year = 12 * month;// 年
public static void main(String[] args) throws ParseException {
String m = "2015-09-15 09:35:01";
System.out.printf(getTimeFormatText(m));
}
/**
* 返回文字描述的日期
* @param date
* @return
*/
public static String getTimeFormatText(String date) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d;
try {
d = f.parse(date);
} catch (ParseException e) {
return date;
}
return getTimeFormatText(d);
}
/**
* 返回文字描述的日期
*
* @param date
* @return
*/
public static String getTimeFormatText(Date date) {
if (date == null) {
return null;
}
long diff = new Date().getTime() - date.getTime();
long r = 0;
if (diff > year) {
r = (diff / year);
return r + "年前";
}
if (diff > month) {
r = (diff / month);
return r + "个月前";
}
if (diff > day) {
r = (diff / day);
return r + "天前";
}
if (diff > hour) {
r = (diff / hour);
return r + "个小时前";
}
if (diff > minute) {
r = (diff / minute);
return r + "分钟前";
}
return "刚刚";
}
}