1 import java.text.SimpleDateFormat; 2 import java.util.Calendar; 3 4 import java.text.DateFormat; 5 6 import java.util.Date; 7 8 /** 9 * Created by Administrator on 2016/3/21. 10 */ 11 public class LongTimeUtil { 12 13 /** 14 * Long类型的时间转换成:n秒前/n分钟前/n小时前/日期 15 * 16 * @param time long类型的时间 17 * @param format 很长时间前显示的日期格式 18 * @return 19 */ 20 public static String dataLongToSNS(long time, String format) { 21 long now = System.currentTimeMillis(); 22 23 long diff = now - time; 24 diff = diff / 1000;// 秒 25 26 if (diff < 0) { 27 return dateLongToString(time, format); 28 } 29 30 if (diff < 30) { // 30秒 31 return "刚刚"; 32 } 33 34 if (diff < 60) { 35 return String.format("%s秒前", diff); 36 } 37 38 if (diff < 3600) { 39 return String.format("%s分钟前", diff / 60); 40 } 41 //获取今天凌晨时间 42 long todayStart = getMorning(new Date()).getTime(); 43 44 if (time >= todayStart) {// 今天 45 return String.format("%s小时前", diff / 3600); 46 } 47 48 if (time < todayStart && time >= todayStart - 86400000) { 49 return "昨天 " + dateLongToString(time, "HH:mm"); 50 } 51 return dateLongToString(time, format); 52 } 53 54 //获取今天凌晨的时间 55 private static Date getMorning(Date date) { 56 Calendar calendar = Calendar.getInstance(); 57 calendar.setTime(date); 58 calendar.set(Calendar.HOUR_OF_DAY, 0); 59 calendar.set(Calendar.MINUTE, 0); 60 calendar.set(Calendar.SECOND, 0); 61 return calendar.getTime(); 62 } 63 64 public static String dateLongToString(long time) { 65 return dateLongToString(time, null); 66 } 67 68 public static String dateLongToString(long time, String format) { 69 if (time <= 0) { 70 return "Empty"; 71 } 72 DateFormat format2 = new SimpleDateFormat(format); 73 String dateString = format2.format(new Date(time * 1000));//此处一定要记得*1000 否则显示1970年的时间 74 return dateString; 75 } 76 }
上面方法有时候效果不佳,下面特意又封装一个工具类,该类使用时候需要注意要将long类型时间*1000,否则有时候会显示1970
1 package king.timeline.com.timeline.utils; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 import java.util.Date; 6 import java.util.Locale; 7 8 /** 9 * Created by Administrator on 2016/3/21. 10 */ 11 public class MyTimeUtil { 12 public static String getTime(long time) { 13 Calendar newCalendar = Calendar.getInstance(); 14 Calendar oldCalendar = Calendar.getInstance(); 15 oldCalendar.setTime(new Date(time)); 16 if (newCalendar.get(Calendar.YEAR) > oldCalendar.get(Calendar.YEAR)) { 17 return getFormatTime(time); 18 } else if (newCalendar.get(Calendar.MONTH) > oldCalendar.get(Calendar.MONTH)) { 19 return (newCalendar.get(Calendar.MONTH) - oldCalendar.get(Calendar.MONTH)) + "月前"; 20 } else if (newCalendar.get(Calendar.DAY_OF_MONTH) > oldCalendar.get(Calendar.DAY_OF_MONTH)) { 21 return (newCalendar.get(Calendar.DAY_OF_MONTH) - oldCalendar.get(Calendar.DAY_OF_MONTH)) + "天前"; 22 } else if (newCalendar.get(Calendar.HOUR) > oldCalendar.get(Calendar.HOUR)) { 23 return (newCalendar.get(Calendar.HOUR) - oldCalendar.get(Calendar.HOUR)) + "小时前"; 24 } else if (newCalendar.get(Calendar.MINUTE) > oldCalendar.get(Calendar.MINUTE)) { 25 return (newCalendar.get(Calendar.MINUTE) - oldCalendar.get(Calendar.MINUTE)) + "分钟前"; 26 } else { 27 return "刚刚"; 28 } 29 } 30 31 private static String getFormatTime(long time) { 32 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); 33 return sdf.format(new Date(time)); 34 } 35 }
转自:https://blog.csdn.net/iblade/article/details/50947939
请使用手机"扫一扫"x