日期处理工具类

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 {
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
int hHmm = Integer.parseInt(new SimpleDateFormat("HHmm").format(date));
if (hHmm <= 12_00) {
return new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(date);
} else {
return new SimpleDateFormat("yyyy-MM-dd 12:00:00").format(date);
}
}

/**
* 若为 0点到12点,(不包括0点包括12点) 则返回当天12
* 若为 12点到24点,(不包括12点包括0点) 则返回次天0
*
* @param time time
*/
public static String getEndHalfDay(String time) throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
int hHmm = Integer.parseInt(new SimpleDateFormat("HHmm").format(date));
if (hHmm <= 12_00) {
return new SimpleDateFormat("yyyy-MM-dd 12:00:00").format(date);
} else {
String format = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(date);
Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format);
long l = parse.getTime() + 24 * 60 * 60 * 1000;
parse.setTime(l);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(parse);
}
}

public static String timeChange(String time, String beforeFormat, String afterFormat) throws ParseException {
Date parse;
parse = new SimpleDateFormat(beforeFormat).parse(time);
return new SimpleDateFormat(afterFormat).format(parse);

}


/**
* 获取昨天0点的数据
*
* @param today 2020/10/11 12:10:00
* @return 2020/10/10 00:00:00
*/
public static String getYesterday0OClock(String today) throws ParseException {
Date date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(today);
date.setTime(date.getTime() - 24 * 60 * 60 * 1000);
return new SimpleDateFormat("yyyy/MM/dd 00:00:00").format(date);
}

/**
* 获取七天前0点的数据
*
* @param today 2020/10/11 12:10:00
* @return 2020/10/10 00:00:00
*/
public static String getLastWeek0OClock(String today) throws ParseException {
Date date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(today);
date.setTime(date.getTime() - 7 * 24 * 60 * 60 * 1000);
return new SimpleDateFormat("yyyy/MM/dd 00:00:00").format(date);
}


/**
* 获取今天0点的数据
*
* @param today 2020/10/10 12:10:00
* @return 2020/10/10 00:00:00
*/
public static String getToday0OClock(String today) throws ParseException {
Date date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(today);
date.setTime(date.getTime() - 24 * 60 * 60 * 1000);
return new SimpleDateFormat("yyyy/MM/dd 00:00:00").format(date);
}

public static String after(int i) {
return before(-i);
}

public static String getOfsetMonyyyyMMdd(int ofset) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
Date parse = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(parse);
cal.add(Calendar.MONTH, -1 * ofset);

return sdf.format(cal.getTime());
}
public static String getYesterdayByyyyyMMMdd(String day) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
Date date = sdf.parse(day);
return DateUtil.offsetDay(date, -1).toString(YYYY_MM_DD);
}

public static String getTodayByyyyyMMdd() {
return DateUtil.format(DateUtil.date(), "yyyyMMdd");
}

public static String getYesterdayByyyyyMMMdd(){
return DateUtil.format(DateUtil.yesterday(), "yyyyMMdd"); }


public static String getTodayByyyyy_MM_dd(){
return DateUtil.format(DateUtil.date(),"yyyy-MM-dd"); }


public static String getToday(){
return DateUtil.format(DateUtil.date(),"yyyy-MM-dd HH:mm:ss"); }

public static String getDay(){
return DateUtil.format(DateUtil.date(),"yyyyMMdd"); }


/**
* 根据时间戳去生成年月日
*
* @param offset 偏移天数
* @return
*/
public static long getYYYYMMddByOffset(long offset) {
long timeStamp = System.currentTimeMillis();
long l = timeStamp + offset * DAY;
return getYYYYMMddByTimeStamp(l); }




public static String getYYYYMMByOffset(int i){
DateTime dataTime=DateUtil.offset(DateUtil.date(), DateField.MONTH,i);
return DateUtil.format(dataTime,"yyyyMM"); }

/**
* 根据时间戳去生成年月日
*
* @param timeStamp 1579154871974
* @return 20200116
*/
public static long getYYYYMMddByTimeStamp(long timeStamp) {
Date date = new Date();
date.setTime(timeStamp);
return Long.parseLong(new SimpleDateFormat(YYYY_MM_DD).format(date)); }


public static String getOfsetMonyyyyMMdd2(int ofset) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
Date parse = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(parse);
cal.add(Calendar.MONTH, -1 * ofset);

return sdf.format(cal.getTime()); }




public static final long DAY = 1000 * 60 * 60 * 24;}

posted @ 2023-02-07 11:27  扬帆起航$  阅读(23)  评论(0)    收藏  举报