今天整理到一个需求是 如果是今天的时间只需要显示时分就好 要是昨天的就显示 昨天 前天的显示前天 其他的就显示年月日时分秒了
代码如下
/*** 和当前时间比较*/public static String getMyData(String start) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat(\"yyyy-MM-dd HH:mm\");long s1 = Long.parseLong(start);Date date1 = new Date(s1);Date date2;date2 = new Date(System.currentTimeMillis());// 现在这条消息的时间String str1 = simpleDateFormat.format(date1);int year1 = Integer.parseInt(str1.substring(0, 4));int month1 = Integer.parseInt(str1.substring(5, 7));int day1 = Integer.parseInt(str1.substring(8, 10));int h1 = Integer.parseInt(str1.substring(11, 13));int m1 = Integer.parseInt(str1.substring(14, 16));// 现在的时间String str2 = simpleDateFormat.format(date2);int year2 = Integer.parseInt(str2.substring(0, 4));int month2 = Integer.parseInt(str2.substring(5, 7));int day2 = Integer.parseInt(str2.substring(8, 10));int h2 = Integer.parseInt(str2.substring(11, 13));int m2 = Integer.parseInt(str2.substring(14, 16));if (year2 > year1) {return year1 + \"-\" + str1.substring(5, 7) + \"-\" + str1.substring(8, 10);} else if (month2 > month1) {return year1 + \"-\" + str1.substring(5, 7) + \"-\" + str1.substring(8, 10);} else if (day2 > day1) {if (day2 - day1 > 2) {return year1 + \"-\" + str1.substring(5, 7) + \"-\" + str1.substring(8, 10);} else if (day2 - day1 == 2) {return \"前天\";} else if (day2 - day1 == 1) {return \"昨天\";} else {return str1.substring(11, 13) + \":\" + str1.substring(14, 16);}} else {return str1.substring(11, 13) + \":\" + str1.substring(14, 16);}}