Java工具类-日期工具类

日期工具类


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

import org.apache.commons.lang3.StringUtils;

public class MyDateUtils {

	private MyDateUtils() {
		throw new IllegalStateException("MyDateUtils class");
	}

	public static final String DATE_FORMAT_YMD = "yyyy-MM-dd";
	public static final String DATE_FORMAT_YMD_HM = DATE_FORMAT_YMD + " HH:mm";
	public static final String DATE_FORMAT_YMD_HMS = DATE_FORMAT_YMD + " HH:mm:ss";
	public static final String DATE_FORMAT_YMDHMS = "yyyyMMddHHmmss";

	/**
	 * 获取被月偏移1号零时零分零秒
	 * @param curDay    Date
	 * @param offset    int
	 * @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public static String getFirstZerotimeDayByMonthoffset(Date curDay, int offset, String formatStr) {
		return new SimpleDateFormat(formatStr).format(getFirstZerotimeDayByMonthoffset(curDay, offset));
	}

	public static Date getFirstZerotimeDayByMonthoffset(Date curDay, int offset) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(curDay);
		if (offset != 0) {
			cal.add(Calendar.MONTH, offset);
		}
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取被日偏移某天的零时零分零秒
	 * @param curDay    Date
	 * @param offset    int
	 * @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public static String getZerotimeDayByDayoffset(Date curDay, int offset, String formatStr) {
		return new SimpleDateFormat(formatStr).format(getZerotimeDayByDayoffset(curDay, offset));
	}

	public static Date getZerotimeDayByDayoffset(Date curDay, int offset) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(curDay);
		if (offset != 0) {
			cal.add(Calendar.DAY_OF_MONTH, offset);
		}
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取被日偏移某天的23时59分59秒
	 * @param curDay    Date
	 * @param offset    int
	 * @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public static String getEndtimeDayByDayoffset(Date curDay, int offset, String formatStr) {
		return new SimpleDateFormat(formatStr).format(getEndtimeDayByDayoffset(curDay, offset));
	}

	public static Date getEndtimeDayByDayoffset(Date curDay, int offset) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(curDay);
		if (offset != 0) {
			cal.add(Calendar.DAY_OF_MONTH, offset);
		}
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}

	/**
	 * 获取被日偏移的某天
	 * @param curDay    Date
	 * @param offset    int
	 * @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
	 */
	public static String getDayByDayoffset(Date curDay, int offset, String formatStr) {
		return new SimpleDateFormat(formatStr).format(offset == 0 ?curDay :getDayByDayoffset(curDay, offset));
	}

	public static Date getDayByDayoffset(Date curDay, int offset) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(curDay);
		if (offset != 0) {
			cal.add(Calendar.DAY_OF_MONTH, offset);
		}
		return cal.getTime();
	}

	/**
	 * 获取被小时偏移的某天
	 * @param curDay    Date
	 * @param offset    int
	 * @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
	 */
	public static String getDayByHourOffset(Date curDay, int offset, String formatStr) {
		return new SimpleDateFormat(formatStr).format(offset == 0 ?curDay :getDayByHourOffset(curDay, offset));
	}

	public static Date getDayByHourOffset(Date curDay, int offset) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(curDay);
		if (offset != 0) {
			cal.add(Calendar.HOUR_OF_DAY, offset);
		}
		return cal.getTime();
	}

	/**
	 * 获取被秒偏移的某天
	 * @param curDay    Date
	 * @param offset    int
	 * @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public static String getDayBySecoundoffset(Date curDay, int offset, String formatStr) {
		return new SimpleDateFormat(formatStr).format(offset == 0 ?curDay :getDayBySecoundoffset(curDay, offset));
	}

	public static Date getDayBySecoundoffset(Date curDay, int offset) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(curDay);
		if (offset != 0) {
			cal.add(Calendar.SECOND, offset);
		}
		return cal.getTime();
	}

	/**
	 * 字串转日期(零时零分零秒)
	 * @param dateStr
	 * @param format  "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public static Date convertStr2Date(String dateStr, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(StringUtils.isNotBlank(format) ?format :DATE_FORMAT_YMDHMS);
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 日期比较
	 * @param date1 String
	 * @param date2 String
	 * @return -2:异常; 1:大于; 0:等于; -1:小于
	 */
	public static int compareDate(String date1, String date2) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YMD);
		if (StringUtils.isNotBlank(date1) && StringUtils.isNotBlank(date2)) {
			try {
				long dl1 = sdf.parse(date1).getTime();
				long dl2 = sdf.parse(date2).getTime();
				if (dl1 > dl2) {
					return 1;
				} else if (dl1 == dl2) {
					return 0;
				} else {
					return -1;
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return -2;
	}

	/**
	 * 获取当前时间戳,单位秒
	 * @return long
	 */
	public static long getCurrentTimestamp() {
		return System.currentTimeMillis() / 1000;
	}

	/**
	 * 日期格式
	 * @param date
	 * @param format
	 * @return
	 */
	public static String date2Format(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亚历山大伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值