Java 计算两个日期相差年数字、月数、天数及时分秒

package sdas;

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

public class MyCalendar {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static Calendar startDate = Calendar.getInstance();
	private static Calendar endDate = Calendar.getInstance();
	private static DateFormat df = DateFormat.getDateInstance();
	private static Date earlydate = new Date();
	private static Date latedate = new Date();

	/**
	 * 计算两个时间相差多少个年
	 * 
	 * @param early
	 * @param late
	 * @return
	 * @throws ParseException
	 */
	public static int yearsBetween(String start, String end) throws ParseException {
		startDate.setTime(sdf.parse(start));
		endDate.setTime(sdf.parse(end));
		return (endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR));
	}

	/**
	 * 计算两个时间相差多少个月
	 * 
	 * @param date1
	 *            <String>
	 * @param date2
	 *            <String>
	 * @return int
	 * @throws ParseException
	 */
	public static int monthsBetween(String start, String end) throws ParseException {
		startDate.setTime(sdf.parse(start));
		endDate.setTime(sdf.parse(end));
		int result = yearsBetween(start, end) * 12 + endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH);
		return result == 0 ? 1 : Math.abs(result);

	}

	/**
	 * 计算两个时间相差多少个天
	 * 
	 * @param early
	 * @param late
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(String start, String end) throws ParseException {
		// 得到两个日期相差多少天
		return hoursBetween(start, end) / 24;
	}

	/**
	 * 计算两个时间相差多少小时
	 * 
	 * @param early
	 * @param late
	 * @return
	 * @throws ParseException
	 */
	public static int hoursBetween(String start, String end) throws ParseException {
		// 得到两个日期相差多少小时
		return minutesBetween(start, end) / 60;
	}

	/**
	 * 计算两个时间相差多少分
	 * 
	 * @param early
	 * @param late
	 * @return
	 * @throws ParseException
	 */
	public static int minutesBetween(String start, String end) throws ParseException {
		// 得到两个日期相差多少分
		return secondesBetween(start, end) / 60;
	}

	/**
	 * 计算两个时间相差多少秒
	 * 
	 * @param early
	 * @param late
	 * @return
	 * @throws ParseException
	 */
	public static int secondesBetween(String start, String end) throws ParseException {
		earlydate = df.parse(start);
		latedate = df.parse(end);
		startDate.setTime(earlydate);
		endDate.setTime(latedate);
		// 设置时间为0时
		startDate.set(Calendar.HOUR_OF_DAY, 0);
		startDate.set(Calendar.MINUTE, 0);
		startDate.set(Calendar.SECOND, 0);
		endDate.set(Calendar.HOUR_OF_DAY, 0);
		endDate.set(Calendar.MINUTE, 0);
		endDate.set(Calendar.SECOND, 0);
		// 得到两个日期相差多少秒
		return ((int) (endDate.getTime().getTime() / 1000) - (int) (startDate.getTime().getTime() / 1000));
	}
	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
		MyCalendar d = new MyCalendar();
		System.out.println(yearsBetween("2016-8-15", "2017-3-1"));
		System.out.println(monthsBetween("2016-8-15", "2017-3-1"));
		System.out.println(daysBetween("2016-8-15", "2017-3-1"));

	}
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值