时间工具------SuperDateUtils

package com.xxxx.common.utils;

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

/**
 * 时间工具类
 * 
 * @author dyw
 * @date 2020年3月12日
 */
public class SuperDateUtils {
	/** 时间格式:2020 */
	public static final SimpleDateFormat year = new SimpleDateFormat("yyyy");
	/** 时间格式:2020-02 */
	public static final SimpleDateFormat year_mouth = new SimpleDateFormat("yyyy-MM");
	/** 时间格式:2020-02-02 */
	public static final SimpleDateFormat year_mouth_day = new SimpleDateFormat("yyyy-MM-dd");
	/** 时间格式:2020-02-02 02:02:02 */
	public static final SimpleDateFormat year_mouth_day_hms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/** 时间格式: 202002 */
	public static final SimpleDateFormat year_mouth_withoutseparater = new SimpleDateFormat("yyyyMM");
	/** 时间格式:20200202 */
	public static final SimpleDateFormat year_mouth_day_withoutseparater = new SimpleDateFormat("yyyyMMdd");
	/** 时间格式:20200202020202 */
	public static final SimpleDateFormat year_mouth_day_hms_withoutseparater = new SimpleDateFormat("yyyyMMddHHmmss");
	/** 时间格式: 2020年 */
	public static final SimpleDateFormat china_year = new SimpleDateFormat("yyyy年");
	/** 时间格式: 2020年02月 */
	public static final SimpleDateFormat china_year_mouth = new SimpleDateFormat("yyyy年MM月");
	/** 时间格式: 2020年02月02日 */
	public static final SimpleDateFormat china_year_mouth_day = new SimpleDateFormat("yyyy年MM月dd日");
	/** 时间格式: 2020年02月02日02时02分02秒 */
	public static final SimpleDateFormat china_year_mouth_day_hms = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
	/** 时间格式: 02月02日 */
	public static final SimpleDateFormat china_mouth_day = new SimpleDateFormat("MM月dd日");

	/**
	 * 获取yyyy格式日期字符串
	 * 
	 * @param date 日期
	 * @return 传入date为null时返回null,传入有效日期返回字符串格式为2020
	 */
	public static String getYear(Date date) {
		if (date != null) {
			return year.format(date);
		}
		return null;
	}

	/**
	 * 获取yyyy-MM格式日期字符串
	 * 
	 * @param date 日期
	 * @return 传入date为null时返回null,传入有效日期返回字符串格式为2020-02
	 */
	public static String getYM(Date date) {
		if (date != null) {
			return year_mouth.format(date);
		}
		return null;
	}

	/**
	 * 获取yyyy-MM-dd格式日期字符串
	 * 
	 * @param date 日期
	 * @return 传入date为null时返回null,传入有效日期返回字符串格式为2020-02-02
	 */
	public static String getYMD(Date date) {
		if (date != null) {
			return year_mouth_day.format(date);
		}
		return null;
	}

	/**
	 * 获取yyyy-MM-dd HH:mm:ss格式日期字符串
	 * 
	 * @param date 日期
	 * @return 传入date为null时返回null,传入有效日期返回字符串格式为2020-02-02 02:02:02
	 */
	public static String getYMDHMS(Date date) {
		if (date != null) {
			return year_mouth_day_hms.format(date);
		}
		return null;
	}

	/**
	 * 获取yyyyMMddHHmmss格式日期字符串
	 * 
	 * @param date 日期
	 * @return 传入date为null时返回null,传入有效日期返回字符串格式为20200202020202
	 */
	public static String getYMDHMSWithoutSeparater(Date date) {
		if (date != null) {
			return year_mouth_day_hms_withoutseparater.format(date);
		}
		return null;
	}

