写了一个java的日期时间常用工具类

本文详细介绍了一个Java日期时间工具类的实现,包括日期格式化、日期加减、判断闰年、获取当前时间、日期比较等功能,提供了丰富的示例代码,帮助开发者高效处理日期时间问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Talk is  cheap,HERE IS THE CODE

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

/** 
 * 时间、日期工具类
 * @author  _沐阳_@youkuaiyun.com
 * @version 1.0.0,2019-6-13
 * @since   1.0.0.0
 */
public class DateTimeUtil {
	
	/**日志工具*/
	private final static Logger log = LoggerFactory.getLogger(DateTimeUtil.class);

	/**日期格式:yyyyMMdd*/
	public static final String FORMAT_DATE_YYYYMMDD="yyyyMMdd";

	/**日期格式:yyyy-MM-dd*/
	public static final String FORMAT_DATE_YYYY_MM_DD="yyyy-MM-dd";

	/**时间格式:HH:mm*/
	public static final String FORMAT_TIME_HH_MM="HH:mm";

	/**时间格式:HH:mm:ss*/
	public static final String FORMAT_TIME_HH_MM_SS="HH:mm:ss";

	/**日期时间格式:yyyyMMddHHmmss*/
	public static final String FORMAT_DATETIME_YYYYMMDDHHMMSS="yyyyMMddHHmmss";

	/**日期时间格式:yyyy-MM-dd HH:mm:ss*/
	public static final String FORMAT_DATETIME_YYYY_MM_DD_HH_MM_SS="yyyy-MM-dd HH:mm:ss";

	/**日期时间格式:yyyy-MM-dd HH:mm:ss.SSS*/
	public static final String FORMAT_DATETIME_YYYY_MM_DD_HH_MM_SS_SSS="yyyy-MM-dd HH:mm:ss.SSS";

	//以下为内部常量,未公开~
	/**大写数字(0写为〇)*/
	private static final String[] CHN_ARY = new String[]{"\u3007","\u4e00","\u4e8c","\u4e09","\u56db","\u4e94","\u516d","\u4e03","\u516b","\u4e5d"};
	/**日期时间转化字符串-零*/
	private static final String STR_ZERO="\u96f6";
	/**日期时间转化字符串-年*/
	private static final String STR_YEAR= "\u5e74";
	/**日期时间转化字符串-月*/
	private static final String STR_MONTH="\u6708";
	/**日期时间转化字符串-日*/
	private static final String STR_DAY="\u65e5";
	/**日期时间转化字符串-时*/
	private static final String STR_HOUR="\u65f6";
	/**日期时间转化字符串-分*/
	private static final String STR_MINUTE="\u5206";
	/**日期时间转化字符串-秒*/
	private static final String STR_SECOND="\u79d2";
	/**日期时间转化字符串-十*/
	private static final String STR_TEN="\u5341";
	
	/**
	 * 将java.util.Date类型转换为java.sql.Date
	 * @param fdate java.util.Date类型对象
	 * @return java.sql.Date类型对象
	 */
	public static java.sql.Date toSqlDate(Date fdate) {
		return new java.sql.Date(fdate.getTime());
	}

	/**
	 * 取得指定日期的前一自然日
	 * @param date 指定日期。
	 * @return 指定日期的前一自然日
	 */
	public static Date getPriorDay(Date date) {
		return addDay(date, -1);
	}

	/**
	 * 取得指定日期的下一自然日
	 * @param date 指定日期。
	 * @return 指定日期的下一自然日
	 */
	public static Date getNextDay(Date date) {
		return addDay(date, 1);
	}

	/**
	 * 取得指定日期的偏移指定天
	 * @param date 指定日期
	 * @param iOffSet 偏移量,大于0则加,否则减
	 * @return 偏移后的日期
	 */
	public static Date addDay(Date date,int iOffSet) {
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		gc.add(Calendar.DATE, iOffSet);
		return gc.getTime();
	}

	/**
	 * 取得指定日期的偏移指定月
	 * (注意:若2019-06-30,减少4个月,则返回2019-02-28)
	 * @param date 指定日期
	 * @param iOffSet 偏移量,大于0则加,否则减
	 * @return 偏移后的日期
	 */
	public static Date addMonth(Date date,int iOffSet) {
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		gc.add(Calendar.MONTH, iOffSet);
		return gc.getTime();
	}

	/**
	 * 取得指定日期的偏移指定年
	 * (注意:若2016-02-29,加1年,则返回2017-02-28)
	 * @param date 指定日期
	 * @param iOffSet 偏移量,大于0则加,否则减
	 * @return 偏移后的日期
	 */
	public static Date addYear(Date date,int iOffSet) {
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		gc.add(Calendar.YEAR, iOffSet);
		return gc.getTime();
	}

	/**
	 * 判断指定日期是否为闰年
	 * @return 是闰年则返回true,否则返回false
	 */
	public static boolean isLeapYear(Date date) {
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		return gc.isLeapYear(gc.get(Calendar.YEAR));
	}

