package com.jilongda.common.utils;
|
|
import com.jilongda.common.basic.Constant;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.time.*;
|
import java.time.format.DateTimeFormatter;
|
import java.util.*;
|
|
/**
|
* @author xiaochen
|
* @ClassName DateUtils
|
* @Description
|
* @date 2021-05-25 11:02
|
*/
|
public class TimeUtils {
|
/**
|
* 时间格式(yyyy-MM-dd)
|
*/
|
public final static String DATE_PATTERN = "yyyy-MM-dd";
|
/**
|
* 时间格式(yyyy-MM-dd HH:mm:ss)
|
*/
|
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
|
/**
|
* @param
|
* @return java.lang.String
|
* @author FEIYC
|
* @date 2021/6/25 14:56
|
* @description 获取流水号日期
|
*/
|
public static String getStreamCodeData() {
|
SimpleDateFormat dateStreamCodeFormat = new SimpleDateFormat(Constant.DATESTREAMCODE);
|
return dateStreamCodeFormat.format(new Date());
|
}
|
|
|
/**
|
* @param
|
* @return java.lang.String
|
* @author feiyunchuan
|
* @date 2021/7/9 17:21
|
* @description 获取今日日期
|
*/
|
public static String getTodayDate() {
|
SimpleDateFormat dateStreamCodeFormat = new SimpleDateFormat(Constant.DATE_FORMATTER_DATE);
|
return dateStreamCodeFormat.format(new Date());
|
}
|
|
/**
|
* @param
|
* @return java.lang.String
|
* @author FEIYC
|
* @date 2021/6/25 14:56
|
* @description 获取流水号时间
|
*/
|
public static String getStreamCodeDataTime() {
|
SimpleDateFormat dateStreamCodeFormat = new SimpleDateFormat(Constant.DATETIMESTREAMCODE);
|
return dateStreamCodeFormat.format(new Date());
|
}
|
|
/**
|
* 仓库创建时间
|
*
|
* @return
|
*/
|
public static String getCreateTime() {
|
SimpleDateFormat dateFormat = new SimpleDateFormat(Constant.DATE_FORMATTER_TIME);
|
return dateFormat.format(new Date());
|
}
|
|
/**
|
* 获取两个日期之间所有的天数集合
|
*
|
* @param startTime
|
* @param endTime
|
* @return:YYYY-MM
|
*/
|
public static List<String> getDayList(String startTime, String endTime, String formatStr) {
|
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
|
// 声明保存日期集合
|
List<String> list = new ArrayList<String>();
|
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) {
|
throw new RuntimeException("日期格式转换错误");
|
}
|
return list;
|
}
|
|
/**
|
* 获取日期的24小时制
|
*
|
* @param date
|
* @return:YYYY-MM
|
*/
|
public static List<String> getDayHourList(Date date, String resultFormatStr) {
|
SimpleDateFormat sdf = new SimpleDateFormat(Constant.DATE_FORMATTER_DATE);
|
// 声明保存日期集合
|
List<String> list = new ArrayList<>(24);
|
try {
|
String dateStr;
|
Date d;
|
SimpleDateFormat sdfd = new SimpleDateFormat(Constant.DATE_FORMATTER_DATE_HOUR);
|
SimpleDateFormat resSdf = new SimpleDateFormat(resultFormatStr);
|
for (int i = 0; i <= 23; i++) {
|
if (String.valueOf(i).length() == 0) {
|
dateStr = sdf.format(date) + " 0" + i;
|
} else {
|
dateStr = sdf.format(date) + " " + i;
|
}
|
d = sdfd.parse(dateStr);
|
list.add(resSdf.format(d));
|
}
|
} catch (ParseException e) {
|
throw new RuntimeException("日期格式转换错误");
|
}
|
return list;
|
}
|
|
|
/***
|
* 获取日期的24小时制
|
* @author xiaochen
|
* @date 2021-12-30 19:51
|
* @return java.util.List<java.lang.String>
|
*/
|
public static List<String> getDayHourList() {
|
List<String> times = new ArrayList<>(24);
|
for (int i = 0; i <= 23; i++) {
|
if (i < 10) {
|
times.add("0" + i + ":00");
|
} else {
|
times.add(i + ":00");
|
}
|
}
|
return times;
|
}
|
|
/**
|
* 将时间戳字符串转为Date类型
|
*
|
* @param timestamp 时间戳
|
* @return Date日期
|
* @throws ParseException 格式化异常
|
*/
|
public static Date getDateByTimestamp(String timestamp, String patternStr) {
|
SimpleDateFormat format = new SimpleDateFormat(patternStr);
|
Long times = Long.valueOf(timestamp);
|
String d = format.format(times);
|
try {
|
return format.parse(d);
|
} catch (ParseException e) {
|
throw new RuntimeException("字符串时间转换错误");
|
}
|
}
|
|
/**
|
* 将时间戳字符串转为Date类型
|
*
|
* @param time 时间
|
* @return Date日期
|
* @throws ParseException 格式化异常
|
*/
|
public static Date getDate(Date time, String patternStr) {
|
SimpleDateFormat format = new SimpleDateFormat(patternStr);
|
String d = format.format(time);
|
try {
|
return format.parse(d);
|
} catch (ParseException e) {
|
throw new RuntimeException("字符串时间转换错误");
|
}
|
}
|
|
/**
|
* 指定日期所在周的周一和周日时间
|
*
|
* @return 结果集
|
*/
|
public static Map<String, Date> getWeekDate(Date date) {
|
Map<String, Date> map = new HashMap<>(2);
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
|
cal.setFirstDayOfWeek(Calendar.MONDAY);
|
// 获得当前日期是一个星期的第几天
|
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
|
if (dayWeek == 1) {
|
dayWeek = 8;
|
}
|
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
|
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
|
Date mondayDate = cal.getTime();
|
cal.add(Calendar.DATE, 4 + cal.getFirstDayOfWeek());
|
Date sundayDate = cal.getTime();
|
map.put("first", mondayDate);
|
map.put("last", sundayDate);
|
return map;
|
}
|
|
/**
|
* 指定日期所在月的第一天/最后一天时间
|
*
|
* @return 结果集
|
*/
|
public static Map<String, Date> getMonthDate(Date date) {
|
Map<String, Date> map = new HashMap<>(2);
|
Calendar cal = Calendar.getInstance();
|
//设置指定日期
|
cal.setTime(date);
|
//获取当月第一天日期
|
int first = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
|
cal.set(Calendar.DAY_OF_MONTH, first);
|
Date firstDay = cal.getTime();
|
map.put("first", firstDay);
|
//获取当月最后一天日期
|
int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
cal.set(Calendar.DAY_OF_MONTH, last);
|
Date lastDay = cal.getTime();
|
map.put("last", lastDay);
|
return map;
|
}
|
|
/**
|
* 获取日期逻辑中所在的季度数
|
*
|
* @param date 当前日期
|
* @return 第几季度
|
*/
|
public static int getQuarterOfYear(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
return calendar.get(Calendar.MONTH) / 3 + 1;
|
}
|
|
/**
|
* 指定日期所在季度的第一天/最后一天时间
|
*
|
* @return 结果集
|
*/
|
public static Map<String, Date> getQuarterDate(Date date) {
|
Map<String, Date> map = new HashMap<>(2);
|
|
int quarter = getQuarterOfYear(date);
|
int year = dateToLocalDateTime(date).getYear();
|
|
int startMonth = (quarter - 1) * 3;
|
// 根据月获取开始时间
|
Calendar cal = Calendar.getInstance();
|
cal.set(Calendar.YEAR, year);
|
cal.set(Calendar.MONTH, startMonth);
|
cal.set(Calendar.DAY_OF_MONTH, 1);
|
cal.set(Calendar.HOUR, 0);
|
cal.set(Calendar.MINUTE, 0);
|
cal.set(Calendar.SECOND, 0);
|
Date first = cal.getTime();
|
map.put("first", first);
|
|
int lastMonth = quarter * 3 - 1;
|
// 根据月获取结束时间
|
cal = Calendar.getInstance();
|
cal.set(Calendar.YEAR, year);
|
cal.set(Calendar.MONTH, lastMonth);
|
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
cal.set(Calendar.HOUR, 0);
|
cal.set(Calendar.MINUTE, 0);
|
cal.set(Calendar.SECOND, 0);
|
Date last = cal.getTime();
|
map.put("last", last);
|
|
return map;
|
}
|
|
/**
|
* 指定日期所在半年的第一天/最后一天时间
|
*
|
* @return 结果集
|
*/
|
public static Map<String, LocalDateTime> getHalfYearDate(Date date) {
|
Map<String, LocalDateTime> map = new HashMap<>(2);
|
|
int quarter = getQuarterOfYear(date);
|
int year = dateToLocalDateTime(date).getYear();
|
if(quarter == 1 || quarter == 2){
|
map.put("first", LocalDateTime.of(year, 1, 1, 0, 0, 0));
|
map.put("last", LocalDateTime.of(year, 6, 30, 23, 59, 59));
|
}else {
|
map.put("first", LocalDateTime.of(year, 7, 1, 0, 0, 0));
|
map.put("last", LocalDateTime.of(year, 12, 31, 23, 59, 59));
|
}
|
return map;
|
}
|
|
/**
|
* 指定日期所在年的第一天/最后一天时间
|
*
|
* @return 结果集
|
*/
|
public static Map<String, Date> getYearDate(Date date) {
|
Map<String, Date> map = new HashMap<>(2);
|
Calendar cal = Calendar.getInstance();
|
//设置指定日期
|
cal.setTime(date);
|
//获取本年第一天日期
|
int first = cal.getActualMinimum(Calendar.DAY_OF_YEAR);
|
cal.set(Calendar.DAY_OF_YEAR, first);
|
Date firstDay = cal.getTime();
|
map.put("first", firstDay);
|
//获取本年最后一天日期
|
int last = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
|
cal.set(Calendar.DAY_OF_YEAR, last);
|
Date lastDay = cal.getTime();
|
map.put("last", lastDay);
|
return map;
|
}
|
|
/**
|
* 分别获取日期中的年月日
|
*
|
* @param date 需要获取的日期
|
* @return 结果集合
|
*/
|
public static Map<String, String> getYearMonthDay(Date date) {
|
String year = String.format("%tY", date);
|
String month = String.format("%tm", date);
|
String day = String.format("%td", date);
|
Map<String, String> result = new HashMap<>(3);
|
result.put("year", year);
|
result.put("month", month);
|
result.put("day", day);
|
result.put("time", String.valueOf(date.getTime()));
|
return result;
|
}
|
|
/**
|
* 获取指定日期字符串的LocalDateTime
|
* String转LocalDateTime
|
*
|
* @param time 日期字符串
|
* @return 结果LocalDateTime
|
*/
|
public static LocalDateTime getLocalDateTime(String time) {
|
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
|
LocalDate localDate = LocalDate.parse(time, dateTimeFormatter);
|
Date date = Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
|
return date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
|
}
|
|
|
/**
|
* 将时间戳字符串转为LocalDateTime类型
|
*
|
* @param time 时间戳
|
* @return LocalDateTime
|
* @throws ParseException 格式化异常
|
*/
|
public static LocalDateTime getLocalDateTimeByTimeStamp(String time) {
|
LocalDateTime localDateTime = Instant.ofEpochMilli(Long.parseLong(time)).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
|
return localDateTime;
|
}
|
|
|
/**
|
* Date转为LocalDateTime
|
*
|
* @param date 日期
|
* @return LocalDateTime
|
*/
|
public static LocalDateTime dateToLocalDateTime(Date date) {
|
Instant instant = date.toInstant();
|
ZoneId zoneId = ZoneId.systemDefault();
|
return instant.atZone(zoneId).toLocalDateTime();
|
}
|
|
/**
|
* LocalDateTime转Date
|
*
|
* @param dateTime 日期
|
* @return Date
|
*/
|
public static Date localDateTimeToDate(LocalDateTime dateTime) {
|
ZoneId zoneId = ZoneId.systemDefault();
|
ZonedDateTime zdt = dateTime.atZone(zoneId);
|
return Date.from(zdt.toInstant());
|
}
|
|
/**
|
* 获取当前时间到凌晨0点的秒数
|
*
|
* @return 当前时间到凌晨0点的秒数
|
*/
|
public static long getSecondsFromNowToAm() throws ParseException {
|
long now = System.currentTimeMillis();
|
SimpleDateFormat sdfOne = new SimpleDateFormat("yyyy-MM-dd");
|
return (now - (sdfOne.parse(sdfOne.format(now)).getTime())) / 1000;
|
}
|
|
/**
|
* 根据身份证获取年龄
|
*
|
* @param idCard 身份证件号
|
* @return 年龄
|
*/
|
public static int getAge(String idCard) {
|
int year = Integer.parseInt(idCard.substring(6, 10));
|
int date = Integer.parseInt(idCard.substring(10, 14));
|
Map<String, String> yearMonthDay = getYearMonthDay(new Date());
|
int currYear = Integer.parseInt(yearMonthDay.get("year"));
|
int currDate = Integer.parseInt(yearMonthDay.get("month") + yearMonthDay.get("day"));
|
int age = currYear - year;
|
if (currDate < date) {
|
age -= 1;
|
}
|
return age;
|
}
|
|
|
/**
|
* 根据身份证获取年龄
|
*
|
* @param idCard 身份证件号
|
* @return 年龄
|
*/
|
public static int getSex(String idCard) {
|
int sex = Integer.parseInt(idCard.substring(16, 17));
|
if (sex % 2 == 1) {
|
sex = 1;
|
} else {
|
sex = 2;
|
}
|
return sex;
|
}
|
|
/**
|
* 根据身份证获取生日
|
*
|
* @param idCard 身份证号码
|
* @return 生日
|
*/
|
public static LocalDateTime getBirthday(String idCard) {
|
String year = idCard.substring(6, 10);
|
String month = idCard.substring(10, 12);
|
String day = idCard.substring(12, 14);
|
String birthday = year + Constant.SEP_LINE + month + Constant.SEP_LINE + day + " 00:00:00";
|
return getLocalDateTime(birthday);
|
}
|
|
/**
|
* 计算两个日期之间相差的天数
|
*
|
* @param smdate 较小的时间
|
* @param bdate 较大的时间
|
* @return 相差天数
|
*/
|
public static int daysBetween(Date smdate, Date bdate) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(smdate);
|
long time1 = cal.getTimeInMillis();
|
cal.setTime(bdate);
|
long time2 = cal.getTimeInMillis();
|
long days = (time2 - time1) / (1000 * 3600 * 24);
|
return Integer.parseInt(String.valueOf(days));
|
}
|
|
/**
|
* localdatetime转为字符串
|
*
|
* @param time localdatetime
|
* @return 字符串
|
*/
|
public static String localDateTimeToString(LocalDateTime time) {
|
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
return df.format(time);
|
}
|
|
/**
|
* localdate转为字符串
|
*
|
* @param time localdate
|
* @return 字符串
|
*/
|
public static String localDateToString(LocalDate time) {
|
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
return df.format(time);
|
}
|
|
/**
|
* localdatetime转为年月日字符串
|
*
|
* @param time localdatetime
|
* @return 字符串
|
*/
|
public static String localDateTimeToYMDString(LocalDateTime time) {
|
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
return df.format(time);
|
}
|
|
/**
|
* 获取当天的00:00:00
|
*
|
* @return
|
*/
|
public static LocalDateTime getDayStart(LocalDateTime time) {
|
return time.with(LocalTime.MIN);
|
}
|
|
|
/**
|
* 获取当天的23:59:59
|
*
|
* @return
|
*/
|
public static LocalDateTime getDayEnd(LocalDateTime time) {
|
return time.with(LocalTime.MAX);
|
}
|
|
/**
|
* 获取指定日期的当月第一天的0点
|
*
|
* @return
|
*/
|
public static String getDayStartString(Date now) {
|
Date date = getMonthDate(now).get("first");
|
LocalDateTime localDateTime = dateToLocalDateTime(date);
|
LocalDateTime dayStart = getDayStart(localDateTime);
|
String start = localDateTimeToString(dayStart);
|
return start;
|
}
|
|
/**
|
* 获取指定日期的上月第一天的0点
|
*
|
* @return
|
*/
|
public static String getLastStartString(Date now) {
|
Date date = getMonthDate(now).get("first");
|
LocalDateTime localDateTime = dateToLocalDateTime(date);
|
LocalDateTime LstEnd = getDayStart(localDateTime).plusMonths(-1L);
|
return localDateTimeToString(LstEnd);
|
}
|
|
|
/**
|
* 计算两个时间的时间差(时:分)
|
*
|
* @param startTime
|
* @param endTime
|
* @return
|
*/
|
public static String getTodayLocalTimeDiff(LocalTime startTime, LocalTime endTime) {
|
try {
|
// 获取结束时间的小时与分钟
|
int endTimeHour = endTime.getHour();
|
int endTimeMinute = endTime.getMinute();
|
// 获取开始时间的小时与分钟
|
int startTimeHour = startTime.getHour();
|
int startTimeMinute = startTime.getMinute();
|
// 计算时间差
|
int hour = Math.abs(endTimeHour - startTimeHour);
|
int minute = Math.abs(endTimeMinute - startTimeMinute);
|
if (endTimeMinute < startTimeMinute) {
|
hour = hour - 1;
|
minute = 60 - minute;
|
}
|
return hour + ":" + minute;
|
} catch (Exception e) {
|
System.out.println(e.getMessage());
|
}
|
return null;
|
}
|
|
/**
|
* 时间格式(时:分)转分钟
|
*
|
* @return
|
*/
|
public static int getHourToMinute(String time) {
|
try {
|
// 获取结束时间的小时与分钟
|
String[] split = time.split(Constant.SEP_COLON);
|
String hour = split[0];
|
String minute = split[1];
|
// 计算时间差
|
int minutes = Integer.parseInt(hour) * 60;
|
return minutes + Integer.parseInt(minute);
|
} catch (Exception e) {
|
System.out.println(e.getMessage());
|
}
|
return 0;
|
}
|
}
|