开发中常用的日期时间工具类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33223299/article/details/82853382
package com.yufei.core.util;

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

/**
 * 日期工具类
 * 
 * @author :lzw
 * @date :2016年3月11日下午5:28:53
 */
public class DateUtil {
	private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

	private final static SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");

	private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");

	private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");

	private final static SimpleDateFormat sdfDayPot = new SimpleDateFormat("yyyy.MM.dd");

	private final static SimpleDateFormat sdfDaySlash = new SimpleDateFormat("yyyy/MM/dd");

	private final static SimpleDateFormat sdfDayCHS = new SimpleDateFormat("yyyy年MM月dd日");

	private final static SimpleDateFormat sdfHourTime = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 返回当前时间【yyyy】字符串格式
	 */
	public static String getYear() {
		return sdfYear.format(new Date());
	}

	/**
	 * 返回当前时间【MM】字符串格式
	 */
	public static String getMonth() {
		return sdfMonth.format(new Date());
	}

	/**
	 * 返回当前时间【yyyy-MM-dd】字符串格式
	 */
	public static String getDay() {
		return sdfDay.format(new Date());
	}

	/**
	 * 返回指定的时间【yyyy-MM-dd】字符串格式
	 */
	public static String getDayHyphen(Date date) {
		if (date == null) {
			return null;
		}
		return sdfDay.format(date);
	}

