自定义date工具类 DateUtils.java

自定义date工具类 DateUtils.java

简介

  • Date日期类型的工具类。

api

  • 日期格式化 format(Date date);
  • 日期格式化 format(Date date, String pattern);
  • 计算距离现在多久,非精确 getTimeBefore(Date date);
  • 计算距离现在多久,精确 getTimeBeforeNow(Date date);

源码

  • import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    /**
     * @说明:Date工具类
     */
    public class DateUtils {
    	
    	public final static String format_1 = "yyyy-MM-dd";
    	public final static String format_2 = "yyyy-MM-dd HH:mm:ss";
    	
    	public static void main(String[] args) throws ParseException {
    		System.err.println(format(new Date()));
    		System.out.println(getTimeBeforeNow(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-08-16 12:17:43")));
    		System.out.println(getTimeBefore(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-08-16 12:17:43")));
    	}
    	
    	public static String format(Date date) {
            return format(date, format_1);
        }
     
        public static String format(Date date, String pattern) {
            if (date != null) {
                SimpleDateFormat df = new SimpleDateFormat(pattern);
                return df.format(date);
            }
            return null;
        }
        
        /**
         * 方法功能说明:@1.计算距离现在多久,非精确
         * "126天前"
         */
        public static String getTimeBefore(Date date) {
            Date now = new Date();
            long l = now.getTime() - date.getTime();
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            String r = "";
            if (day > 0) {
                r += day + "天";
            } else if (hour > 0) {
                r += hour + "小时";
            } else if (min > 0) {
                r += min + "分";
            } else if (s > 0) {
                r += s + "秒";
            }
            r += "前";
            return r;
        }
        
        /**
         * 方法功能说明:@1.计算距离现在多久,精确
         * "126天10小时27分28秒前"
         */
        public static String getTimeBeforeNow(Date date) {
            Date now = new Date();
            long l = now.getTime() - date.getTime();
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            String r = "";
            if (day > 0) {
                r += day + "天";
            }
            if (hour > 0) {
                r += hour + "小时";
            }
            if (min > 0) {
                r += min + "分";
            }
            if (s > 0) {
                r += s + "秒";
            }
            r += "前";
            return r;
        }
     
    }
    

你可能感兴趣的:(编程学习,JAVA基础工作中实际总结,java,python,开发语言)