java中日期处理的一些工具方法

大家好,我是雄雄,欢迎关注微信公众号:雄雄的小课堂

前言

现在是2022年4月16日15:35:14!忙里偷闲,直接来看方法吧,写完文章继续去改Bug:

1.计算两个日期之间相差的天数

/**
	 * @param stratTime 开始时间
	 * @param endTime 结束时间
	 * @return 计算两个日期之间相差的天数
	 */
	public static Map<String, Object> dateDiff(Long stratTime, Long endTime) {
		Map<String, Object> map = new HashMap<>();
		Long diff = endTime - stratTime;
		Long day = diff / (24 * 60 * 60 * 1000);
		Long hour = (diff / (60 * 60 * 1000) - day * 24);
		Long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
		Long sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		map.put("day", day);
		map.put("hour", hour);
		map.put("min", min);
		map.put("sec", sec);
		return map;
	}

2.将时间戳转换成时间

	/**
	 * 将时间戳转换成时间
	 */
	public static Map<String, Object> formatTime(Long time) {
		Map<String, Object> map = new HashMap<>();
		Long day = time / (24 * 60 * 60 * 1000);
		Long hour = (time / (60 * 60 * 1000) - day * 24);
		Long min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
		Long sec = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		map.put("day", day);
		map.put("hour", hour);
		map.put("min", min);
		map.put("sec", sec);
		return map;
	}

3. 获取当前24小时的时间前后时间戳

	/**
	 * 将时间戳转换成时间
	 */
		public static Map<String, Object> getTimeChuo() {
			Map<String, Object> map = new HashMap<>();
			Long nowTime = System.currentTimeMillis();
			Long startTime = nowTime - 24 * 60 * 60 * 1000;
			Long endTime = nowTime + 24 * 60 * 60 * 1000;
			map.put("startTime", startTime);
			map.put("endTime", endTime);
		return map;
	}

4.由出生日期获得年龄

	public static int getAge(Date birthDay) throws Exception {
		Calendar cal = Calendar.getInstance();
		if (cal.before(birthDay)) {
			throw new IllegalArgumentException("出生日期小于当前时间,无效的日期!");
		}
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH);
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(birthDay);

		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		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;
	}

5.统计两个日期之间的所有日期

		/**
	 * 统计两个日期之间的所有日期
	 */
	public static List<String> getBeginTimeAndEndTime(Date beginTime, Date endTime) {
		List<String> listDate = new ArrayList<>();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(beginTime);
			while (calendar.getTime().before(endTime) || calendar.getTime().equals(endTime)) {
				listDate.add(dateFormat.format(calendar.getTime()));
				calendar.add(Calendar.DAY_OF_MONTH, 1);
			}
			return listDate;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listDate;
	}

6.根据生日获取年龄的另一种方法

	/**
	 * 根据日期获取年龄
	 */
	public static int getAgeByDate(Date date) throws Exception {
		Calendar cal = Calendar.getInstance();
		int thisYear = cal.get(Calendar.YEAR);
		int thisMonth = cal.get(Calendar.MONTH);
		int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(date);
		int birthYear = cal.get(Calendar.YEAR);
		int birthMonth = cal.get(Calendar.MONTH);
		int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

		int age = thisYear - birthYear;

		// 未足月
		if (thisMonth <= birthMonth) {
			// 当月
			if (thisMonth == birthMonth) {
				// 未足日
				if (dayOfMonth < birthdayOfMonth) {
					age--;
				}
			} else {
				age--;
			}
		}
		return age;
	}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穆雄雄

哎,貌似还没开张来着呢~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值