项目开发中DateUtils工具类常见方法

package com.chang.util;

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

/**
 * 
 * @ClassName: DateUtils
 * @Description: 日期工具类 默认使用 "yyyy-MM-dd HH:mm:ss" 格式化日期
 * @author
 * @date
 * @Copyright:
 */
public final class DateUtils {
	//==========================日期格式化的各种格式===============================
	/**
	 * 英文简写(默认)如:2010-12-01
	 */
	public static String FORMAT_SHORT = "yyyy-MM-dd";
	/**
	 * 英文全称 如:2010-12-01 23:15:06
	 */
	public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
	/**
	 * 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
	 */
	public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
	/**
	 * 中文简写 如:2010年12月01日
	 */
	public static String FORMAT_SHORT_CN = "yyyy年MM月dd日";
	/**
	 * 中文全称 如:2010年12月01日 23时15分06秒
	 */
	public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH时mm分ss秒";
	/**
	 * 精确到毫秒的完整中文时间
	 */
	public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";

	/**
	 * 获得默认的 date pattern
	 */
	public static String getDatePattern() {
		return FORMAT_LONG;
	}

	//==========================对日期进行格式化===============================
	/**
	 * 返回当前日期     固定格式为2021-10-11 16:10:22
	 */
	public static String getNow() {
		return format(new Date());
	}

	/**
	 * 返回当前日期     可以自定义自己需要的格式  如:2021-10-11 16:12:14.443  2021年10月11日 等等
	 */
	public static String getNow(String format) {
		return format(new Date(), format);
	}

	/**
	 * 根据日期返回格式化日期  固定格式为2021-10-11 16:19:14
	 */
	public static String format(Date date) {
		return format(date, getDatePattern());
	}

	/**
	 * 使用用户自定义格式格式化日期    可以自定义自己需要的格式  如:2021-10-11 16:12:14.443  2021年10月11日 等等
	 * @param date  日期
	 * @param pattern 日期格式
	 */
	public static String format(Date date, String pattern) {
		String returnValue = "";
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}

