package com.consum.base.util; import org.apache.commons.lang3.StringUtils; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; /** * 功能描述: 时间处理 * * @author lxk * @date 2024/4/9 10:34 * @version 1.0 **/ public class DateUtil { /** * @author: hyf * @create: 2023/4/26 0026 * @description: 获取当前时间 * **/ public static Timestamp getNowTimestamp(){ return new Timestamp(System.currentTimeMillis()); } /** * 获取当前时间的long值 * * @return 返回当前时间的long值:例 20230101101010 */ public static Long getNowDate() { DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); long currentTimeMillis = System.currentTimeMillis(); Date date = new Date(currentTimeMillis); String format = df.format(date); return new Long(format); } /** * 把14位的时间long,转化时间字符串 * @param dateLong long时间14位 * @param format 要转的时间格式 * @return */ public static String getStrDateByLong(Long dateLong,String format) { String dateStr = dateLong.toString(); DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); DateFormat df2 = new SimpleDateFormat(format); try { Date parse = df.parse(dateStr); String format1 = df2.format(parse); return format1; } catch (ParseException e) { throw new RuntimeException(e); } } public static Long DateToLong(Date date) { DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String format = df.format(date); return new Long(format); } /** * 获取当前时分 * @return */ public static Long getNowTime4HHMM() { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmm"); LocalTime localTime = LocalTime.now(); String format = localTime.format(formatter); return Long.parseLong(format); } /** * 判断当前时间在某个区间 * @param startTime * @param endTime * @return */ public static Boolean betweenNowTime(Long startTime, Long endTime) { Long nowTime4HHMM = DateUtil.getNowTime4HHMM(); if (startTime <= nowTime4HHMM && endTime >= nowTime4HHMM) { return true; } return false; } /** * 时间添加天 * @param time * @param day * @return */ public static Long addDay(Long time, Long day) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); LocalDateTime localDateTime = LocalDateTime.parse(time.toString(time), formatter); LocalDateTime newTime = localDateTime.plusDays(day); String format = newTime.format(formatter); return Long.parseLong(format); } /** * 时间减少天 * @param time * @param day * @return */ public static Long subDay(Long time, Long day) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); LocalDateTime localDateTime = LocalDateTime.parse(time.toString(time), formatter); LocalDateTime newTime = localDateTime.minusDays(day); String format = newTime.format(formatter); return Long.parseLong(format); } private static final DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("yyyyMMdd"); private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); private static final SimpleDateFormat birthdayFormat = new SimpleDateFormat("yyyy-MM-dd"); /** * 方法描述: 日期类型格式转换 * eg:20240428163146 转换为 自定义日期格式 * **/ public static String longToStr(Long time,String fmt) { DateTimeFormatter defaultFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); if(StringUtils.isNotBlank(fmt)){ defaultFormatter = DateTimeFormatter.ofPattern(fmt); } if(time.toString().length() == 8) { LocalDate start = LocalDate.parse(time.toString(), dayFormatter); return start.format(defaultFormatter); } else { LocalDateTime start = LocalDateTime.parse(time.toString(), timeFormatter); return start.format(defaultFormatter); } } /** * 根据生日获取年龄 * eg:出生年月日=2000-04-30;当前时间=2024-04-29;则age=24 * @param birthday 出生日期 * @return */ private static int getAgeByBirth(String birthday) { int age = 0; try { Date date = birthdayFormat.parse(birthday); Calendar now = Calendar.getInstance(); // 当前时间 now.setTime(new Date()); Calendar birth = Calendar.getInstance(); birth.setTime(date); //如果传入的时间,在当前时间的后面,返回0岁 if (birth.after(now)) { age = 0; } else { age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR); if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) { age += 1; } } return age; } catch (Exception e) { e.getStackTrace(); return 0; } } /** * 根据年龄获取生日 * eg:age=24;当前时间=2024-04-30;则出生年月日=2000-04-30; * @param age 年龄 * @return */ public static LocalDate getBirthByAge(int age){ LocalDate localDate = LocalDate.now(); return localDate.minusYears(age); } /** * 获取当前日期 */ public static Long getNowTime(String fmt) { if(StringUtils.isBlank(fmt)){ fmt = "yyyyMMddHHmmss"; } DateTimeFormatter formatter = DateTimeFormatter.ofPattern(fmt); if(fmt.length() == 8) { LocalDate localTime = LocalDate.now(); String format = localTime.format(formatter); return Long.parseLong(format); } else { LocalDateTime localTime = LocalDateTime.now(); String format = localTime.format(formatter); return Long.parseLong(format); } } /** * 判断当前日期在某个区间 * @param startDate 开始日期 * @param endDate 截止日期 * @param fmt 自定义格式;默认=yyyyMMddHHmmss */ public static Boolean betweenNowDateTime(Long startDate, Long endDate,String fmt) { Long nowDate = DateUtil.getNowTime(fmt); if (startDate <= nowDate && endDate >= nowDate) { return true; } return false; } /** * 获取当前时间的long值 * @param fmt 自定义格式 * @return */ public static Long getNowDateLong(String fmt) { if(StringUtils.isBlank(fmt)) { fmt = "yyyyMMddHHmmss"; } DateFormat df = new SimpleDateFormat(fmt); long currentTimeMillis = System.currentTimeMillis(); Date date = new Date(currentTimeMillis); String format = df.format(date); return new Long(format); } public static void main(String aaa[]){ //System.out.println(getAgeByBirth("2025-04-30")); // System.out.println(betweenNowDateTime(20240429135709L,20240429170909L,"")); // System.out.println(getBirthByAge(23).toString()); // System.out.println(longToStr(20240515L,"yyyy-MM-dd")); System.out.println(getNowDateLong("yyyyMMdd")); } }