java中时间的往前推N小时

/**
	 * 获取八小时之前的结束时间
	 * @return
	 */
		public static String getDateRoll(){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.HOUR,-8);

			calendar.set(Calendar.MINUTE,59);
			calendar.set(Calendar.SECOND,59);
			calendar.set(Calendar.MILLISECOND,999);

			Date time = calendar.getTime();
			String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);

			return format;
		}

	/**
	 * 获取八小时之前的结束时间,然后往前推二十四个小时
	 * @return
	 */
	public static String getDateRollBeforeDay(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.HOUR,-8);

		calendar.add(Calendar.HOUR,-24);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);

		Date time = calendar.getTime();
		String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);

		return format;
	}

	/**
	 * 获取八小时之前的结束时间,然后往前推十六个小时
	 * @return
	 */
	public static String getDateRollBeforeSixTeen(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.HOUR,-8);

		calendar.add(Calendar.HOUR,-16);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);

		Date time = calendar.getTime();
		String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);

		return format;
	}

 

你可能感兴趣的:(工具使用)