积累的时间工具类

package dateUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.map.CaseInsensitiveMap;

public class dateUtil {

	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
    
    public static SimpleDateFormat sdftime = new SimpleDateFormat("HH:mm:ss");
    
    public static final String FULL_TIME_STRING="yyyy-MM-dd HH:mm:ss";
    
    public static final String TIME_STRING="HH:mm:ss";
    
    public static final String DAY_STRING="yyyy-MM-dd";
    
    public enum TIME_FORMAT_ENUM{
    	FULL_TIME_STRING,
    	TIME_STRING,
    	DAY_STRING
    }
    
	 /**
     * 获得yyyy-MM-dd HH:mm:ss格式日期字符串
     * 
     * @param date
     *            日期
     * @return
     * @author da.zhang
     * @date 2013-3-25 下午6:39:00
     */
    public static String formatDate(Date date) {
        if (date != null) {
            return sdf.format(date);
        }
        return "";
    }
    
    /**
     * 获得yyyy-MM-dd格式日期字符串
     * 
     * @param date
     *            日期
     * @return
     * @author da.zhang
     * @date 2013-4-25 下午3:39:00
     */
    public static String formatDate2(Date date) {
        if (date != null) {
            return sdfDay.format(date);
        }
        return "";
    }
    
    /**
     * 任意类型字符串转时间
     * TODO
     * @param thedate
     * @param format
     * @return
     * @throws java.text.ParseException
     * @return Date
     * @author min.zhang
     * @date: 2017年4月10日 上午11:03:56
     */
	public static Date parseStringToDate(String thedate, String format)
			throws java.text.ParseException {
		DateFormat sdf = new SimpleDateFormat(format);
		Date dd1 = null;
		if (thedate != null && !thedate.equals("")) {
			dd1 = sdf.parse(thedate);
		}
		return dd1;
	}
    
    
 	/** 
      * 得到指定日期的前后几天日期字符串,前后几小时,前后几秒的日期字符串
      * @param format  格式 
      * @return  字符串日期 
      */  
	public static String beforeAfterNDaysDateStr(Date dt, int type, Integer day,String format) {
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance();// 得到日历
		calendar.setTime(dt);// 把当前时间赋给日历
		calendar.add(type, day); // 设置为前后N天
		dBefore = calendar.getTime(); // 得到前一天的时间
		SimpleDateFormat sdf = new SimpleDateFormat(format); // 设置时间格式
		String timed = sdf.format(dBefore); // 格式化前一天
		return timed;
	}
	
	/**
	 * 
	 * TODO  获得指定日期的前后几天,前后几秒,前后几小时的日期
	 * @param dt
	 * @param type
	 * @param day
	 * @return
	 * @return Date
	 * @author min.zhang
	 * @date: 2017年4月10日 上午10:54:09
	 */
	public static Date beforeAfterNDaysDate(Date dt, int type, Integer day) {
		Calendar calendar = Calendar.getInstance();// 得到日历
		calendar.setTime(dt);// 把当前时间赋给日历
		calendar.add(type, day); // 设置为前后N天
		return calendar.getTime();
	}
	
