1、指定时间之前到00:00

	/**
	 * 获取指定格式的上个小时的Date 00:00
	 * @param i   > 0 i小时之后的数据,< 0 则是i之前时间
	 * @return
	 */
	public static Date getBeforeHourDate(int i) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + i);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		return calendar.getTime();
	}

2、指定时间之前到59:59

	/**
	 * 获取指定格式的上个小时的Date 的59:59
	 */
	public static Date getBeforeHourEndDate(int i) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + i);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MINUTE, 59);
		return calendar.getTime();
	}

应用: 场景假设: 当前时间16:03:00,我要获取上一个小时的时间段,即15:00:00-15:59:59

 Date beginTime = TimeUtils.getBeforeHourDate(-1); //15:00:00
 Date endTime = TimeUtils.getBeforeHourEndDate(-1); //15:59:59
  • 欢迎加入 CSDN技术交流群:QQ群:681223095,方便问题讨论。博主不一定长期在线,但是qq群里会有很多热心的小伙伴,大家一起讨论解决问题。
    关注公众号,更多学习内容给予推送,争取每日更新

    获取指定格式的上几小时的Date

相关文章:

  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2022-02-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-08
  • 2021-12-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案