Java返回距离当前时间段

 1     /**
 2      * 计算该时间离当前时间的差距
 3      * @param time 格式为:yyyy-MM-dd HH:mm:ss
 4      * @return
 5      */
 6     public static String getShortTime(String time) {
 7         Date date = getDateByString(time);
 8         return getShortTime(date);
 9     }
10     
11     public static String getShortTime(Date date) {
12         String shortstring = null;
13         long now = Calendar.getInstance().getTimeInMillis();
14         if(date == null) return shortstring;
15         long deltime = (now - date.getTime())/1000;
16         if(deltime > 365*24*60*60) {
17             shortstring = (int)(deltime/(365*24*60*60)) + "年前";
18         } else if(deltime > 7*24*60*60) {
19             shortstring = (int)(deltime/(7*24*60*60)) + "周前";
20         } else if(deltime > 24*60*60) {
21             shortstring = (int)(deltime/(24*60*60)) + "天前";
22         } else if(deltime > 60*60) {
23             shortstring = (int)(deltime/(60*60)) + "小时前";
24         } else if(deltime > 60) {
25             shortstring = (int)(deltime/(60)) + "分钟前";
26         } else if(deltime > 10) {
27             shortstring = deltime + "秒前";
28         } else {
29             shortstring = "刚刚";
30         }
31         return shortstring;
32     }