Java时间工具类DateUtils(把日期时间转换成xx秒前、xx分钟前、xx小时前...)

时间:2022-09-16 15:13:25

 java中给定时间字符串,转换为距离现在几分钟,几天,几周前

importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
 
publicclass DateUtils {
    privatestatic final long ONE_MINUTE = 60;
    privatestatic final long ONE_HOUR = 3600;
    privatestatic final long ONE_DAY = 86400;
    privatestatic final long ONE_MONTH = 2592000;
    privatestatic final long ONE_YEAR = 31104000;
 
    publicstatic Calendar calendar = Calendar.getInstance();
 
    /**
     *
     * @return yyyy-mm-dd
     *  2012-12-25
     */
    publicstatic String getDate() {
        returngetYear() + "-"+ getMonth() + "-"+ getDay();
    }
 
    /**
     * @param format
     * @return 
     * yyyy年MM月dd HH:mm 
     * MM-dd HH:mm 2012-12-25
     *
     */
    publicstatic String getDate(String format) {
        SimpleDateFormat simple = newSimpleDateFormat(format);
        returnsimple.format(calendar.getTime());
    }
 
    /**
     *
     * @return yyyy-MM-dd HH:mm 
     * 2012-12-29 23:47
     */
    publicstatic String getDateAndMinute() {
        SimpleDateFormat simple = newSimpleDateFormat("yyyy-MM-dd HH:mm");
        returnsimple.format(calendar.getTime());
    }
 
    /**
     *
     * @return
     *  yyyy-MM-dd HH:mm:ss 
     *  2012-12-29 23:47:36
     */
    publicstatic String getFullDate() {
        SimpleDateFormat simple = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        returnsimple.format(calendar.getTime());
    }
 
    /**
     * 距离今天多久
     * @param date
     * @return 
     *
     */
    publicstatic String fromToday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
 
        longtime = date.getTime() / 1000;
        longnow = newDate().getTime() / 1000;
        longago = now - time;
        if(ago <= ONE_HOUR)
            returnago / ONE_MINUTE + "分钟前";
        elseif (ago <= ONE_DAY)
            returnago / ONE_HOUR + "小时"+ (ago % ONE_HOUR / ONE_MINUTE)
                    +"分钟前";
        elseif (ago <= ONE_DAY * 2)
            return"昨天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        elseif (ago <= ONE_DAY * 3)
            return"前天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        elseif (ago <= ONE_MONTH) {
            longday = ago / ONE_DAY;
            returnday + "天前"+ calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        }elseif (ago <= ONE_YEAR) {
            longmonth = ago / ONE_MONTH;
            longday = ago % ONE_MONTH / ONE_DAY;
            returnmonth + "个月"+ day + "天前"
                    + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        }else{
            longyear = ago / ONE_YEAR;
            intmonth = calendar.get(Calendar.MONTH) + 1;// JANUARY which is 0 so month+1
            returnyear + "年前"+ month + "月"+ calendar.get(Calendar.DATE)
                    +"日";
        }
 
    }
 
    /**
     * 距离截止日期还有多长时间
     *
     * @param date
     * @return
     */
    publicstatic String fromDeadline(Date date) {
        longdeadline = date.getTime() / 1000;
        longnow = (newDate().getTime()) / 1000;
        longremain = deadline - now;
        if(remain <= ONE_HOUR)
            return"只剩下" + remain / ONE_MINUTE + "分钟";
        elseif (remain <= ONE_DAY)
            return"只剩下" + remain / ONE_HOUR + "小时"
                    + (remain % ONE_HOUR / ONE_MINUTE) + "分钟";
        else{
            longday = remain / ONE_DAY;
            longhour = remain % ONE_DAY / ONE_HOUR;
            longminute = remain % ONE_DAY % ONE_HOUR / ONE_MINUTE;
            return"只剩下" + day + "天"+ hour + "小时"+ minute + "分钟";
        }
 
    }
 
    /**
     * 距离今天的绝对时间
     *
     * @param date
     * @return
     */
    publicstatic String toToday(Date date) {
        longtime = date.getTime() / 1000;
        longnow = (newDate().getTime()) / 1000;
        longago = now - time;
        if(ago <= ONE_HOUR)
            returnago / ONE_MINUTE + "分钟";
        elseif (ago <= ONE_DAY)
            returnago / ONE_HOUR + "小时"+ (ago % ONE_HOUR / ONE_MINUTE) + "分钟";
        elseif (ago <= ONE_DAY * 2)
            return"昨天" + (ago - ONE_DAY) / ONE_HOUR + "点"+ (ago - ONE_DAY)
                    % ONE_HOUR / ONE_MINUTE + "分";
        elseif (ago <= ONE_DAY * 3) {
            longhour = ago - ONE_DAY * 2;
            return"前天" + hour / ONE_HOUR + "点"+ hour % ONE_HOUR / ONE_MINUTE
                    +"分";
        }elseif (ago <= ONE_MONTH) {
            longday = ago / ONE_DAY;
            longhour = ago % ONE_DAY / ONE_HOUR;
            longminute = ago % ONE_DAY % ONE_HOUR / ONE_MINUTE;
            returnday + "天前"+ hour + "点"+ minute + "分";
        }elseif (ago <= ONE_YEAR) {
            longmonth = ago / ONE_MONTH;
            longday = ago % ONE_MONTH / ONE_DAY;
            longhour = ago % ONE_MONTH % ONE_DAY / ONE_HOUR;
            longminute = ago % ONE_MONTH % ONE_DAY % ONE_HOUR / ONE_MINUTE;
            returnmonth + "个月"+ day + "天"+ hour + "点"+ minute + "分前";
        }else{
            longyear = ago / ONE_YEAR;
            longmonth = ago % ONE_YEAR / ONE_MONTH;
            longday = ago % ONE_YEAR % ONE_MONTH / ONE_DAY;
            returnyear + "年前"+ month + "月"+ day + "天";
        }
 
    }
 
    publicstatic String getYear() {
        returncalendar.get(Calendar.YEAR) + "";
    }
 
    publicstatic String getMonth() {
        intmonth = calendar.get(Calendar.MONTH) + 1;
        returnmonth + "";
    }
 
    publicstatic String getDay() {
        returncalendar.get(Calendar.DATE) + "";
    }
 
    publicstatic String get24Hour() {
        returncalendar.get(Calendar.HOUR_OF_DAY) + "";
    }
 
    publicstatic String getMinute() {
        returncalendar.get(Calendar.MINUTE) + "";
    }
 
    publicstatic String getSecond() {
        returncalendar.get(Calendar.SECOND) + "";
    }
 
    publicstatic void main(String[] args) throwsParseException {
        String deadline = "2012-12-30 12:45:59";
        Date date = newDate();
        SimpleDateFormat simple = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        date = simple.parse(deadline);
        System.out.println(DateUtils.fromDeadline(date));
 
        String before = "2012-12-12 0:0:59";
        date = simple.parse(before);
        System.out.println(DateUtils.fromToday(date));
 
        System.out.println(DateUtils.getFullDate());
        System.out.println(DateUtils.getDate());
    }
 
}