	/**
	 * 功能说明:判断闰年
	 * @param year 传入得年份
	 * @return 是否为闰年;
	 */
	public static boolean isLeapYear(int year) {
		return new GregorianCalendar().isLeapYear(year);
	}

	/**
	 * 返回给定日期为星期几的标识位,获取到的返回值可以使用Calendar.MONDAY|...|SUNDAY等常量判断
	 * @param dDate 日期
	 * @return 星期几标识位:1=星期日; 2=星期一 ;3=星期二; 4=星期三; 5=星期四; 6=星期五 ;7=星期六 
	 */
	public static int getWeekDay(Date dDate) {
		GregorianCalendar cl = new GregorianCalendar();
		cl.setTime(dDate);
		return cl.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 获取当前日期格式字符串
	 * @return yyyy-MM-dd格式的当前日期
	 */
	public static String getToday(){
		return toString(new Date(),FORMAT_DATE_YYYY_MM_DD);
	}

	/**
	 * 获取当前时间格式字符串
	 * @return HH:mm:ss格式的当前时间
	 */
	public static String getNowTime(){
		return toString(new Date(),FORMAT_TIME_HH_MM_SS);
	}

	/**
	 * 获取当前日期及时间
	 * @return yyyy-MM-dd HH:mm:ss格式的当前时间
	 */
	public static String getNow(){
		return toString(new Date(),FORMAT_DATETIME_YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 返回Timestamp类型的当前日期
	 * @return 当前日期
	 */
	public static Timestamp getNowTimeStamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * 获取当前日期及时间按指定格式转化后的字符串
	 * @param format  指定格式
	 * @return 指定格式的当前日期和时间
	 */
	public static String getNow(String format){
		return toString(new Date(),format);
	}

	/**
	 * 获取日期的字符串格式
	 * @param dt 指定日期
	 * @return 格式化的字符串(默认yyyy-MM-dd)
	 */
	public static String toString(Date dt){
		return toString(dt,FORMAT_DATE_YYYY_MM_DD);
	}

	/**
	 * 获取指定日期的指定格式字符串
	 * @param dt 指定日期
	 * @param format 指定格式
	 * @return 格式化后的字符串
	 */
	public static String toString(Date dt,String format){
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(dt);
	}

	/**
	 * 将日期格式字符串转换为Date对象
	 * @param strDate 字符串(默认yyyy-MM-dd格式)
	 * @return	Date对象
	 * @throws ParseException 转换异常
	 */
	public static Date parseDate(String strDate) throws ParseException{
		return parseDate(strDate,FORMAT_DATE_YYYY_MM_DD);
	}

	/**
	 * 将指定日期格式字符串转换为Date对象
	 * @param strDate 字符串
	 * @param format 指定格式
	 * @return	Date对象
	 * @throws ParseException 转换异常
	 */
	public static Date parseDate(String strDate,String format) throws ParseException{
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		Date date;
		try {
			date = dateFormat.parse(strDate);
		} catch (ParseException e) {
			log.error("[工具类]转换日期失败,输入日期=【"+strDate+"】,输入转化格式=【"+format+"】,失败原因:"+e.getMessage());
			throw e;
		}
		return date;
	}

	/**
	 * 返回当前年份
	 * @return int 年份值
	 */
	public static final int getYear() {
		return getYear(new Date());
	}

	/**
	 * 返回指定日期所在年份
	 * @return int 年份值
	 */
	public static final int getYear(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int year = cal.get(Calendar.YEAR);
		return year;
	}

	/**
	 * 返回当前是第几季度
	 * @return int 当前是第几季度(1-4)
	 */
	public static final int getSeason() {
		return getSeason(new Date());
	}

	/**
	 * 返回指定日期所在第几季度
	 * @return int 当前是第几季度(1-4)
	 */
	public static final int getSeason(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int month = cal.get(Calendar.MONTH);
		return month/3+1;
	}

	/**
	 * 返回当前月是所处季度的第几个月
	 * @return int 当前月是所处季度的第几个月(1-3)
	 */
	public static final int getMonthOfSeason() {
		return getMonthOfSeason(new Date());
	}

	/**
	 * 返回指定日期是所处季度的第几个月
	 * @return int 当前月是所处季度的第几个月(1-3)
	 */
	public static final int getMonthOfSeason(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int month = cal.get(Calendar.MONTH);
		return month % 3 + 1;
	}

	/**
	 * 返回当前月份
	 * @return int 月值(1-12)
	 */
	public static final int getMonth() {
		return getMonth(new Date());
	}

	/**
	 * 返回指定日期的月份
	 * @return int 月值(1-12)
	 */
	public static final int getMonth(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		return cal.get(Calendar.MONTH)+1;
	}

	/**
	 * 返回当前日为几号
	 * @return int 当前日
	 */
	public static final int getDay() {
		return getDay(new Date());
	}

	/**
	 * 返回指定日期的几号
	 * @return int 所在日
	 */
	public static final int getDay(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取指定日期偏移指定月后的第一天(比如2019-06-13,偏移量-3,得到2019-03-01)
	 * @param date 日期
	 * @param monthOffSet 月份偏移量
	 * @return Date 该日期偏移后的第一天
	 */
	public static Date getFirstDayOffsetMonth(Date date,int monthOffSet) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, monthOffSet);// 在指定日期基础上偏移指定月份
		cal.set(Calendar.DATE, 1);// 上个月第一天
		return cal.getTime();
	}

	/**
	 * 获取指定日期偏移指定月后的最后天(比如2019-06-13,偏移量-3,得到2019-03-31)
	 * @param date 日期
	 * @param monthOffSet 月份偏移量
	 * @return Date 该日期偏移后的第一天
	 */
	public static Date getLastDayOffsetMonth(Date date,int monthOffSet) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);//先将日期置为1号
		cal.add(Calendar.MONTH, monthOffSet+1);//偏移月份,多加一个月
		cal.add(Calendar.DATE, -1);//再减去前一天,即得到月份偏移后的最后一天
		return cal.getTime();
	}

