IntelligentRecognition/ah-jjsp-service/.svn/pristine/1b/1bb15db98455d169c0ffd61d3b6...

794 lines
24 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.securityControl.common.core.utils.aes;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.est.CACertsResponse;
import java.text.*;
import java.util.*;
/**
* 日期工具类
*/
@Slf4j
public class DateTimeHelper {
public static String format(Date d, String f) {
SimpleDateFormat df = new SimpleDateFormat(f, Locale.US);
return df.format(d);
}
public static String format(String timeLong, String defaut) {
try {
long tmpLong = Long.parseLong(timeLong);
Date d = new Date(timeLong);
String timeStr = format(d, "yyyy-MM-dd HH:mm:ss");
return timeStr;
} catch (Exception e) {
return defaut;
}
}
/**
* 获取当前时间
*
* @return
*/
public static String getNowTime() {
return format(new Date(), "yyyy-MM-dd HH:mm:ss");
}
/**
* 获取当前时间的年份
*
* @return
*/
public static String getNowYear() {
return format(new Date(), "yyyy");
}
public static String getYear(Date d) {
int year = d.getYear() + 1900;
return year + "";
}
/**
* 获取当前月份
*
* @return
*/
public static String getNowMonths() {
return format(new Date(), "MM");
}
/**
* 获取当前小时
*
* @return
*/
public static String getNowHours() {
return format(new Date(), "HH");
}
public static String getNowDMS() {
String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
return time;
}
public static String getNowYMD() {
String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
return time;
}
public static String getMonth(Date d) {
int month = d.getMonth() + 1;
return StringHelper.fillPrefixZero(month, 2);
}
public static String getNowDay() {
String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
return time;
}
public static String getTimesUPDown(String time){
try {
if(StringHelper.isEmpty(time)){
return time;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(time);
// 创建Calendar对象表示当前时间
Calendar calendar = Calendar.getInstance();
// 将Calendar对象设置为当前时间
calendar.setTime(date);
// 将时间减去5分钟
calendar.add(Calendar.MINUTE, -5);
calendar.add(Calendar.SECOND, -13);
// 获取减去5分钟后的时间
Date newTime = calendar.getTime();
String startTime = sdf.format(newTime);
return startTime;
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
public static String getNextDay(){
Calendar calendar = Calendar.getInstance();
// 对日期进行加1天的操作
calendar.add(Calendar.DAY_OF_MONTH, 1);
// 获取明天的年、月、日
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // 注意月份从0开始计算
int day = calendar.get(Calendar.DAY_OF_MONTH);
return year+"-"+month+"-"+day;
}
public static String getNowDayHMS() {
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
return time;
}
@SuppressWarnings("static-access")
public static String getHouDay() {
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
calendar.add(calendar.DATE, 1);// 把日期往后增加一天.整数往后推,负数往前移动
date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String day = sdf.format(date);
return day;
}
@SuppressWarnings("static-access")
public static String getYesTodayt() {
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
calendar.add(calendar.DATE, -1);// 把日期往后增加一天.整数往后推,负数往前移动
date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String day = sdf.format(date);
return day;
}
@SuppressWarnings("static-access")
public static String getQtDay() {
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
calendar.add(calendar.DATE, -2);// 把日期往后增加一天.整数往后推,负数往前移动
date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String day = sdf.format(date);
return day;
}
public static String format(String str, String format, String disFormat) {
Date d = parse(str, format);
return format(d, disFormat);
}
/**
* 格式化 时间
*
* @param format
* @return
*/
public static String format(Date d, DateFormat format) {
return format.format(d);
}
/**
* 格式化 时间
*
* @param format
* @return Date
* @throws ParseException
*/
public static Date parse(String dateStr, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
try {
return df.parse(dateStr);
} catch (ParseException e) {
return new Date();
}
}
public static Date parse(String dateStr, String format, Date d) {
try {
SimpleDateFormat df = new SimpleDateFormat(format);
return df.parse(dateStr);
} catch (Exception e) {
return d;
}
}
/**
* 解析时间
*
* @return Date
* @throws ParseException
*/
public static Date parse(String dateStr, DateFormat format) throws ParseException {
return format.parse(dateStr);
}
/**
* 获取当前时间的年月
*
* @return
*/
public static String getPrevMonth() {
return minusMonth(new Date(), 1);
}
/**
* 获取当前时间的上个年月
*
* @return
*/
public static String getNowMonth() {
return format(new Date(), "yyyy-MM");
}
/**
* 获取当前时间的年月日
*
* @return
*/
public static String getNowDate() {
return format(new Date(), "yyyy-MM-dd");
}
/**
* @return
*/
public static String minusMonth(Date date, int c) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
try {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -c);
return format(cal.getTime(), df);
} catch (Exception e) {
return format(date, df);
}
}
/**
* @param month
* @return
*/
public static String minusMonth(String month, int c) {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
Date date = parse(month, df);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -c);
return format(cal.getTime(), df);
} catch (Exception e) {
return month;
}
}
/**
* @return
*/
public static Date minus(Date d, int type, int c) {
try {
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.add(type, -c);
return cal.getTime();
} catch (Exception e) {
return new Date();
}
}
private static String[] digits = { "零","一", "二", "三", "四", "五", "六", "七", "八", "九"};
public static String getWeekTimes(String time) {
try{
int month=Integer.parseInt(time.split("-")[1].trim());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(time);
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(date);
int weekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
String msg=digits[weekOfMonth];
return month+"月" +msg+"周";
}catch (Exception e){
log.error(e.toString(),e);
}
return null;
}
/**
* 获取七周日期
* @return
*/
public static List<String> getWeekList() {
List<String> list=new ArrayList<>();
String time= DateTimeHelper.getNowDay();
// String startWeek= DateTimeHelper.getWeekStartOrEndTime(time,true);//本周开始时间
// String endWeek= DateTimeHelper.getWeekStartOrEndTime(time,false);//本周结束时间
for (int i=6;i>=1;i--) {
String timess = getDayADDorReduce(time,-7*i);
list.add(timess);
}
list.add(time);
return list;
}
public static void main(String[] args) {
// getWeekDataList("2023-08-01");
// System.err.println(getWeekTimes("2023-12-31"));
// System.err.println(getWeekTimes("2024-01-01"));
// System.err.println(getWeekTimes("2024-01-07"));
// System.err.println(getWeekTimes("2024-01-14"));
// System.err.println( getDayADDorReduce("2024-01-17",-7));
// System.err.println(getWeekStartOrEndTime("2023-09-01",true));
// System.err.println(getWeekStartOrEndTime("2023-09-01",false));
String time= DateTimeHelper.getNowDay();
String startWeek= DateTimeHelper.getWeekStartOrEndTime(time,true);//本周开始时间
String endWeek= DateTimeHelper.getWeekStartOrEndTime(time,false);//本周结束时间
for (int i=6;i>=1;i--) {
String timess = getDayADDorReduce("2024-01-17",-7*i);
System.err.println(timess);
System.err.println(getWeekTimes(timess));
}
}
/**
* 得到前一个月
*
* @param month
* @return
*/
public static String prevMonth(String month) {
return minusMonth(month, 1);
}
/**
* 得到前一个月
*
* @param month
* @return
*/
public static String prevMonth(String month, int c) {
return minusMonth(month, c);
}
public static float duration(String startTime, String stopTime) {
Date sd = DateTimeHelper.parse(startTime, "yyyy-MM-dd HH:mm");
long sl = sd.getTime();
Date ed = DateTimeHelper.parse(stopTime, "yyyy-MM-dd HH:mm");
long el = ed.getTime();
float l = el - sl;
if (l <= 0) {
l = 0;
}
l = l / (1000 * 60);
return l;
}
public static String getStartYearMonthBySeason(String year, int season) {
switch (season) {
case 1:
return getYearMonth(year, "01");
case 2:
return getYearMonth(year, "04");
case 3:
return getYearMonth(year, "07");
case 4:
return getYearMonth(year, "10");
default:
return getYearMonth(year, "01");
}
}
private static String getYearMonth(String year, String month) {
return year + "-" + month;
}
public static String getEndYearMonthBySeason(String year, int season) {
switch (season) {
case 1:
return getYearMonth(year, "03");
case 2:
return getYearMonth(year, "06");
case 3:
return getYearMonth(year, "09");
case 4:
return getYearMonth(year, "12");
default:
return getYearMonth(year, "03");
}
}
/**
* 获取本周的时间段 本周一至本周日
*
* @return
*/
public static String getCurrentWeekTime() {
String result = "";
String currentDay = getNowDay();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
Calendar cal = Calendar.getInstance();
Date time;
try {
time = sdf.parse(currentDay);
cal.setTime(time);
System.out.println("要计算日期为:" + sdf.format(cal.getTime())); //输出要计算日期
//判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);//根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
System.out.println("所在周一的日期:" + sdf.format(cal.getTime()));
result += sdf.format(cal.getTime());
System.out.println(cal.getFirstDayOfWeek() + "-" + day + "+6=" + (cal.getFirstDayOfWeek() - day + 6));
cal.add(Calendar.DATE, 6);
result += "," + sdf.format(cal.getTime());
System.out.println("所在下周日的日期:" + sdf.format(cal.getTime()));
return result;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取下周的开始时间和结束时间
*
* @return
*/
public static String getNextWeekTime() {
Date date = getBeginDayOfWeek();
String time = format(date, "yyyy-MM-dd");
Date date2 = getEndDayOfWeek();
String time2 = format(date2, "yyyy-MM-dd");
return time + "," + time2;
}
public static Date getBeginDayOfWeek() {
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.WEEK_OF_MONTH, 1);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
return cal.getTime();
}
/**
* 获取前一周的开始和结束时间
* @return
*/
public static String getPrevWeek(){
Date date = getBeginDayOfPrevWeek();
String time = format(date, "yyyy-MM-dd");
Date date2 = getEndDayOfPrevWeek();
String time2 = format(date2, "yyyy-MM-dd");
return time + "," + time2;
}
//获取前一周的开始时间
public static Date getBeginDayOfPrevWeek() {
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.WEEK_OF_MONTH, -1);
if (dayofweek == 1) {
dayofweek -= 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
return cal.getTime();
}
public static Date getEndDayOfPrevWeek() {
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfPrevWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return weekEndSta;
}
/**
* 获取指定时间 当前周的周一至周日的时间
* @return
*/
public static List<String> getWeekDataList(String time){
List<String> week = new ArrayList<String>();
try{
if(StringHelper.isEmpty(time)){
time=DateTimeHelper.getNowDay();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dataTime = sdf.parse(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dataTime);
// 如果是周日
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
calendar.add(Calendar.DAY_OF_YEAR,-1);
}
// 获取当前日期是当周的第i天
int i = calendar.get(Calendar.DAY_OF_WEEK) - 1;
// 获取当前日期所在周的第一天
calendar.add(Calendar.DATE , -i+1);
for (int j = 0; j < 7; j++) {
if(j >0){
calendar.add(Calendar.DATE , 1);
}
week.add(sdf.format(calendar.getTime()));
System.err.println(sdf.format(calendar.getTime()));
}
return week;
}catch (Exception e){
log.error(e.toString(),e);
}
return week;
}
/**
* 仁义日期加减
* @return
*/
public static String getDayADDorReduce(String time,int num){
String returnTime=null;
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dataTime = sdf.parse(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dataTime);
// 如果是周日
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
calendar.add(Calendar.DAY_OF_YEAR,-1);
}
// 获取当前日期是当周的第i天
// int i = calendar.get(Calendar.DAY_OF_WEEK) - 1;
// // 获取当前日期所在周的第一天
// calendar.add(Calendar.DATE , -i+1);
calendar.add(Calendar.DATE , num);
return sdf.format(calendar.getTime());
}catch (Exception e){
log.error(e.toString(),e);
}
return returnTime;
}
/**
* 周开始和周结束时间
* @param time
* @param isStart
* @return
*/
public static String getWeekStartOrEndTime(String time,boolean isStart){
String returnTime=null;
try{
if(StringHelper.isEmpty(time)){
time=DateTimeHelper.getNowDay();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dataTime = sdf.parse(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dataTime);
// 如果是周日
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
calendar.add(Calendar.DAY_OF_YEAR,-1);
}
// 获取当前日期是当周的第i天
int i = calendar.get(Calendar.DAY_OF_WEEK) - 1;
// 获取当前日期所在周的第一天
calendar.add(Calendar.DATE , -i+1);
if(isStart){
return sdf.format(calendar.getTime());
}else{
calendar.add(Calendar.DATE , 6);
return sdf.format(calendar.getTime());
}
}catch (Exception e){
log.error(e.toString(),e);
}
return returnTime;
}
/**
* 获取当前日期是多少周 周
* @return
*/
public static int getNowWeeks(String time){
try{
Date date = new Date();
if(StringHelper.isNotEmpty(time)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(time);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 获取一年中的第几周
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
return weekOfYear;
} catch (Exception e){
log.error(e.toString(),e);
return 0;
}
}
/**
* 获取前一天的日期
*
* @return
*/
public static String getFrontDay() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
Calendar now = Calendar.getInstance();
now.setTime(date);
now.add(Calendar.DAY_OF_MONTH, -1);
return sdf.format(now.getTime());
}
public static Date getEndDayOfWeek() {
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return weekEndSta;
}
/**
* 获取两个时间之间的所有日期
*
* @param startTime
* @param endTime
* @return
*/
public static List<String> getBetweenDate(String startTime, String endTime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> list = new ArrayList<>();
try {
//转化日期类型
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
//用calendar 进行日期比较判断
Calendar calendar = Calendar.getInstance();
while (startDate.getTime() <= endDate.getTime()) {
//把日期添加到集合中
list.add(sdf.format(startDate));
//设置日期
calendar.setTime(startDate);
//把日期加一天
calendar.add(Calendar.DATE, 1);
//获取增加后的日期
startDate = calendar.getTime();
}
} catch (ParseException e) {
e.printStackTrace();
}
return list;
}
/**
* @return float
* @Author ccw
* @Description 秒换算成小时
* @Date 14:08 2022/6/24
* @Param [seconds]
*/
public static String getHours(int seconds) {
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMaximumFractionDigits(3); //保留几位小数填写几
String hours = numberFormat.format((float) seconds / (float) 3600);
return hours;
}
/**
* @return int
* @Author ccw
* @Description 时间差
* @Date 14:05 2022/6/24
* @Param [startTime, endTime]
*/
public static int getTimeDifference2(String startTime, String endTime) throws ParseException {
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleFormat.parse(startTime);
long time = date.getTime();
Date date2 = simpleFormat.parse(endTime);
long time2 = date2.getTime();
long timestamp = time2 - time;
int seconds = (int) (timestamp / 1000);
return seconds;
}
/**
* 小时差
* @param endTime
* @param startTime
* @return
* @throws ParseException
*/
public static Double getTimeHours(String endTime, String startTime) throws ParseException {
try{
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date st = simpleFormat.parse(startTime);
Date et = simpleFormat.parse(endTime);
long time = et.getTime()-st.getTime();
DecimalFormat decimalFormat = new DecimalFormat("0.00");
String format = decimalFormat.format(time * 1.0 / 3600000 * 1.0);
return Double.valueOf(format);
}catch (Exception e){
return 0.0;
}
}
/**
* 分钟差
* @param endTime
* @param startTime
* @return
* @throws ParseException
*/
public static Long getTimeMine(String endTime, String startTime) throws ParseException {
try{
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date st = simpleFormat.parse(startTime);
Date et = simpleFormat.parse(endTime);
long time = et.getTime()-st.getTime();
Double times=Double.parseDouble(time+"");
Double b=times/1000/60;
long hours=time/1000/60;
return hours;
}catch (Exception e){
return 0L;
}
}
}