	/**
	 * 获得指定时间当天的开始时间
	 *
	 * @return 当天开始时间
	 */
	public static Date getDayBeginTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		date = cal.getTime();
		return date;
	}

	/**
	 * 获得指定时间当天的结束时间
	 *
	 * @return 当天结束时间
	 */
	public static Date getDayEndTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		date = cal.getTime();
		return date;
	}

	/**
	 * 获得指定时间当月的开始时间
	 *
	 * @return
	 */
	public static Date getMonthBeginTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		date = cal.getTime();
		return date;
	}

	/**
	 * 获得指定时间当月的结束时间
	 *
	 * @return
	 */
	public static Date getMonthEndTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		date = cal.getTime();
		return date;
	}

	/**
	 * 获取指定时间当季度的开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getQuarterBeginTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int quarter = getQuarter(date);
		switch (quarter) {
		case 1:
			setTime(cal, Calendar.JANUARY, 1, 0, 0, 0);
			break;
		case 2:
			setTime(cal, Calendar.APRIL, 1, 0, 0, 0);
			break;
		case 3:
			setTime(cal, Calendar.JULY, 1, 0, 0, 0);
			break;
		case 4:
			setTime(cal, Calendar.OCTOBER, 1, 0, 0, 0);
			break;
		default:
			break;
		}
		return cal.getTime();
	}

	/**
	 * 获取指定时间当季度的结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getQuarterEndTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int quarter = getQuarter(date);
		switch (quarter) {
		case 1:
			setTime(cal, Calendar.MARCH, 31, 23, 59, 59);
			break;
		case 2:
			setTime(cal, Calendar.JUNE, 30, 23, 59, 59);
			break;
		case 3:
			setTime(cal, Calendar.SEPTEMBER, 30, 23, 59, 59);
			break;
		case 4:
			setTime(cal, Calendar.DECEMBER, 31, 23, 59, 59);
			break;
		default:
			break;
		}
		return cal.getTime();
	}

	/**
	 * 获取指定时间当年的开始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getYearBeginTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		setTime(cal, Calendar.JANUARY, 1, 0, 0, 0);
		return cal.getTime();
	}

	/**
	 * 获取指定时间当年的结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getYearEndTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		setTime(cal, Calendar.DECEMBER, 31, 23, 59, 59);
		return cal.getTime();
	}

	/**
	 * 设置日历的月日
	 * 
	 * @param cal 日历对象
	 */
	private static void setTime(Calendar cal, int mouth, int day, int hour, int minute, int second) {
		cal.set(Calendar.MONTH, mouth);
		cal.set(Calendar.DAY_OF_MONTH, day);
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, minute);
		cal.set(Calendar.SECOND, second);
	}

	/**
	 * 
	 * 获取指定时间所在的季度:
	 * 
	 * 1(第一季度),2(第二季度),3(第三季度),4(第四季度)
	 * 
	 * @param date
	 * @return
	 */
	public static int getQuarter(Date date) {
		int quarter = 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:
			quarter = 1;
			break;
		case Calendar.APRIL:
		case Calendar.MAY:
		case Calendar.JUNE:
			quarter = 2;
			break;
		case Calendar.JULY:
		case Calendar.AUGUST:
		case Calendar.SEPTEMBER:
			quarter = 3;
			break;
		case Calendar.OCTOBER:
		case Calendar.NOVEMBER:
		case Calendar.DECEMBER:
			quarter = 4;
			break;
		default:
			break;
		}
		return quarter;
	}

	/**
	 * 获取年
	 * 
	 * @param date
	 * @return 2020年
	 */
	public static String getYearName(Date date) {
		return china_year.format(date);
	}

	/**
	 * 获取年列表
	 * 
	 * @param form
	 * @param to
	 * @return
	 */
	public static List<String> getYearNameList(Date form, Date to) {
		List<String> yearNameList = new ArrayList<>();
		Calendar formCal = Calendar.getInstance();
		formCal.setTime(form);
		setTime(formCal, Calendar.JANUARY, 1, 0, 0, 0);
		while (!formCal.getTime().after(to)) {
			String yearName = getYearName(formCal.getTime());
			if (!yearNameList.contains(yearName)) {
				yearNameList.add(yearName);
			}
			formCal.add(Calendar.YEAR, 1);
		}
		return yearNameList;
	}

	/**
	 * 获取月所在季度名
	 * 
	 * @param date
	 * @return 2020年1季度
	 */
	public static String getQuarterName(Date date) {
		String year = getYear(date);
		int quarter = getQuarter(date);
		StringBuffer sb = new StringBuffer();
		sb.append(year).append("年").append(quarter).append("季度");
		return sb.toString();
	}

	/**
	 * 获取季度列表
	 * 
	 * @param form 开始时间
	 * @param to   结束时间
	 * @return
	 */
	public static List<String> getQuarterNameList(Date form, Date to) {
		List<String> quarterNameList = new ArrayList<>();
		Calendar formCal = Calendar.getInstance();
		formCal.setTime(form);
		setTime(formCal, formCal.get(Calendar.MONTH), 1, 0, 0, 0);
		while (!formCal.getTime().after(to)) {
			String quarterName = getQuarterName(formCal.getTime());
			if (!quarterNameList.contains(quarterName)) {
				quarterNameList.add(quarterName);
			}
			formCal.add(Calendar.MONTH, 1);
		}
		return quarterNameList;
	}

	/**
	 * 获取月
	 * 
	 * @param date
	 * @return
	 */
	public static String getMouthName(Date date) {
		return china_year_mouth.format(date);
	}

	/**
	 * 获取月列表
	 * 
	 * @param form
	 * @param to
	 * @return
	 */
	public static List<String> getMouthNameList(Date form, Date to) {
		List<String> mouthNameList = new ArrayList<>();
		Calendar formCal = Calendar.getInstance();
		formCal.setTime(form);
		setTime(formCal, formCal.get(Calendar.MONTH), 1, 0, 0, 0);
		while (!formCal.getTime().after(to)) {
			String mouthName = getMouthName(formCal.getTime());
			mouthNameList.add(mouthName);
			formCal.add(Calendar.MONTH, 1);
		}
		return mouthNameList;
	}

	/**
	 * 获取日
	 * 
	 * @param date
	 * @return
	 */
	public static String getDayName(Date date) {
		return china_year_mouth_day.format(date);
	}

	/**
	 * 获取日列表
	 * 
	 * @param form
	 * @param to
	 * @return
	 */
	public static List<String> getDayNameList(Date form, Date to) {
		List<String> dayNameList = new ArrayList<>();
		Calendar formCal = Calendar.getInstance();
		formCal.setTime(form);
		setTime(formCal, formCal.get(Calendar.MONTH), formCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		while (!formCal.getTime().after(to)) {
			String dayName = getDayName(formCal.getTime());
			dayNameList.add(dayName);
			formCal.add(Calendar.DAY_OF_MONTH, 1);
		}
		return dayNameList;
	}

	/**
	 * 获取间隔天数
	 * 
	 * @param from
	 * @param to
	 * @return
	 * @throws ParseException
	 */
	public static int getDays(Date from, Date to) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(from);
		int cnt = 0;
		while (calendar.getTime().compareTo(to) != 0) {
			calendar.add(Calendar.DATE, 1);
			cnt++;
		}
		return cnt;
	}

	/**
	 * 增加指定天数后的时间
	 * 
	 * @param date        2020-03-01
	 * @param addDay      2
	 * @param chinesename false 2020-03-03,true 2019年03月03日
	 * 
	 * @return
	 */
	public static String addDays(Date date, Integer addDay, boolean chinesename) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, addDay);
		if (chinesename) {
			return china_year_mouth_day.format(calendar.getTime());
		}
		return year_mouth_day.format(calendar.getTime());
	}
}

你可能感兴趣的:(时间工具------SuperDateUtils)