	/**
	 * 返回指定日期【yyyy-MM-dd】字符串格式
	 */
	public static Date fomatDate(String date) {
		try {
			return sdfDay.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 返回当前时间【yyyyMMdd】字符串格式
	 */
	public static String getDays() {
		return sdfDays.format(new Date());
	}

	/**
	 * 返回指定时间【yyyyMMdd】字符串日格式
	 */
	public static String getDayWithoutSymbol(Date date) {
		if (date == null) {
			return null;
		}
		return sdfDays.format(date);
	}

	/**
	 * 返回当前时间【yyyy年MM月dd日】字符串格式
	 */
	public static String getDaysCHS() {
		return sdfDayCHS.format(new Date());
	}

	/**
	 * 返回指定时间【yyyy年MM月dd日】字符串格式
	 */
	public static String getDaysCHS(Date date) {
		if (date == null) {
			return null;
		}
		return sdfDayCHS.format(date);
	}

	/**
	 * 返回指定时间【yyyy.MM.dd】字符串格式
	 */
	public static String getDayPot(Date date) {
		if (date == null) {
			return null;
		}
		return sdfDayPot.format(date);
	}

	/**
	 * 返回指定时间【yyyy/MM/dd日】字符串格式
	 */
	public static String getDaySlash(Date date) {
		if (date == null) {
			return null;
		}
		return sdfDaySlash.format(date);
	}

	/**
	 * 返回指定日期【yyyy/MM/dd日】日期格式
	 */
	public static Date fomatDaySlash(String date) {
		try {
			date = date.replace("-", "/");
			return sdfDaySlash.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 返回指定的日期【yyyy-MM-dd HH:mm】日期格式
	 */
	public static Date fomatDateTime(String date) {
		if (date == null || date == "") {
			return null;
		}
		// 先转化为yyyy-MM-dd HH:mm,如果不合法,则转化为 yyyy-MM-dd输出
		try {
			return sdfHourTime.parse(date);
		} catch (ParseException e) {
			try {
				return sdfDay.parse(date);
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 返回当前时间【yyyy-MM-dd HH:mm:ss】日期格式
	 */
	public static Date fomatTime(String dateStr) {
		try {
			return sdfTime.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 返回当前时间【yyyy-MM-dd HH:mm:ss】字符串格式
	 */
	public static String getTime() {
		return sdfTime.format(new Date());
	}

	/**
	 * 判断d1和d2是否为同一天
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static Boolean isSameDay(Date d1, Date d2) {
		if (getDayPot(d1).equals(getDayPot(d2))) {
			return true;
		}
		return false;
	}

	/**
	 * 两个日期比较大小(忽略小时,只比较日期)
	 * 
	 * @param d1
	 * @param d2
	 * @return -1:d1在d2前;0:等于;1:d1在d2后
	 */
	public static int compareDate(Date d1, Date d2) {
		try {
			int dt1 = Integer.parseInt(getDayWithoutSymbol(d1));
			int dt2 = Integer.parseInt(getDayWithoutSymbol(d2));
			if (dt1 < dt2) {
				return -1;
			} else if (dt1 > dt2) {
				return 1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return -2;
	}

	/**
	 * 两个日期比较大小(包含小时)
	 * 
	 * @param d1
	 *            d2 d3,判断d3是否在d1和d2之间 -1 值不合法为空,1 在d1和d2之间,0不在范围之间
	 */
	public static int compareDateTime(Date d1, Date d2) {
		try {
			if (d1 == null || d2 == null) {
				return -1;
			}
			if (d1.getTime() <= d2.getTime()) {
				return 1;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * <li>功能描述:时间相减得到天数
	 * 
	 * @param beginDateStr
	 * @param endDateStr
	 * @return long
	 * @author Administrator
	 */
	public static long getDaySub(String beginDateStr, String endDateStr) {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
		java.util.Date beginDate = null;
		java.util.Date endDate = null;
		try {
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
	}

	/**
	 * <li>功能描述:时间相减得到天数
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return long
	 * @author Administrator
	 */
	public static long getDaySub(Date beginDate, Date endDate) {
		return (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
	}

	/**
	 * 当前日期的n天之后的日期【yyyy-MM-dd】字符串格式
	 * 
	 * @param days
	 * @return
	 */
	public static String getAfterDayDate(int days) {
		// 获取当前时间
		Calendar canlendar = Calendar.getInstance();
		// 日期减 如果不够减会将月变动
		canlendar.add(Calendar.DATE, days);
		Date date = canlendar.getTime();
		return sdfDay.format(date);
	}

	/**
	 * 得到n天之后的指定日期【yyyy-MM-dd】日期格式
	 * 
	 * @param beginDate
	 * @param days
	 * @return 日期型
	 */
	public static Date getAfterDayDate(Date beginDate, int days) {
		try {
			// 获取指定日期的n天之后的日期
			String dateStr = getAfterDayDateStr(beginDate, days);
			return sdfDay.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 得到指定日期的n天之后日期【yyyy-MM-dd】字符串格式
	 * 
	 * @param beginDate
	 * @param days
	 * @return 字符型
	 */
	public static String getAfterDayDateStr(Date beginDate, int days) {
		// 获取当前时间
		Calendar canlendar = Calendar.getInstance();
		// 设置指定日期
		canlendar.setTime(beginDate);
		canlendar.add(Calendar.DATE, days);
		Date date = canlendar.getTime();
		return sdfDay.format(date);
	}

	/**
	 * 得到指定日期的n月之后的日期【yyyy-MM-dd HH:mm:ss】日期格式
	 * 
	 * @param beginDate
	 * @param months
	 * @return 日期型
	 */
	public static Date getAfterMonthDate(Date beginDate, int months) {
		try {
			Calendar canlendar = Calendar.getInstance();
			canlendar.setTime(beginDate);
			canlendar.add(Calendar.MONTH, months);
			Date date = canlendar.getTime();
			return sdfTime.parse(sdfTime.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 得到n天之后是周几
	 * 
	 * @param days
	 * @return
	 */
	public static String getAfterDayWeek(String days) {
		int daysInt = Integer.parseInt(days);
		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("E");
		return sdf.format(date);
	}

	/**
	 * 两个日期相差月份
	 * 
	 * @param d1
	 *            起始日期
	 * @param d2
	 *            结束日期
	 * @return
	 */
	public static int getDateSpanMonth(Date d1, Date d2) {
		Calendar c = Calendar.getInstance();

		Date new_d2 = getAfterDayDate(d2, 1);
		c.setTime(new_d2);
		int year1 = c.get(Calendar.YEAR);
		int month1 = c.get(Calendar.MONTH);

		c.setTime(d1);
		int year2 = c.get(Calendar.YEAR);
		int month2 = c.get(Calendar.MONTH);

		int result;
		if (year1 == year2) {
			result = month1 - month2;
		} else {
			result = 12 * (year1 - year2) + month1 - month2;
		}
		return result;
	}

	public static void main(String[] args) {
		int i = compareDate(fomatDate("2016-06-01 00:00"), fomatDate("2016-06-02 00:00"));
		Date d = getAfterMonthDate(new Date("2016/08/31"), 6);
		System.out.println(getDays());
		System.out.println(getAfterDayWeek("3"));
	}

	/**
	 * 获取某个日期的前一周的星期几的日期
	 * @param date
	 * @param weekDetail
	 * @return
	 */
	public static String lastWeekDetail(Date date, int weekDetail) {

		// 作用防止周日得到本周日期
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
			calendar.add(Calendar.DAY_OF_WEEK, -1);
		}
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		int offset = 7 - dayOfWeek;
		calendar.add(Calendar.DATE, offset - 9);

		return sdfDay.format(getFirstDayOfWeek(calendar.getTime(), weekDetail + 1));// 这是从上周日开始数的到本周五为6

	}

	public static Date getFirstDayOfWeek(Date date, int firstDayOfWeek) {
		Calendar cal = Calendar.getInstance();
		if (date != null)
			cal.setTime(date);
		cal.setFirstDayOfWeek(firstDayOfWeek);// 设置一星期的第一天是哪一天
		cal.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);// 指示一个星期中的某天
		cal.set(Calendar.HOUR_OF_DAY, 0);// 指示一天中的小时。HOUR_OF_DAY 用于 24
											// 小时制时钟。例如,在 10:04:15.250 PM
											// 这一时刻,HOUR_OF_DAY 为 22。
		cal.set(Calendar.MINUTE, 0);// 指示一小时中的分钟。例如,在 10:04:15.250 PM
									// 这一时刻,MINUTE 为 4。
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	/**
	 * 获取本周一的日期
	 * @param date
	 * @return
	 */
	public static Date getThisWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		return cal.getTime();
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_33223299/article/details/82853382
今日推荐