	//==========================获取日期月的前后一天===============================
	/**
	 * 获取当前月的第一天        Fri Oct 01 14:26:39 CST 2021
	 * @return date
	 */
	public static Date getFirstDayOfMonth() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return cal.getTime();
	}

	/**
	 * 获取当前月的最后一天      Sun Oct 31 14:32:38 CST 2021
	 * @return
	 */
	public static Date getLastDayOfMonth() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return cal.getTime();
	}

	/**
	 * 获取当前月(传入的时间的)的第一天   Fri Oct 01 15:14:05 CST 2021
	 * @return date
	 */
	public static Date getFirstDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return cal.getTime();
	}

	/**
	 * 获取当前月(传入的时间的)的最后一天
	 *
	 * @return
	 */
	public static Date getLastDayOfMonth(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);
		return cal.getTime();
	}

	//==========================对日期进行添加相应的时间===============================
	/**
	 * 在日期上增加数个整年    Thu Oct 12 15:39:13 CST 2028
	 * @param date  日期
	 * @param n 要增加的年数
	 */
	public static Date addYear(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, n);
		return cal.getTime();
	}

	/**
	 * 在日期上增加数个整月    Thu May 12 15:40:13 CST 2022
	 * @param date 日期
	 * @param n 要增加的月数
	 */
	public static Date addMonth(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, n);
		return cal.getTime();
	}

	/**
	 * 在日期上增加天数  返回固定日期格式。需要自行进行格式化  Thu Oct 14 16:28:28 CST 2021
	 * @param date 日期
	 * @param n  要增加的天数
	 */
	public static Date addDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		return cal.getTime();
	}

	/**
	 *
	 * @Title: addDays
	 * @Description: 日期添加天数
	 */
	public static Date addDays(Date date, int addDays) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		// 把日期往后增加一天.整数往后推,负数往前移动
		calendar.add(Calendar.DATE, addDays);
		date = calendar.getTime();
		return date;
	}

	/**
	 *
	 * @param date
	 * @param addSecond
	 * @return
	 */
	public static Date addsecond(Date date, int addSecond) {
		long time = date.getTime();
		long timeRenturn = time + (addSecond * 1000);
		date  =new Date(timeRenturn);
		return date;
	}

	//==========================根据时间返回固定的时间===============================
	/**
	 * 根据时间返回凌晨    Tue Oct 12 00:00:00 CST 2021
	 * @flag 0 返回yyyy-MM-dd 00:00:00日期<br>
	 *       1 返回yyyy-MM-dd 23:59:59日期
	 */
	public static Date weeHours(Date date, int flag) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);
		// 时分秒(毫秒数)
		long millisecond = hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
		// 凌晨00:00:00
		cal.setTimeInMillis(cal.getTimeInMillis() - millisecond);
		if (flag == 0) {
			return cal.getTime();
		} else if (flag == 1) {
			// 凌晨23:59:59
			cal.setTimeInMillis(cal.getTimeInMillis() + 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000);
		}
		return cal.getTime();
	}

	/**
	 * 获取时间戳  2021-10-11 16:30:48.580
	 */
	public static String getTimeString() {
		SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
		Calendar calendar = Calendar.getInstance();
		return df.format(calendar.getTime());
	}

	//==========================根据时间返回指定的年月日和时间间隔等===============================
	/**
	 * 获取日期年份
	 * @param date 日期
	 */
	public static String getYear(Date date) {
		return format(date).substring(0, 4);
	}


	/**
	 * @Title: getBetweenDay @Description: TODO(判断两个日期之间相差多少天) @param @param
	 *         date1 @param @param date2 @param @return 设定文件 @return int
	 *         返回类型 @throws
	 */
	public static int getBetweenDay(Date date1, Date date2) {
		Calendar d1 = new GregorianCalendar();
		d1.setTime(date1);
		Calendar d2 = new GregorianCalendar();
		d2.setTime(date2);
		int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
		int y2 = d2.get(Calendar.YEAR);
		if (d1.get(Calendar.YEAR) != y2) {
			// d1 = (Calendar) d1.clone();
			do {
				days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
				d1.add(Calendar.YEAR, 1);
			} while (d1.get(Calendar.YEAR) != y2);
		}
		return days;
	}

	/**
	 * 使用预设格式提取字符串日期
	 * @param strDate 日期字符串
	 */
	public static Date parse(String strDate) {
		return parse(strDate, getDatePattern());
	}

	/**
	 * 使用用户格式提取字符串日期
	 * @param strDate  日期字符串
	 * @param pattern  日期格式
	 */
	public static Date parse(String strDate, String pattern) {
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		try {
			return df.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}



	/**
	 * 按默认格式的字符串距离今天的天数
	 * @param date  日期字符串
	 * @return
	 */
	public static int countDays(String date) {
		long t = Calendar.getInstance().getTime().getTime();
		Calendar c = Calendar.getInstance();
		c.setTime(parse(date));
		long t1 = c.getTime().getTime();
		return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
	}

	/**
	 * 按用户格式字符串距离今天的天数
	 * @param date  日期字符串
	 * @param format 日期格式
	 * @return
	 */
	public static int countDays(String date, String format) {
		long t = Calendar.getInstance().getTime().getTime();
		Calendar c = Calendar.getInstance();
		c.setTime(parse(date, format));
		long t1 = c.getTime().getTime();
		return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
	}

	/**
	 * UTC 时间转化为日期
	 * @Title: getUtcDate
	 * @Description: TODO
	 * @param strDate
	 * @return: Date
	 */
	static public Date getUtcDate(String strDate) {
		String date = strDate;
		date = date.replace("Z", " UTC");
		System.out.println(date);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
		Date d = null;
		try {
			d = format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * 
	 * @Title: getTodayZeroTime
	 * @Description: 获取今天的零时
	 * @return
	 * @return: Date
	 */
	public static Date getTodayZeroTime() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MILLISECOND, 0);
		return ca.getTime();
	}

	public static Integer getTodayZeroSeconds(Date date) {
		Date endDate = weeHours(date, 1);
		long time = endDate.getTime();
		long millSeconds = (time - System.currentTimeMillis())/1000;
		int intExact = Math.toIntExact(millSeconds);
		return intExact;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值