类似于time ago插件的java帮助类
1 public static String getTime(String time) { 2 3 String now = DateUtil.getNow(); 4 if(time == null) { 5 time = DateUtil.getNow(); 6 } 7 int nowYear = Integer.valueOf(now.substring(0, 4)); 8 int year = Integer.valueOf(time.substring(0, 4)); 9 10 String temp; 11 temp = now.substring(now.indexOf("-"),now.lastIndexOf("-")).replace("-", " ").trim(); 12 temp = (temp.indexOf("0") == 0)?temp.replaceAll("0"," ").trim():temp; 13 temp = temp.equals("")?"0":temp; 14 int nm = Integer.valueOf(temp); 15 temp = time.substring(time.indexOf("-"),time.lastIndexOf("-")).replace("-", " ").trim(); 16 temp = (temp.indexOf("0") == 0)?temp.replaceAll("0"," ").trim():temp; 17 temp = temp.equals("")?"0":temp; 18 int m = Integer.valueOf(temp); 19 20 21 temp = now.substring(now.lastIndexOf("-"),now.indexOf(" ")).replace("-", " ").trim(); 22 temp = (temp.indexOf("0") == 0)?temp.replaceAll("0"," ").trim():temp; 23 temp = temp.equals("")?"0":temp; 24 int nd = Integer.valueOf(temp); 25 temp = time.substring(time.lastIndexOf("-"),time.indexOf(" ")).replace("-", " ").trim(); 26 temp = (temp.indexOf("0") == 0)?temp.replaceAll("0"," ").trim():temp; 27 temp = temp.equals("")?"0":temp; 28 int d = Integer.valueOf(temp); 29 30 temp = now.substring(now.indexOf(" "),now.indexOf(":")).trim(); 31 temp = (temp.indexOf("0") == 0)?temp.replaceAll("0"," ").trim():temp; 32 temp = temp.equals("")?"0":temp; 33 int nh = Integer.valueOf(temp); 34 temp = time.substring(time.indexOf(" "),time.indexOf(":")).trim(); 35 temp = (temp.indexOf("0") == 0)?temp.replaceAll("0"," ").trim():temp; 36 temp = temp.equals("")?"0":temp; 37 int h = Integer.valueOf(temp); 38 39 temp = now.substring(now.indexOf(":"),now.lastIndexOf(":")).replace(":", "").trim(); 40 temp = (temp.indexOf("0") == 0)?temp.replaceAll("0"," ").trim():temp; 41 temp = temp.equals("")?"0":temp; 42 int nn = Integer.valueOf(temp); 43 temp = time.substring(time.indexOf(":"),time.lastIndexOf(":")).replace(":", "").trim(); 44 temp = (temp.indexOf("0") == 0)?temp.replaceAll("0"," ").trim():temp; 45 temp = temp.equals("")?"0":temp; 46 int n = Integer.valueOf(temp); 47 48 if(nowYear - year == 0 || ((nowYear - year == 1)&&((nm + 12 - m) / 12 == 0))) { 49 50 if(nm - m == 0 || ((nm - m == 1)&&((nd + 30 - d) / 30 == 0))) { 51 52 if(nd - d == 0 || ((nd - d == 1)&&((nh + 24 - h) / 24 == 0))) { 53 54 if(nh - h == 0 || ((nh - h == 1)&&((nn + 60 - n) / 60 == 0))) { 55 56 if((nn + 60 - n) % 60 <1) { 57 return "刚刚"; 58 } else { 59 return (nn + 60 - n) % 60 + "分钟前"; 60 } 61 } else { 62 return (nh + 24 - h) % 24 + "小时前"; 63 } 64 } else { 65 return (nd + 30 - d) % 30 + "天前"; 66 } 67 } else { 68 return (nm + 12 - m) % 12 + "月前"; 69 } 70 } else { 71 return nowYear - year + "年前"; 72 } 73 }