java计算两个时间差值

场景:
    java计算两个字符串时间差值和两个Date时间差值,判断两个时间大小
注意:
    计算机时间是1970年1月1日0点规定为时间起点.
1.示例

	/** 比较两个字符串时间大小 */
	public static int compareTwoTime(String time1, String time2) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		int flagValue = 0;
		try {
			Date date1, date2;
			date1 = simpleDateFormat.parse(time1);
			date2 = simpleDateFormat.parse(time2);
			long millisecond = date1.getTime() - date2.getTime();
			if (millisecond > 0) {
				flagValue = 1;
			} else if (millisecond < 0) {
				flagValue = -1;
			} else if (millisecond == 0) {
				flagValue = 0;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return (flagValue);
	}

	/** 表两个时间差 */
	public static int compareTwoTime(Date time1, Date time2) {
		int flagValue = 0;
		try {
			long millisecond = time1.getTime() - time2.getTime();
			if (millisecond > 0) {
				flagValue = 1;
			} else if (millisecond < 0) {
				flagValue = -1;
			} else if (millisecond == 0) {
				flagValue = 0;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (flagValue);
	}

	/** 比较两个时间相差天数 */
	public static float calculateTimeGapDay(String time1, String time2) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");

		float day = 0;
		Date date1 = null;
		Date date2 = null;

		try {
			date1 = simpleDateFormat.parse(time1);
			date2 = simpleDateFormat.parse(time2);
			long millisecond = date2.getTime() - date1.getTime();
			day = millisecond / (24 * 60 * 60 * 1000);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return (day);
	}

	/** 比较两个时间相差天数 */
	public static float calculateTimeGapDay(Date time1, Date time2) {
		float day = 0;
		try {
			Date date1, date2;
			date1 = time1;
			date2 = time2;
			long millisecond = date2.getTime() - date1.getTime();
			day = millisecond / (24 * 60 * 60 * 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (day);
	}

	/** 比较两个时间相差小时 */
	public static double calculatetimeGapHour(String time1, String time2) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		double hour = 0;
		try {
			Date date1, date2;
			date1 = simpleDateFormat.parse(time1);
			date2 = simpleDateFormat.parse(time2);
			double millisecond = date2.getTime() - date1.getTime();
			hour = millisecond / (60 * 60 * 1000);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return hour;
	}

	/** 比较两个时间相差小时 */
	public static double calculatetimeGapHour(Date date1, Date date2) {
		double hour = 0;
		double millisecond = date2.getTime() - date1.getTime();
		hour = millisecond / (60 * 60 * 1000);
		return hour;
	}

	/** 比较两个时间相差分钟 */
	public static double calculatetimeGapMinute(String time1, String time2) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		double minute = 0;
		try {
			Date date1, date2;
			date1 = simpleDateFormat.parse(time1);
			date2 = simpleDateFormat.parse(time2);
			double millisecond = date2.getTime() - date1.getTime();
			minute = millisecond / (60 * 1000);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return minute;
	}

	/** 比较两个时间相差分钟 */
	public static double calculatetimeGapMinute(Date date1, Date date2) {
		double minute = 0;
		double millisecond = date2.getTime() - date1.getTime();
		minute = millisecond / (60 * 1000);
		return minute;
	}

	/** 比较两个时间相差秒 */
	public static double calculatetimeGapSecond(String time1, String time2) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		double second = 0;
		try {
			Date date1, date2;
			date1 = simpleDateFormat.parse(time1);
			date2 = simpleDateFormat.parse(time2);
			double millisecond = date2.getTime() - date1.getTime();
			second = millisecond / (1000);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return second;
	}

	/** 比较两个时间相差秒 */
	public static double calculatetimeGapSecond(Date date1, Date date2) {
		double second = 0;
		double millisecond = date2.getTime() - date1.getTime();
		second = millisecond / (1000);
		return second;
	}
	

以上,TKS.

  • 8
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值