日期工具类

本文介绍了一个包含多种日期操作功能的工具类,包括日期比较、格式转换、周期计算、年龄计算等。提供了丰富的示例代码,适用于Java开发人员进行日期相关任务。

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

日期工具类

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateUtil {
	
	/**
	 * 比较两个日期的年月日,忽略时分秒。
	 * 
	 * @param d1
	 * @param d2
	 * @return 如果d1晚于d2返回大于零的值,如果d1等于d2返回0,否则返回一个负值。
	 */
	public static int compare(Date d1, Date d2) {
		return new LocalDate(d1.getTime())
				.compareTo(new LocalDate(d2.getTime()));
	}

	/**
	 * 将日期转换成Date对象,支持的格式为yyyyMMdd, yyyyMMddHHmmss或者yyyy-MM-dd, yyyy-MM-dd
	 * HH:mm:ss,日期分隔符为(-,/,\)中的任意一个, 时间分隔符为(:)。 如果传入的日期格式不正确将返回null。
	 * 
	 * @param date
	 * @return 如果传入的日期格式正确将返回一个Date对象,否则返回null。
	 */
	public static Date toDate(String str) {
		if (str == null
				|| (str.length() != 8 && str.length() != 14
						&& str.length() != 10 && str.length() != 19)) {
			return null;
		}
		String pattern = null;
		if (str.length() == 8) {
			pattern = "yyyyMMdd";
		} else if (str.length() == 14) {
			pattern = "yyyyMMddHHmmss";
		} else if (str.length() == 10 || str.length() == 19) {
			if (str.contains("-")) {
				pattern = "yyyy-MM-dd";
			} else if (str.contains("/")) {
				pattern = "yyyy/MM/dd";
			} else if (str.contains("\\")) {
				pattern = "yyyy\\MM\\dd";
			}
			if (str.length() == 19) {
				pattern += " HH:mm:ss";
			}
		}
		DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
		return DateTime.parse(str, dtf).toDate();
	}

	/**
	 * 按如下格式:yyyy-MM-dd 返回日期。
	 * 
	 * @param date
	 * @return
	 */
	public static String toString(Date date) {
		if (date == null) {
			return null;
		}
		return new DateTime(date).toString("yyyy-MM-dd");
	}

	/**
	 * 按指定的格式返回日期。
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String toString(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		return new DateTime(date).toString(pattern);
	}

	/**
	 * 按如下格式:yyyy-MM-dd HH:mm:ss 返回日期。
	 * 
	 * @param date
	 * @return
	 */
	public static String toLongString(Date date) {
		if (date == null) {
			return null;
		}
		return new DateTime(date).toString("yyyy-MM-dd HH:mm:dd");
	}

	/**
	 * 按指定格式返回日期。
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String toLongString(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		return new DateTime(date).toString(pattern);
	}

	/**
	 * 获取基准日期是开始日期后的第几周,0到7天为第一周,8到14到第二周,依此类推。
	 * 
	 * @param begin
	 *            起始日期。
	 * @param datum
	 *            基准日期。
	 * @return 基准日期是开始日期后的第几周。
	 */
	public static int getWeeks(Date begin, Date datum) {
		if (compare(begin, datum) > 0) {
			return -1;
		}
		int days = getPeriod(begin, datum);
		return days % 7 > 0 ? days / 7 + 1 : days / 7;
	}

	/**
	 * 计算周年。
	 * 
	 * @param beginDate
	 *            起始日期。
	 * @param calculateDate
	 *            计算日。
	 * @return
	 */
	public static int getAnniversary(Date beginDate, Date calculateDate) {
		DateTime start = new DateTime(beginDate);
		DateTime end = new DateTime();
		if (calculateDate != null) {
			end = new DateTime(calculateDate);
		}
		Period p = new Period(start, end, PeriodType.years());
		return p.getYears();
	}

	/**
	 * 计算两个日期之间的天数,参数null表示当前日期。如果date2为null,计算date1到当前时间的天数。
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getPeriod(Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return 0;
		}
		DateTime start = new DateTime(date1);
		DateTime end = new DateTime();
		if (date2 != null) {
			end = new DateTime(date2);
		}
		Period p = new Period(start, end, PeriodType.days());
		return p.getDays();
	}
	 /**
	  * 计算年龄
	  * @param birthday 出生日期
	  * @param calculateDate 计算日期(不填默认为当前日期)
	  * @return
	  */
    public static int getAgeByBirthday(Date birthday, Date calculateDate){
    	Calendar cal = Calendar.getInstance();
    	
    	if (calculateDate != null) {
    		cal.setTime(calculateDate);
		}
    	
    	if (cal.before(birthday)) {
    		throw new IllegalArgumentException(
    				"The birthDay is before Now.It's unbelievable!");
    	}

    	int yearNow = cal.get(Calendar.YEAR);
    	int monthNow = cal.get(Calendar.MONTH) + 1;
    	int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

    	cal.setTime(birthday);
    	int yearBirth = cal.get(Calendar.YEAR);
    	int monthBirth = cal.get(Calendar.MONTH) + 1;
    	int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

    	int age = yearNow - yearBirth;

    	if (monthNow <= monthBirth) {
    		if (monthNow == monthBirth) { 		
    			if (dayOfMonthNow < dayOfMonthBirth) {
    				age--;
    			}
    		} else {
    			age--;
    		}
    	}
    	return age;
    }
    
	/**
	 * 获得本周星期日的日期
	 * 
	 * @return
	 */
	public static Date getCurrentWeekday() {
		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
		Date monday = currentDate.getTime();
		return monday;
	}

	/**
	 * 获得下周星期一的日期
	 * 
	 * @return
	 */
	public static Date getNextMonday() {
		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
		Date monday = currentDate.getTime();
		return monday;
	}

	/**
	 * 获得下周星期日的日期
	 * 
	 * @return
	 */
	public static Date getNextSunday() {
		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 + 6);
		Date monday = currentDate.getTime();
		return monday;
	}

	/**
	 * 获得当前日期与本周日相差的天数
	 * 
	 * @return
	 */
	private static int getMondayPlus() {
		Calendar cd = Calendar.getInstance();
		// 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 1) {
			return 0;
		} else if (dayOfWeek == 0) {
			return -6;
		} else {
			return 1 - dayOfWeek;
		}
	}
    
    /**
	 * 获取年份。
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		return new DateTime(date).getYear();
	}

	/**
	 * @param date
	 * @return 实际月份的字面值,比Calendar返回的值多1。
	 */
	public static int getMonth(Date date) {
		return new DateTime(date).getMonthOfYear();
	}

	/**
	 * @param date
	 * @return
	 */
	public static int getDayOfMonth(Date date) {
		return new DateTime(date).getDayOfMonth();
	}

	/**
	 * @param date
	 * @return
	 */
	public static int getHour(Date date) {
		return new DateTime(date).getHourOfDay();
	}

	/**
	 * @param date
	 * @return
	 */
	public static int getMunite(Date date) {
		return new DateTime(date).getMinuteOfHour();
	}

	/**
	 * @param date
	 * @return
	 */
	public static int getSecond(Date date) {
		return new DateTime(date).getSecondOfMinute();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值