package com.landtool.lanbase.common.utils; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; public class TimeIntervalUtil { public static String getInterval(String createtime) { //传入的时间格式必须类似于2012-8-21 17:53:20这样的格式 String interval = null; SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ParsePosition pos = new ParsePosition(0); Date d1 = (Date) sd.parse(createtime, pos); //用现在距离1970年的时间间隔new Date().getTime()减去以前的时间距离1970年的时间间隔d1.getTime()得出的就是以前的时间与现在时间的时间间隔 long time = new Date().getTime() - d1.getTime();// 得出的时间间隔是毫秒 if(time/1000 < 10 && time/1000 >= 0) { //如果时间间隔小于10秒则显示“刚刚”time/10得出的时间间隔的单位是秒 interval ="刚刚"; } else if(time/3600000 < 24 && time/3600000 > 0) { //如果时间间隔小于24小时则显示多少小时前 int h = (int) (time/3600000);//得出的时间间隔的单位是小时 interval = h + "小时前"; } else if(time/60000 < 60 && time/60000 > 0) { //如果时间间隔小于60分钟则显示多少分钟前 int m = (int) ((time%3600000)/60000);//得出的时间间隔的单位是分钟 interval = m + "分钟前"; } else if(time/1000 < 60 && time/1000 > 0) { //如果时间间隔小于60秒则显示多少秒前 int se = (int) ((time%60000)/1000); interval = se + "秒前"; } else if(time/3600000/24 < 24 && time/3600000/24 >= 0 ) { //如果时间间隔大于24小时则显示多少天前 int d = (int) (time/3600000/24); interval = d + "天前"; } else if(time/3600000/24/30 < 12 && time/3600000/24/30 >= 0 ) { //如果时间间隔大于30天,则显示多少个月前 int d = (int) (time/3600000/24/30); interval = d + "个月前"; } else { //大于12个月,则显示正常的时间,但是不显示时分秒 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); ParsePosition pos2 = new ParsePosition(0); Date d2 = (Date) sdf.parse(createtime, pos2); interval = sdf.format(d2); } return interval; } }