Java日期时间加减比较工具类

package com.zy.day15;

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

public class DateUtils {

	public static void main(String[] args) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date1 = null, date2 = null;
		try {
			date1 = simpleDateFormat.parse("2018/07/17 12:00:56");
			date2 = simpleDateFormat.parse("2019/07/17 12:00:56");
		} catch (ParseException e) {
			e.printStackTrace();
		}
//		System.out.println(dateAddHours(date1, 15));
//		System.out.println(dateAddMinutes(date1, 15));
//		System.out.println(dateAddSeconds(date1, 15));
//		System.out.println(dateAddDays(date1, 10));
//		System.out.println(dateCompare(date1, date2));
//		System.out.println(getLastDayOfMonth(date1, 12));
	}

	/** 
	 * 时间加减小时
	 * @param startDate 要处理的时间,Null则为当前时间 
	 * @param hours 加减的小时 
	 * @return Date 
	 */
	public static Date dateAddHours(Date startDate, int hours) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hours);
		return c.getTime();
	}

	/**
	 * 时间加减分钟
	 * @param startDate 要处理的时间,Null则为当前时间 
	 * @param minutes 加减的分钟
	 * @return Date
	 */
	public static Date dateAddMinutes(Date startDate, int minutes) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes);
		return c.getTime();
	}

	/**
	 * 时间加减秒数
	 * @param startDate 要处理的时间,Null则为当前时间 
	 * @param minutes 加减的秒数
	 * @return Date
	 */
	public static Date dateAddSeconds(Date startDate, int seconds) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.SECOND, c.get(Calendar.SECOND) + seconds);
		return c.getTime();
	}

	/** 
	 * 时间加减年数
	 * @param startDate 要处理的时间,Null则为当前时间 
	 * @param years 加减的年数 
	 * @return Date 
	 */
	public static Date dateAddYears(Date startDate, int years) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.YEAR, c.get(Calendar.YEAR) + years);
		return c.getTime();
	}

	/** 
	 * 时间加减月数
	 * @param startDate 要处理的时间,Null则为当前时间 
	 * @param months 加减的月数 
	 * @return Date 
	 */
	public static Date dateAddMonths(Date startDate, int months) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.MONTH, c.get(Calendar.MONTH) + months);
		return c.getTime();
	}

	/** 
	 * 时间加减天数 
	 * @param startDate 要处理的时间,Null则为当前时间 
	 * @param days 加减的天数 
	 * @return Date 
	 */
	public static Date dateAddDays(Date startDate, int days) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.DATE, c.get(Calendar.DATE) + days);
		return c.getTime();
	}

	/** 
	 * 时间比较(如果myDate>compareDate返回1,<返回-1,相等返回0) 
	 * @param myDate 时间 
	 * @param compareDate 要比较的时间 
	 * @return int 
	 */
	public static int dateCompare(Date myDate, Date compareDate) {
		Calendar myCal = Calendar.getInstance();
		Calendar compareCal = Calendar.getInstance();
		myCal.setTime(myDate);
		compareCal.setTime(compareDate);
		return myCal.compareTo(compareCal);
	}

	/**
	 * 获取两个时间中最小的一个时间
	 * @param date
	 * @param compareDate
	 * @return Date
	 */
	public static Date dateMin(Date date, Date compareDate) {
		if (date == null) {
			return compareDate;
		}
		if (compareDate == null) {
			return date;
		}
		if (1 == dateCompare(date, compareDate)) {
			return compareDate;
		} else if (-1 == dateCompare(date, compareDate)) {
			return date;
		}
		return date;
	}

	/**
	 * 获取两个时间中最大的一个时间
	 * @param date
	 * @param compareDate
	 * @return Date
	 */
	public static Date dateMax(Date date, Date compareDate) {
		if (date == null) {
			return compareDate;
		}
		if (compareDate == null) {
			return date;
		}
		if (1 == dateCompare(date, compareDate)) {
			return date;
		} else if (-1 == dateCompare(date, compareDate)) {
			return compareDate;
		}
		return date;
	}
	
	
	/**
	 * 获取时间当年某个月的最后一天
	 * @param startDate
	 * @param month 月份
	 * @return int 天数
	 */
	public static int getLastDayOfMonth(Date startDate, int month) {
		if (startDate == null) {
			startDate = new Date();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		c.set(c.get(Calendar.YEAR), month, 1);
		c.add(Calendar.DATE, -1);
		int day = c.get(Calendar.DAY_OF_MONTH);
		return day;
	}

}

 

你可能感兴趣的:(Java)