	/**
	 * 计算两个日期之间相差的天数,考虑两个时间相差的总毫秒数,相差时间不超过24小时的不算一天
	 * @param dt1 :减数
	 * @param dt2 :被减数
	 * @return 相差天数(参数2-参数1 的天数)
	 */
	public static int dateDiff(Date dt1, Date dt2) {
		if(dt1 == null || dt2 == null) {
			return 0;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(dt2);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return (int)between_days;
	}
	
	/**
	 * 计算两个日期之间相差的天数,只判断日期,不考虑时间
	 * @param dt1 :减数
	 * @param dt2 :被减数
	 * @return 相差天数(参数2-参数1 的天数)
	 */
	public static int dateDiffDays(Date dt1, Date dt2) {
		if(dt1 == null || dt2 == null) {
			return 0;
		}
		Calendar cal = Calendar.getInstance();
		cal.set(getYear(dt1), getMonth(dt1)-1, getDay(dt1));
		long time1 = cal.getTimeInMillis();
		cal.set(getYear(dt2), getMonth(dt2)-1, getDay(dt2));
		long time2 = cal.getTimeInMillis();
		int between_days = (int)((time2 - time1) / (1000 * 3600 * 24));
		return between_days;
	}

	/**
	 * 获取最大日期
	 * @return 2999-12-31
	 */
	public static Date maxDate(){
		Calendar cal = Calendar.getInstance();
		cal.set(2999, 11, 31, 23, 59, 59);
		return cal.getTime();
	}

	/**
	 * 获取最小日期
	 * @return 1900-01-01
	 */
	public static Date minDate(){
		Calendar cal = Calendar.getInstance();
		cal.set(1900, 0, 1, 0, 0, 0);
		return cal.getTime();
	}

	/**
	 * 将日期转化为大写的中文形式
	 * @param dt 指定日期
	 * @return 转化后的中文
	 */
	public static String toCHN(Date dt){
		return toCHN(dt,false);
	}

	/**
	 * 将日期转化为大写的中文形式
	 * @param dt 指定日期
	 * @param isHms 是否包含时分秒的转化
	 * @return 转化后的中文
	 */
	public static String toCHN(Date dt,boolean isHms){
		StringBuffer sBuffer = new StringBuffer();
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		//转换年份
		char[] str = String.valueOf(year).toCharArray();
		for (int i = 0; i < str.length; i++) {
			sBuffer.append(CHN_ARY[Integer.parseInt(String.valueOf(str[i]))]);
		}
		sBuffer.append(STR_YEAR);
		//转换月份
		sBuffer.append(toUpperCHN(month));
		sBuffer.append(STR_MONTH);
		//转换日期
		sBuffer.append(toUpperCHN(day));
		sBuffer.append(STR_DAY);

		if(isHms){//处理时分秒
			sBuffer.append(" ");//先加个空格
			int hh=cal.get(Calendar.HOUR_OF_DAY);
			int mm = cal.get(Calendar.MINUTE);
			int ss = cal.get(Calendar.SECOND);
			//小时
			sBuffer.append(toUpperCHN(hh));
			sBuffer.append(STR_HOUR);
			//分钟
			sBuffer.append(toUpperCHN(mm));
			sBuffer.append(STR_MINUTE);
			//秒
			sBuffer.append(toUpperCHN(ss));
			sBuffer.append(STR_SECOND);
		}
		return sBuffer.toString();
	}
	
	/**
	 * 将数字转化为大写(仅限于0-99)
	 * @param num 数字(仅限于0-99)
	 * @return 大写后的字符串
	 */
	private static String toUpperCHN(int num){
		StringBuffer sBuffer = new StringBuffer();
		if(num == 0){
			sBuffer.append(STR_ZERO);
		}else if(num>=20){
			sBuffer.append(CHN_ARY[num/10]);
			sBuffer.append(STR_TEN);
		}else if(num >= 10){
			sBuffer.append(STR_TEN);
		}
		if(num>0 && num%10 != 0){
			sBuffer.append(CHN_ARY[num%10]);
		}
		return sBuffer.toString();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值