实用的一个类:
<span style="font-size:24px;">
</span><span style="font-size:14px;">import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	 private static final long ONE_MINUTE = 60;
	 private static final long ONE_HOUR = 3600;
	 private static final long ONE_DAY = 86400;
	 private static final long ONE_MONTH = 2592000;
	 private static final long ONE_YEAR = 31104000;
	 
	 public static Calendar calendar = Calendar.getInstance();
	 
	 /**
	  * 
	  * @param timestr
	  * @return 距离现在
	  */
	 public static String fromToday(String timestr){
		 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		 Date date = null;
			try {
				date = df.parse(timestr);
			} catch (ParseException e) {
				e.printStackTrace();
				return "未知时间";
			}
		 Calendar calendar = Calendar.getInstance();
		 calendar.setTime(date);
		 
		 long time = date.getTime() / 1000;
		 long now = new Date().getTime() / 1000;
		 long ago = now - time;
		 if(ago <= ONE_HOUR){
			 return ago / ONE_MINUTE + "分钟前";
		 }
		 else if(ago <= ONE_DAY){
			 return ago / ONE_HOUR + "小时" + (ago % ONE_HOUR / ONE_MINUTE)+ "分钟前";
		 }
		 else if(ago <= ONE_DAY * 2){
			 return "昨天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"+ calendar.get(Calendar.MINUTE) + "分";
		 }
		 else if (ago <= ONE_DAY * 3){
			 return "前天" + calendar.get(Calendar.HOUR_OF_DAY) + "点" + calendar.get(Calendar.MINUTE) + "分";
		 }
		 else if (ago <= ONE_MONTH){
			 long day = ago / ONE_DAY;
			 if(day < 7)
	            return day + "天前" ;
			 else if (day >= 7 && day <14)
				 return "1周前";
			 else if (day >=14 && day <21)
				 return "2周前";
			 else if (day >= 21 && day <28)
				 return "3周前";
			 else 
				 return "4周前";
		 }
		 else if (ago <= ONE_YEAR){
			 long month = ago / ONE_MONTH;
	            long day = ago % ONE_MONTH / ONE_DAY;
	            return month + "个月" + day + "天前";
		 }
		 else {
	            long year = ago / ONE_YEAR;
	            int month = calendar.get(Calendar.MONTH) + 1;// JANUARY which is 0 so month+1
	            return year + "年前" + month + "月" + calendar.get(Calendar.DATE)
	                    + "日";
	     }
	 }

}
</span>