zoukankan      html  css  js  c++  java
  • 轮子:DateUtil.java

    日期工具类

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateUtil {
    	
    	public static final String DATE = "yyyy/MM/dd";
    	
    	public static final String Date = "yyyy-MM-dd";
    	
    	public static final String TIME = "HH:mm:ss";
    	
    	public static final String DATE_TIME = "yyyy/MM/dd HH:mm:ss";
    	
    	public static final String Date_TIME = "yyyy-MM-dd HH:mm:ss";
    	
    	public static final String DateTime = "yyyyMMddHHmmss";
    	
    	/**
    	 * 自定义时间格式化
    	 * @param date
    	 * @param simpleDateFormat
    	 * @return
    	 */
    	public static String customFormat(Date date, String simpleDateFormat){
    		SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormat);
    		return sdf.format(date);
    	}
    	
    	/**
    	 * 获取当前时间  yyyy-MM-dd HH:mm:ss
    	 * @return 字符类型时间
    	 */
    	public static String nowFormatStr(){
    		return customFormat(new Date(), Date_TIME);
    	}
    	
    	/**
    	 * 获取当前时间  yyyyMMddHHmmss
    	 * @return 字符类型时间
    	 */
    	public static String nowFormatStrTwo(){
    		return customFormat(new Date(), DateTime);
    	}
    	
    	/**
    	 * 判断日期大小
    	 * 前者小 返回-1
    	 * 前者大 返回1
    	 * 相等 返回0
    	 * @param date1
    	 * @param date2
    	 * @return
    	 */
    	public static int compareTo(Date date1, Date date2){
    		return date1.compareTo(date2);
    	}
    	
    	/**
    	 * 判断与当前时间戳的相差的毫秒数
    	 * @return
    	 */
    	public static long timestampDifferByNow(long timestamp){
    		long millis = System.currentTimeMillis();
    		return Math.abs(timestamp-millis);
    	}
    	
    	/**
    	 * 判断指定的时间戳与当前时间是否超时
    	 * @param timestamp
    	 * @param second 在有效期内的秒数
    	 * @return
    	 */
    	public static boolean judgeTimeOutBySecond(long timestamp, long second){
    		long differ = timestampDifferByNow(timestamp);
    		return differ/1000 >= second;
    	}
    	
    	/**
    	 * 判断指定的时间戳与当前时间是否超时
    	 * @param timestamp
    	 * @param MS 毫秒
    	 * @return
    	 */
    	public static boolean judgeTimeOutByMS(long timestamp, long MS){
    		long differ = timestampDifferByNow(timestamp);
    		return differ >= MS;
    	}
    	
    	/**
    	 * 获取两个Date之间相差的天数
    	 * @param begintime
    	 * @param endtime
    	 * @return
    	 */
    	public static Double getDaysDiffer(Date begintime, Date endtime) {
    		long time01 = begintime.getTime();
    		long time02 = endtime.getTime();
    		long diff = Math.abs(time01 - time02)/1000;
    		return diff / 86400.0;
    	}
    	
    	/**
    	 * 获取两个Date之间相差的天数(向上取整)
    	 * @param begintime
    	 * @param endtime
    	 * @return
    	 */
    	public static Integer getDaysCeilDiffer(Date begintime, Date endtime) {
    		Double days = getDaysDiffer(begintime, endtime);
    		return Double.valueOf(Math.ceil(days)).intValue();
    	}
    	
    	/**
    	 * 获取两个Date之间相差的天数(向下取整)
    	 * @param begintime
    	 * @param endtime
    	 * @return
    	 */
    	public static Integer getDaysFloorDiffer(Date begintime, Date endtime) {
    		Double days = getDaysDiffer(begintime, endtime);
    		return Double.valueOf(Math.floor(days)).intValue();
    	}
    	
    }
    

    记录快速开发所需的轮子,保持更新中

    入我相思门,知我相思苦, 长相思兮长相忆,短相思兮无穷极, 早知如此绊人心,何如当初莫相识。
  • 相关阅读:
    黑马程序员_字符串常用处理方法
    动软代码生成器,主子表增加的时候子表的parentID无法插入问题解决方案
    大数据量高并发的数据库优化详解
    C# Socket网络编程精华篇 (转)
    html+javascript+soap获取webservice免费天气预报信息
    js中字符串怎么转化为日期
    attachEvent方法的作用
    C#中[WebMethod]的用法,aspx、ashx、asmx
    C#操作XML方法详解
    C#操作XML的通用方法总结
  • 原文地址:https://www.cnblogs.com/banmoon/p/13356350.html
Copyright © 2011-2022 走看看