package com.teradata.springboot.util;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeUtil {
public static final String YYYY_MM_DD = "yyyyMMdd";
/**
* @param time 2030-04-03 20:00:00
* @return true
*/
public static boolean isFuture(String time) {
long now = System.currentTimeMillis();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
try {
date = simpleDateFormat.parse(time);
long ts = date.getTime();
return ts > now;
} catch (ParseException e) {
return false;
}
}
public static boolean isPast(String endTime) {
return !isFuture(endTime);
}
/**
* 判断现在的时间是否处于 begin-end之间
*
* @param begin 800
* @param end 2030
* @return 若处于8:00- 20:30之间,则返回true
*/
public static boolean nowIsInTimeRange(int begin, int end) {
int hHmm = Integer.parseInt(new SimpleDateFormat("HHmm").format(new Date()));
return begin <= hHmm && hHmm <= end;
}
/**
* 获取n秒前的yyyy-MM-dd HH:mm:ss 格式数据
*
* @param seconds 多少秒前
*/
public static String before(long seconds) {
Date date = new Date();
long time = date.getTime();
time -= seconds * 1000;
date.setTime(time);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
}
/**
* 获取n秒前的yyyy-MM-dd HH:mm:ss 格式数据
*
* @param now 从某个时刻开始 2020-10-10 10:00:00
* @param seconds 多少秒前
*/
public static String before(String now, long seconds) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date nowDate = simpleDateFormat.parse(now);
long time = nowDate.getTime();
time -= seconds * 1000;
nowDate.setTime(time);
return simpleDateFormat.format(nowDate);
}
/**
* 获取n秒后的yyyy-MM-dd HH:mm:ss 格式数据
*
* @param now 从某个时刻开始 2020-10-10 10:00:00
* @param seconds 多少秒前
*/
public static String after(String now, long seconds) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date nowDate = simpleDateFormat.parse(now);
long time = nowDate.getTime();
time += seconds * 1000;
nowDate.setTime(time);
return simpleDateFormat.format(nowDate);
}
/**
* 若为 0点到12点,(不包括0点包括12点) 则返回当天0点
* 若为 12点到24点,(不包括12点包括0点) 则返回当天12点
*
* @param time time
*/
public static String getStartHalfDay(String time) throws ParseException {