时间工具类

  1. 把long型日期转String    longToString(long date, String format)

  2. 把String型日期转Date   stringToDate(String date, String format)

  3. 把String 日期转换成long型日期   stringToLong(String date, String format)

  4. date 可能是 yyyy/MM/dd  也可能是 yyyyMMdd  也 可能是 yyyy-MM-dd 等转为时间戳  reverse2Long(String date)

  5. 获取指定时间是星期几   getDayOfWeek(String date)

  6. 获取指定时间的年份 2020   getYear(Long time)

  7. 获取指定时间的月份 02  getMonth(Long time)

  8. 获取指定时间的天 20  getDay(Long time)

  9. 获取今天0点开始的秒数    1613664000,  2021-02-19 00:00:00  getTimeNumberToday() 

  10. 获取指定时间的日期 yyyyMMdd  getYyyyMMddString(Long time) 

  11. 获取当前时间往前,或往后几天零点的时间    往前参数值为负数, 往后参数值为正数    getDayZeroHour(int day)

  12. 将time对应的时间,转化为下一天0点对应的时间   parseNextZero(long time)

  13. 将time对应的时间,转化为该天0点对应的时间    parseZero(long time)

  14. 获取当前时间往前几天23:59:59 的时间或者往后几天23:59:59的时间   getDay24Hour(int day)

  15. 获得指定日期所在的自然周的第一天,即周日  getStartDayOfWeek(Date date)

  16. 获得指定日期所在的自然周的最后一天,即周六 getLastDayOfWeek(Date date)

  17. 获得指定日期所在当月第一天  getStartDayOfMonth(Date date) 

  18. 获得指定日期所在当月第n天 getNextDayOfMonth(Date date, int offset) 

  19. 获得指定日期所在当月最后一天  getLastDayOfMonth(Date date)

  20. 获得指定日期的下一个月的第一天 getStartDayOfNextMonth(Date date)

  21. 获得指定日期的下一个月的最后一天  getLastDayOfNextMonth(Date date)

  22. 求某一个时间向前多少秒的时间(currentTimeToBefer)  givedTimeToBefore(String givedTime, long interval, String format_Date_Sign)

  23. 求某一个时间向后或者向前()多少秒的时间(currentTimeToBefer)  givedTimeToBeforeOrAfter(String givedTime, long interval, String formatDateSign)

  24. 将代表日期的字符串分割为代表年月日的整形数组 splitYMD(String date)

  25. 检查传入的参数代表的年份是否为闰年   isLeapYear(int year)

  26. 日期加1天    addOneDay(int year, int month, int day)

  27.  将不足两位的月份或日期补足为两位   formatMonthDay(int decimal)

  28. 将不足四位的年份补足为四位    formatYear(int decimal)

  29. 计算两个日期之间相隔的天数    countDay(String begin, String end) 

  30. 获取两个时间之间的日期         getEveryday(String beginDate, String endDate)

  31. 根据某天获取星期几    getWeek(String date)

  32. 获取某月所有日期     getDaysOfMonth(long date)

  33.  获取某年某月的开始时间 结束时间  calcDate(int year, int month)

  34.  获取指定时间向前或前后的时间 格式为20200218   getAfterDate(long time, int dayCnt)

  35. 获取给定时间戳当天的结束时间戳  getDayEndMs(long timeMillis)

  36. 获取给定时间戳当天的开始时间戳   getDayStartMs(long timeMillis)

  37. 获取当前时间的Hour    getCurrentHour()

  38. 获取指定时间所在周周一     getMonday(String time)

  39. 获取指定时间所在周周日    getSunday(String time)

  40. 获取指定时间段内月份  getMonthBetween(String minDate, String maxDate)

  41. 获取某年某月共有多少天   getMaxDate(int year, int month)

  42. 判断是否是今天  isToday(String date)

  • 具体实现代码如下:
  1. 
    
    import java.text.DecimalFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    
    public class DateUtils {
    	private static String ymdhms = "yyyy-MM-dd HH:mm:ss";
    
    	private static String ymd = "yyyy-MM-dd";
    
    	private static String year = "yyyy";
    
    	private static String month = "MM";
    
    	private static String day = "dd";
    
    	public static SimpleDateFormat yearSDF = new SimpleDateFormat(year);
    
    	public static SimpleDateFormat monthSDF = new SimpleDateFormat(month);
    
    	public static SimpleDateFormat daySDF = new SimpleDateFormat(day);
    
    	public static SimpleDateFormat yyyyMMddHHmm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    
    	public static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
    
    	private static transient int gregorianCutoverYear = 1582;
    
    	/**
    	 * 闰年中每月天数
    	 */
    	private static final int[] DAYS_P_MONTH_LY = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    	/**
    	 * 非闰年中每月天数
    	 */
    	private static final int[] DAYS_P_MONTH_CY = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    	/**
    	 * 代表数组里的年、月、日
    	 */
    	private static final int Y = 0, M = 1, D = 2;
    
    	/**
    	 * 把long型日期转String ;
    	 *
    	 * @param date   long型日期;时间戳
    	 * @param format 日期格式;要转换的日期格式
    	 * @return
    	 */
    	public static String longToString(long date, String format) {
    		SimpleDateFormat sdf = new SimpleDateFormat(format);
    		Date dt2 = new Date(date);
    		String sDateTime = sdf.format(dt2);
    		return sDateTime;
    	}
    
    	/**
    	 * String To Date ---OK
    	 *
    	 * @param date   待转换的字符串型日期;
    	 * @param format 转化的日期格式
    	 * @return 返回该字符串的日期型数据;
    	 */
    	public static Date stringToDate(String date, String format) {
    		SimpleDateFormat sdf = new SimpleDateFormat(format);
    		try {
    			return sdf.parse(date);
    		} catch (ParseException e) {
    			return null;
    		}
    	}
    
    	/**
    	 * 把String 日期转换成long型日期;---OK
    	 *
    	 * @param date   String 型日期; 2021-02-09
    	 * @param format 日期格式;         yyyy-MM-dd
    	 * @return 1613664000000  毫秒值
    	 */
    	public static long stringToLong(String date, String format) {
    		SimpleDateFormat sdf = new SimpleDateFormat(format);
    		Date dt2 = null;
    		long lTime = 0;
    		try {
    			dt2 = sdf.parse(date);
    			// 继续转换得到毫秒数的long型
    			lTime = dt2.getTime();
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    
    		return lTime;
    	}
    
    	/**
    	 * 方法描述:
    	 *
    	 * @param date 可能是 yyyy/MM/dd  也可能是 yyyyMMdd  也 可能是 yyyy-MM-dd 等
    	 * @return 返回时间戳  1612800000000
    	 */
    	public static Long reverse2Long(String date) {
    		Long result = null;
    		try {
    			if (null != date && "" != date) {
    				if (date.length() == 8) {
    					result = new SimpleDateFormat("yyyyMMdd").parse(date).getTime();
    				} else if (date.length() == 10) {
    					if (date.indexOf("/") != -1) {
    						result = new SimpleDateFormat("yyyy/MM/dd").parse(date).getTime();
    					} else if (date.indexOf("-") != -1) {
    						result = new SimpleDateFormat("yyyy-MM-dd").parse(date).getTime();
    					}
    				}
    			}
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    		return result;
    	}
    
    
    	/**
    	 * 方法描述:获取指定时间是星期几
    	 *
    	 * @param date "2017-11-15";
    	 * @return 返回1是星期日、2是星期一、3是星期二、4是星期三、5是星期四、6是星期五、7是星期六
    	 */
    	public static int getDayOfWeek(String date) {
    		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
    		Date d = null;
    		Calendar c = null;
    		try {
    			d = sd.parse(date);
    			c = Calendar.getInstance();
    			c.setTime(d);
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    		return c.get(Calendar.DAY_OF_WEEK);
    	}
    
    	/**
    	 * 获取指定时间的年份 2020
    	 * @param time 时间戳
    	 * @return
    	 */
    	public static String getYear(Long time) {
    		return yearSDF.format(new Date(time));
    	}
    
    	/**
    	 * 获取指定时间的月份 02
    	 *
    	 * @param time
    	 * @return
    	 */
    	public static String getMonth(Long time) {
    		return monthSDF.format(new Date(time));
    	}
    
    	/**
    	 * 获取指定时间的天 20
    	 *
    	 * @param time
    	 * @return
    	 */
    	public static String getDay(Long time) {
    		return daySDF.format(new Date(time));
    	}
    
    
    	/**
    	 * 获取今天0点开始的秒数    1613664000,  2021-02-19 00:00:00
    	 *
    	 * @return long
    	 */
    	public static long getTimeNumberToday() {
    		Date date = new Date();
    		String str = yyyyMMdd.format(date);
    		try {
    			date = yyyyMMdd.parse(str);
    			return date.getTime() / 1000L;
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    		return 0L;
    	}
    
    
    	/**
    	 * 获取指定时间的日期 yyyyMMdd
    	 *
    	 * @param time 时间戳
    	 * @return 20210201
    	 */
    	public static String getYyyyMMddString(Long time) {
    		SimpleDateFormat yyyyMMddHH_NOT_ = new SimpleDateFormat("yyyyMMdd");
    		String str = yyyyMMddHH_NOT_.format(new Date(time));
    		return str;
    	}
    
    	/**
    	 * 获取当前时间往前,或往后几天零点的时间    往前参数值为负数, 往后参数值为整数
    	 *
    	 * @param day
    	 * @return
    	 */
    	public static Date getDayZeroHour(int day) {
    		Calendar cal = Calendar.getInstance();
    		cal.add(Calendar.DATE, day);
    		cal.set(Calendar.SECOND, 0);
    		cal.set(Calendar.MINUTE, 0);
    		cal.set(Calendar.HOUR_OF_DAY, 0);
    		return cal.getTime();
    	}
    
    	/**
    	 * 将time对应的时间,转化为下一天0点对应的时间
    	 *
    	 * @param time
    	 * @return
    	 */
    	public static long parseNextZero(long time) {
    		/*1.获取一天后的时间点*/
    		long nextTime = time + 24 * 3600 * 1000;
    		/*2.计算增加一天后对应的0点的整数*/
    		return parseZero(nextTime);
    	}
    
    	/**
    	 * 将time对应的时间,转化为该天0点对应的时间
    	 *
    	 * @param time
    	 * @return
    	 */
    	public static long parseZero(long time) {
    		/*1.将整型格式化到日期的字符串*/
    		Date date = new Date(time);
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    		/*2.再将字符串转化会日期整数*/
    		String datString = sdf.format(date);
    		Date zeroDate = null;
    		try {
    			zeroDate = sdf.parse(datString);
    		} catch (ParseException e) {
    			//log.error("时间格式转换错误,原样返回原有时间:" + time + ", {}",  e);
    			return time;
    		}
    		return zeroDate.getTime();
    	}
    
    	/**
    	 * 获取当前时间往前几天23:59:59 的时间或者往后几天23:59:59的时间
    	 *
    	 * @return
    	 */
    	public static Date getDay24Hour(int day) {
    		Calendar cal = Calendar.getInstance();
    		cal.add(Calendar.DATE, day);
    		cal.set(Calendar.SECOND, 59);
    		cal.set(Calendar.MINUTE, 59);
    		cal.set(Calendar.HOUR_OF_DAY, 23);
    		return cal.getTime();
    	}
    
    
    	/**
    	 * 获得指定日期所在的自然周的第一天,即周日
    	 *
    	 * @param date 日期
    	 * @return 自然周的第一天
    	 */
    	public static Date getStartDayOfWeek(Date date) {
    		Calendar c = Calendar.getInstance();
    		c.setTime(date);
    		c.set(Calendar.DAY_OF_WEEK, 1);
    		date = c.getTime();
    		return date;
    	}
    
    	/**
    	 * 获得指定日期所在的自然周的最后一天,即周六
    	 *
    	 * @param date
    	 * @return
    	 */
    	public static Date getLastDayOfWeek(Date date) {
    		Calendar c = Calendar.getInstance();
    		c.setTime(date);
    		c.set(Calendar.DAY_OF_WEEK, 7);
    		date = c.getTime();
    		return date;
    	}
    
    	/**
    	 * 获得指定日期所在当月第一天
    	 *
    	 * @param date
    	 * @return
    	 */
    	public static Date getStartDayOfMonth(Date date) {
    		Calendar c = Calendar.getInstance();
    		c.setTime(date);
    		c.set(Calendar.DAY_OF_MONTH, 1);
    		date = c.getTime();
    		return date;
    	}
    
    	/**
    	 * 获得指定日期所在当月第n天
    	 *
    	 * @param date
    	 * @return
    	 */
    	public static Date getNextDayOfMonth(Date date, int offset) {
    		Calendar c = Calendar.getInstance();
    		c.setTime(date);
    		c.set(Calendar.DAY_OF_MONTH, offset);
    		date = c.getTime();
    		return date;
    	}
    
    	/**
    	 * 获得指定日期所在当月最后一天
    	 *
    	 * @param date
    	 * @return
    	 */
    	public static Date getLastDayOfMonth(Date date) {
    		Calendar c = Calendar.getInstance();
    		c.setTime(date);
    		c.set(Calendar.DATE, 1);
    		c.add(Calendar.MONTH, 1);
    		c.add(Calendar.DATE, -1);
    		date = c.getTime();
    		return date;
    	}
    
    	/**
    	 * 获得指定日期的下一个月的第一天
    	 *
    	 * @param date
    	 * @return
    	 */
    	public static Date getStartDayOfNextMonth(Date date) {
    		Calendar c = Calendar.getInstance();
    		c.setTime(date);
    		c.add(Calendar.MONTH, 1);
    		c.set(Calendar.DAY_OF_MONTH, 1);
    		date = c.getTime();
    		return date;
    	}
    
    	/**
    	 * 获得指定日期的下一个月的最后一天
    	 *
    	 * @param date
    	 * @return
    	 */
    	public static Date getLastDayOfNextMonth(Date date) {
    		Calendar c = Calendar.getInstance();
    		c.setTime(date);
    		c.set(Calendar.DATE, 1);
    		c.add(Calendar.MONTH, 2);
    		c.add(Calendar.DATE, -1);
    		date = c.getTime();
    		return date;
    	}
    
    	/**
    	 * 求某一个时间向前多少秒的时间(currentTimeToBefer)---OK
    	 *
    	 * @param givedTime        给定的时间
    	 * @param interval         间隔时间的秒数;计算方式 :n(天)*24(小时)*60(分钟)*60(秒)(类型)
    	 * @param format_Date_Sign 输出日期的格式;如yyyy-MM-dd、yyyyMMdd等;
    	 */
    	public static String givedTimeToBefore(String givedTime, long interval, String format_Date_Sign) {
    		String tomorrow = null;
    		try {
    			SimpleDateFormat sdf = new SimpleDateFormat(format_Date_Sign);
    			Date gDate = sdf.parse(givedTime);
    			long current = gDate.getTime(); // 将Calendar表示的时间转换成毫秒
    			long beforeOrAfter = current - interval * 1000L; // 将Calendar表示的时间转换成毫秒
    			Date date = new Date(beforeOrAfter); // 用timeTwo作参数构造date2
    			tomorrow = new SimpleDateFormat(format_Date_Sign).format(date);
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    		return tomorrow;
    	}
    
    	/**
    	 * 求某一个时间向后或者向前()多少秒的时间(currentTimeToBefer)---OK
    	 *
    	 * @param givedTime      给定的时间
    	 * @param interval       间隔时间的毫秒数;计算方式 :n(天)*24(小时)*60(分钟)*60(秒)(类型) 负值 - 向前  vs  正值-向后
    	 * @param formatDateSign 输出日期的格式;如yyyy-MM-dd、yyyyMMdd等;
    	 */
    	public static Date givedTimeToBeforeOrAfter(String givedTime, long interval, String formatDateSign) {
    		try {
    			SimpleDateFormat sdf = new SimpleDateFormat(formatDateSign);
    			Date gDate = sdf.parse(givedTime);
    			long current = gDate.getTime(); // 将Calendar表示的时间转换成毫秒
    			long beforeOrAfter = current + interval * 1000L; // 将Calendar表示的时间转换成毫秒
    			Date date = new Date(beforeOrAfter); // 用timeTwo作参数构造date2
    			return date;
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    
    	/**
    	 * 将代表日期的字符串分割为代表年月日的整形数组
    	 *
    	 * @param date
    	 * @return
    	 */
    	public static int[] splitYMD(String date) {
    		date = date.replace("-", "");
    		int[] ymd = {0, 0, 0};
    		ymd[Y] = Integer.parseInt(date.substring(0, 4));
    		ymd[M] = Integer.parseInt(date.substring(4, 6));
    		ymd[D] = Integer.parseInt(date.substring(6, 8));
    		return ymd;
    	}
    
    	/**
    	 * 检查传入的参数代表的年份是否为闰年
    	 *
    	 * @param year
    	 * @return
    	 */
    	public static boolean isLeapYear(int year) {
    		return year >= gregorianCutoverYear ?
    				((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) : (year % 4 == 0);
    	}
    
    	/**
    	 * 日期加1天
    	 *
    	 * @param year
    	 * @param month
    	 * @param day
    	 * @return
    	 */
    	private static int[] addOneDay(int year, int month, int day) {
    		if (isLeapYear(year)) {
    			day++;
    			if (day > DAYS_P_MONTH_LY[month - 1]) {
    				month++;
    				if (month > 12) {
    					year++;
    					month = 1;
    				}
    				day = 1;
    			}
    		} else {
    			day++;
    			if (day > DAYS_P_MONTH_CY[month - 1]) {
    				month++;
    				if (month > 12) {
    					year++;
    					month = 1;
    				}
    				day = 1;
    			}
    		}
    		int[] ymd = {year, month, day};
    		return ymd;
    	}
    
    	/**
    	 * 将不足两位的月份或日期补足为两位
    	 *
    	 * @param decimal
    	 * @return
    	 */
    	public static String formatMonthDay(int decimal) {
    		DecimalFormat df = new DecimalFormat("00");
    		return df.format(decimal);
    	}
    
    	/**
    	 * 将不足四位的年份补足为四位
    	 *
    	 * @param decimal
    	 * @return
    	 */
    	public static String formatYear(int decimal) {
    		DecimalFormat df = new DecimalFormat("0000");
    		return df.format(decimal);
    	}
    
    	/**
    	 * 计算两个日期之间相隔的天数
    	 *
    	 * @param begin "2020-02-19"
    	 * @param end   "2021-02-19"
    	 * @return
    	 * @throws ParseException
    	 */
    	public static long countDay(String begin, String end) {
    		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    		Date beginDate, endDate;
    		long day = 0;
    		try {
    			beginDate = format.parse(begin);
    			endDate = format.parse(end);
    			day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    		return day;
    	}
    
    	/**
    	 * 以循环的方式计算日期
    	 *
    	 * @param beginDate 2020-02-19
    	 * @param endDate   2020-02-21
    	 * @return [2020-02-19, 2020-02-20, 2020-02-21]
    	 */
    	public static List<String> getEveryday(String beginDate, String endDate) {
    		long days = countDay(beginDate, endDate);
    		int[] ymd = splitYMD(beginDate);
    		List<String> everyDays = new ArrayList<String>();
    		everyDays.add(beginDate);
    		for (int i = 0; i < days; i++) {
    			ymd = addOneDay(ymd[Y], ymd[M], ymd[D]);
    			everyDays.add(formatYear(ymd[Y]) + "-" + formatMonthDay(ymd[M]) + "-" + formatMonthDay(ymd[D]));
    		}
    		return everyDays;
    	}
    
    	/**
    	 * 根据某天获取星期几
    	 *
    	 * @param date yyyy-MM-dd    2021-02-19
    	 * @return 星期几
    	 */
    	public static String getWeek(String date) {
    		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
    		SimpleDateFormat sdw = new SimpleDateFormat("E");
    		Date d = null;
    		try {
    			d = sd.parse(date);
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    		return sdw.format(d);
    	}
    
    
    	/**
    	 * 获取某月所有日期
    	 *
    	 * @param date 年月时间戳
    	 * @return 如果是当月,返回日期列表截止到昨天,如果不是当月,返回所有日期
    	 */
    	public static List<Long> getDaysOfMonth(long date) {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
    		String format = sdf.format(date);
    		int year = Integer.parseInt(format.substring(0, 4));
    		int month = Integer.parseInt(format.substring(4, 6));
    		Calendar cal = Calendar.getInstance();
    		List<Long> days = new ArrayList<Long>();
    		if (month == cal.get(Calendar.MONTH) + 1) {
    			//如果是当前月,只返回截止昨天的日期
    			int today = cal.get(Calendar.DAY_OF_MONTH);
    			days.add(parseZero(cal.getTimeInMillis()));
    			for (int i = 1; i < today; i++) {
    				cal.add(Calendar.DATE, -1);
    				days.add(parseZero(cal.getTimeInMillis()));
    			}
    			return days;
    		}
    		//如果是以前的月,返回所有日期
    		cal.set(year, month - 1, 1);
    		while (cal.get(Calendar.YEAR) == year && cal.get(Calendar.MONTH) < month) {
    			days.add(parseZero(cal.getTimeInMillis()));
    			cal.add(Calendar.DATE, 1);
    		}
    		return days;
    	}
        /**
         * 获取某年某月的开始时间 结束时间
         * @param year   2020
         * @param month   2
         * @Return       20200201#20200229
         * @Exception
         *
         */
    	public static String calcDate(int year, int month) {
    		Calendar cal = Calendar.getInstance();
    		cal.set(year, month - 1, 1);
    		Date start = cal.getTime();
    		cal.add(Calendar.MONTH, 1);
    		cal.add(Calendar.DATE, -1);
    		Date end = cal.getTime();
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    		return sdf.format(start) + "#" + sdf.format(end);
    	}
    	/**
    	 * 获取指定时间向前或前后的时间 格式为20200218
    	 * @param time  时间戳
    	 * @param dayCnt  向后  正数  向前  负数
    	 * @Return      20200218
    	 * @Exception
    	 *
    	 */
    	public static int getAfterDate(long time, int dayCnt) {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTime(new Date(time));
    		calendar.add(Calendar.DATE, dayCnt);
    		String back = sdf.format(calendar.getTime());
    		return Integer.valueOf(back);
    	}
    
    	/**
    	 * 获取给定时间戳当天的结束时间戳
    	 *
    	 * @param timeMillis
    	 * @return 时间戳形式
    	 */
    	public static long getDayEndMs(long timeMillis) {
    		Date date = new Date(timeMillis);
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTime(date);
    		calendar.set(Calendar.HOUR_OF_DAY, 23);
    		calendar.set(Calendar.SECOND, 59);
    		calendar.set(Calendar.MINUTE, 59);
    		calendar.set(Calendar.MILLISECOND, 999);
    		return calendar.getTimeInMillis();
    	}
    
    	/**
    	 * 获取给定时间戳当天的开始时间戳
    	 *
    	 * @param timeMillis
    	 * @return 时间戳形式
    	 */
    	public static long getDayStartMs(long timeMillis) {
    		Date date = new Date(timeMillis);
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTime(date);
    		calendar.set(Calendar.SECOND, 0);
    		calendar.set(Calendar.MINUTE, 0);
    		calendar.set(Calendar.HOUR_OF_DAY, 0);
    		calendar.set(Calendar.MILLISECOND, 0);
    		return calendar.getTimeInMillis();
    	}
    
    
    	/**
    	 * 获取当前时间的Hour
    	 *
    	 * @return 时间戳形式
    	 */
    	public static int getCurrentHour() {
    		Calendar calendar = Calendar.getInstance();
    		return calendar.get(Calendar.HOUR_OF_DAY);
    	}
    
    
    	/**
    	 * 获取指定时间所在周周一
    	 * @param time Date类型yyyy-MM-dd  日期
    	 * @return 获得 yyyy-MM-dd 参数所在日期的周一时间
    	 */
    	public static String getMonday(String time) {
    		//Date time = DateTimeUtil.dateStrToDate("2017-11-01");
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
    		Calendar cal = Calendar.getInstance();
    		try {
    			cal.setTime(sdf.parse(time));
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    		//判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
    		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
    		if (1 == dayWeek) {
    			cal.add(Calendar.DAY_OF_MONTH, -1);
    		}
    		//System.out.println("要计算日期为:"+sdf.format(cal.getTime())); //输出要计算日期
    		cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
    		int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
    		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);//根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
    		String imptimeBegin = sdf.format(cal.getTime()); //周一时间
    		return imptimeBegin;
    	}
    
    	/**
    	 * 获取指定时间所在周周日
    	 * @param time Date类型yyyy-MM-dd  日期
    	 * @return 获得 yyyy-MM-dd 参数所在日期的周一时间
    	 */
    	public static String getSunday(String time) {
    		//Date time = DateTimeUtil.dateStrToDate("2017-11-01");
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    		Calendar cal = Calendar.getInstance();
    		try {
    			cal.setTime(sdf.parse(time));
    			cal.setFirstDayOfWeek(Calendar.MONDAY);
    			cal.set(Calendar.DAY_OF_WEEK, 1);
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    		return sdf.format(cal.getTime());
    	}
    
    	/**
    	 * 获取指定时间段内月份:
    	 * 2020-01, 2020-02, 2020-03, 2020-04, 2020-05, 2020-06, 2020-07, 2020-08, 2020-09, 2020-10, 2020-11, 2020-12
    	 *
    	 * @param minDate 2020-01-01
    	 * @param maxDate 2020-12-31
    	 * @return
    	 */
    	public static List<String> getMonthBetween(String minDate, String maxDate) {
    		ArrayList<String> result = new ArrayList<String>();
    		try {
    			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
    			Calendar min = Calendar.getInstance();
    			Calendar max = Calendar.getInstance();
    			min.setTime(sdf.parse(minDate));
    			min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
    
    			max.setTime(sdf.parse(maxDate));
    			max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
    
    			Calendar curr = min;
    			while (curr.before(max)) {
    				result.add(sdf.format(curr.getTime()));
    				curr.add(Calendar.MONTH, 1);
    			}
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    		return result;
    	}
    
    	/**
    	 * 获取某年某月天数
    	 *
    	 * @param year  年 yyyy
    	 * @param month MM
    	 * @return 当年当月共有多少天
    	 */
    	public static int getMaxDate(int year, int month) {
    		Calendar a = Calendar.getInstance();
    		a.set(Calendar.YEAR, year);
    		a.set(Calendar.MONTH, month - 1);
    		a.set(Calendar.DATE, 1);
    		a.roll(Calendar.DATE, -1);
    		int maxDate = a.get(Calendar.DATE);
    		return maxDate;
    	}
    
    	/**
    	 * 判断是否是今天
    	 *
    	 * @param date yyyy-MM-dd
    	 * @return true  同一天  false  不是当天
    	 */
    	public static Boolean isToday(String date) {
    		//String date = "2017-11-15";
    		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
    		Long time = System.currentTimeMillis();
    		String d = sd.format(time);
    		return date.equals(d);
    	}
    
    
    	public static void main(String[] args) {
    		System.out.println(getMonthBetween("2020-01-01", "2020-12-31"));
    		System.out.println(getDayOfWeek("2021-02-19"));
    		System.out.println(getYear(1613804598000L));
    		System.out.println(getMonth(1613804598000L));
    		System.out.println(getDay(1613804598000L));
    		System.out.println(getTimeNumberToday());
    		System.out.println(getYyyyMMddString(1612162998000L));
    		System.out.println(yyyyMMddHHmm.format(getDayZeroHour(0)));
    		System.out.println(yyyyMMddHHmm.format(getDayZeroHour(5)));
    		System.out.println(yyyyMMddHHmm.format(getDay24Hour(1)));
    		System.out.println(longToString(System.currentTimeMillis(), ymd));
    		System.out.println(stringToDate("2021-02-19", ymd));
    		System.out.println(getStartDayOfWeek(stringToDate("2021-02-19", ymd)));
    		System.out.println(getLastDayOfWeek(stringToDate("2021-02-19", ymd)));
    		System.out.println(getStartDayOfMonth(stringToDate("2021-02-19", ymd)));
    		System.out.println(getNextDayOfMonth(stringToDate("2021-02-19", ymd), 5));
    		System.out.println(getLastDayOfMonth(stringToDate("2021-02-19", ymd)));
    		System.out.println(getStartDayOfNextMonth(stringToDate("2021-02-19", ymd)));
    		System.out.println(getLastDayOfNextMonth(stringToDate("2021-02-19", ymd)));
    		System.out.println(givedTimeToBefore("2021-02-19", 3600 * 24 * 10L, ymd));
    		System.out.println(stringToLong("2021-02-19", ymd));
    		System.out.println(splitYMD("2021-02-19")[0] + "===" + splitYMD("2021-02-19")[1] + "===" + splitYMD("2021-02-19")[2]);
    		System.out.println(isLeapYear(2020));
    		System.out.println(formatMonthDay(1));
    		System.out.println(formatYear(20));
    		System.out.println(countDay("2021-02-09", "2021-02-19"));
    		System.out.println(getEveryday("2021-02-09", "2021-02-19"));
    		System.out.println(getWeek("2021-02-09"));
    		System.out.println(reverse2Long("2021-02-09"));
    		System.out.println(reverse2Long("20210209"));
    		System.out.println(reverse2Long("2021/02/09"));
    		System.out.println(parseNextZero(System.currentTimeMillis()));
    		System.out.println(getDaysOfMonth(System.currentTimeMillis()));
    		System.out.println(calcDate(2020,2 ));
    		System.out.println(getAfterDate(System.currentTimeMillis(),-2 ));
    		System.out.println(getDayEndMs(System.currentTimeMillis() ));
    		System.out.println(getDayStartMs(System.currentTimeMillis() ));
    		System.out.println(getCurrentHour(  ));
    		System.out.println(getMonday("2021-02-20"));
    		System.out.println(getSunday("2021-02-20"));
    
    
    	}
    
    
    }

     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值