	/**
	 * 获得两个字符串日期相差几天--后一个字符日期比前一个字符日期大几天
	 * TODO
	 * @param smdate
	 * @param bdate
	 * @return
	 * @throws ParseException
	 * @throws java.text.ParseException
	 * @return int
	 * @author min.zhang
	 * @date: 2017年4月10日 上午10:57:49
	 */
	public static int daysBetween(String smdate, String bdate)
			throws ParseException, java.text.ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(DAY_STRING);
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}
	
	/**
	 * 前一个日期和后一个日期比较年月日时分秒的大小,相等返回0,小于返回-1,大于返回1
	 * TODO
	 * @param date
	 * @param otherDate
	 * @param withUnit
	 * @return
	 * @return int
	 * @author min.zhang
	 * @date: 2017年4月10日 上午11:51:59
	 */
	public static int compareDate(Date date, Date otherDate, int withUnit) {
		Calendar dateCal = Calendar.getInstance();
		dateCal.setTime(date);
        Calendar otherDateCal = Calendar.getInstance();
        otherDateCal.setTime(otherDate);
        
        switch (withUnit) {
        case Calendar.YEAR:
        	dateCal.clear(Calendar.MONTH);
        	otherDateCal.clear(Calendar.MONTH);
        case Calendar.MONTH:
        	dateCal.set(Calendar.DATE, 1);
        	otherDateCal.set(Calendar.DATE, 1);
        case Calendar.DATE:
        	dateCal.set(Calendar.HOUR_OF_DAY, 0);
        	otherDateCal.set(Calendar.HOUR_OF_DAY, 0);
        case Calendar.HOUR:
        	dateCal.clear(Calendar.MINUTE);
        	otherDateCal.clear(Calendar.MINUTE);
        case Calendar.MINUTE:
        	dateCal.clear(Calendar.SECOND);
        	otherDateCal.clear(Calendar.SECOND);
        case Calendar.SECOND:
        	dateCal.clear(Calendar.MILLISECOND);
        	otherDateCal.clear(Calendar.MILLISECOND);
        case Calendar.MILLISECOND:
			break;
		default: throw new IllegalArgumentException("withUnit 单位字段 " + withUnit + " 不合法!!");
		}
		return dateCal.compareTo(otherDateCal);
	}
	
	/**
	 * 得到当月的最后一天
	 * TODO
	 * @param dt
	 * @return
	 * @return Date
	 * @author min.zhang
	 * @date: 2017年4月10日 上午11:01:44
	 */
	public static Date getLastDayOfMonth(Date dt) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dt);// 把当前时间赋给日历
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
		calendar.set(Calendar.DAY_OF_MONTH,
				calendar.getActualMaximum(Calendar.DATE));
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		return calendar.getTime();
	}
	
	/**
	 * 时间戳转时间
	 * TODO
	 * @param time
	 * @return
	 * @return Date
	 * @author min.zhang
	 * @date: 2017年4月10日 上午11:36:58
	 */
	public static Date timeStampToDate(String time) {
        long lt = new Long(time);
        Date date = new Date(lt);
        return date;
	}
	
	/**
	 * 时间戳转时间字符串
	 * TODO
	 * @param time
	 * @return
	 * @return String
	 * @author min.zhang
	 * @date: 2017年4月10日 上午11:39:48
	 */
	public static String timeStampToDateStr(String time) {
		long lt = new Long(time);
        Date date = new Date(lt);
        String str=formatDate(date);
        return str;
	}
	
	/**
	 * 字符串日期转时间戳
	 * TODO
	 * @param s
	 * @return
	 * @throws ParseException
	 * @return String
	 * @author min.zhang
	 * @date: 2017年4月10日 上午11:40:20
	 */
	public static String dateToStamp(String s,String format) throws ParseException{
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }
	
	/**
     * 判断是否为同一天
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        boolean isSameMonth = isSameYear && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }
    
    /**
     * 开始和结束之间的时间列表,要求第一个日期小于第二个日期
     * @param startDate
     * @param endDate
     * @return
     * @throws Exception
     */
    public static List<Date> getCheckDateList(Date startDate,Date endDate) {

        int period =compareDate(endDate,startDate, Calendar.DATE);
        if (period < 0){
            return null;
        }
        List<Date> list = new ArrayList<Date>();
        long l = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
        for (int i=0;i<=l;i++){
            list.add(beforeAfterNDaysDate(startDate,Calendar.DAY_OF_MONTH,i));
        }
        return list;
    }
    
    /**
     * 判断这个时间是否在开始时间和结束时间之内
     * TODO
     * @param now
     * @param start
     * @param end
     * @param model
     * @return
     * @return boolean
     * @author min.zhang
     * @date: 2017年4月10日 下午12:04:53
     */
    public static boolean isBetween(Date now, Date start, Date end) {

    	if (now == null || start == null || end == null)
    		throw new IllegalArgumentException("The scale must be a positive integer or zero");
    	if (now.after(start) && now.before(end))
    			return true;
    	return false;
    }
    
    /**当前日期是第几季度
     * 
     * TODO
     * @param date
     * @return
     * @return int
     * @author min.zhang
     * @date: 2017年4月10日 下午12:08:40
     */
    public static int getSeason(Date date) {

		int season = 0;

		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH);
		switch (month) {
		case Calendar.JANUARY:
		case Calendar.FEBRUARY:
		case Calendar.MARCH:
			season = 1;
			break;
		case Calendar.APRIL:
		case Calendar.MAY:
		case Calendar.JUNE:
			season = 2;
			break;
		case Calendar.JULY:
		case Calendar.AUGUST:
		case Calendar.SEPTEMBER:
			season = 3;
			break;
		case Calendar.OCTOBER:
		case Calendar.NOVEMBER:
		case Calendar.DECEMBER:
			season = 4;
			break;
		default:
			break;
		}
		return season;
	}
    


    public static void main(String[] args) throws ParseException {
    	System.out.println(formatDate(new Date()));
    	System.out.println(formatDate2(new Date()));
    	System.out.println(parseStringToDate("2017-09-09 12:00:00",FULL_TIME_STRING));
    	System.out.println(beforeAfterNDaysDateStr(new Date(),Calendar.MINUTE,1,FULL_TIME_STRING));
    	System.out.println(beforeAfterNDaysDate(new Date(),Calendar.DAY_OF_MONTH,1));
    	System.out.println(daysBetween("2017-09-09","2017-09-10"));
    	System.out.println(getLastDayOfMonth(new Date()));
    	System.out.println(dateToStamp("2017-01-01",DAY_STRING));
    	
    	String timeString="1483200000000";
    	System.out.println(timeStampToDate(timeString));
    	System.out.println(timeStampToDateStr(timeString));
    	System.out.println(isSameDate(new Date(),new Date()));
    	
    	Date startDate=parseStringToDate("2017-04-01",DAY_STRING);
    	Date otherDate=parseStringToDate("2017-04-11",DAY_STRING);
    	System.out.println(compareDate(new Date(),otherDate,Calendar.DATE));
    	
    	System.out.println(getCheckDateList(new Date(),otherDate));
    	
    	System.out.println(isBetween(new Date(),startDate,otherDate));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值