| @ -0,0 +1,823 @@ | |||
| package org.jeecg.common.util; | |||
| import java.math.BigDecimal; | |||
| import java.sql.Timestamp; | |||
| import java.text.DateFormat; | |||
| import java.text.ParseException; | |||
| import java.text.SimpleDateFormat; | |||
| import java.time.*; | |||
| import java.time.format.DateTimeFormatter; | |||
| import java.time.temporal.ChronoUnit; | |||
| import java.time.temporal.TemporalAdjusters; | |||
| import java.util.Calendar; | |||
| import java.util.Date; | |||
| import java.util.TimeZone; | |||
| /** | |||
| 基于JDK 1.8 日期API | |||
| @Author: hy | |||
| @ClassName: DateUtils | |||
| @Date: 2019/11/25下午5:34 | |||
| @Description: | |||
| */ | |||
| public class DateUtils2 { | |||
| private static String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss"; | |||
| private static String YYYYMMDD = "yyyy-MM-dd"; | |||
| public static String DAY_FORMAT = "yyyy-MM-dd"; | |||
| private static String YYYY = "yyyy"; | |||
| private static String MM = "MM"; | |||
| private static String YYYYMM = "yyyy-MM"; | |||
| private static String HHMMSS = "HH:mm:ss"; | |||
| private static String YYYYMMDD2 = "yyyyMMdd"; | |||
| private static String YYYYMMDDHHMMSS2 = "yyyyMMddHHmmss"; | |||
| private static String YYYYMMDD3 = "yyyy年MM月dd日"; | |||
| private static String DD = "dd"; | |||
| public static String DEFUALT_TIME_START =" 00:00:00"; | |||
| public static String DEFUALT_TIME_END =" 23:59:59"; | |||
| /** | |||
| * 获取当前时间 yyyy-mm-dd HH:mm:ss | |||
| * @return | |||
| */ | |||
| public static String getNowDate(){ | |||
| //获取当前时间 | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS); | |||
| return dateTimeFormatter.format(localDateTime); | |||
| } | |||
| /** | |||
| * 获取当前年份 yyyy | |||
| * @return | |||
| */ | |||
| public static String getNowYear(){ | |||
| //获取当前时间 | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(YYYY); | |||
| return dateTimeFormatter.format(localDateTime); | |||
| } | |||
| /** | |||
| * 获取当前时间 dd | |||
| * @return | |||
| */ | |||
| public static String getNowDay(){ | |||
| //获取当前时间 | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DD); | |||
| return dateTimeFormatter.format(localDateTime); | |||
| } | |||
| /** | |||
| * 获取当前日期 yyyymmdd | |||
| * @return | |||
| */ | |||
| public static String getNowDateString(){ | |||
| //获取当前时间 | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(YYYYMMDD2); | |||
| return dateTimeFormatter.format(localDateTime); | |||
| } | |||
| /** | |||
| * 获取当前日期 yyyymmdd | |||
| * @return | |||
| */ | |||
| public static String getNowDateString2(){ | |||
| //获取当前时间 | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(YYYYMMDD3); | |||
| return dateTimeFormatter.format(localDateTime); | |||
| } | |||
| /** | |||
| * 获取当前日期 yyyymmddHHMMss | |||
| * @return | |||
| */ | |||
| public static String getNowDateStrings(){ | |||
| //获取当前时间 | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS2); | |||
| return dateTimeFormatter.format(localDateTime); | |||
| } | |||
| /** | |||
| * 格式化 HH:mm:ss | |||
| * @return | |||
| */ | |||
| public static String getNowTime(long time){ | |||
| //获取当前时间 | |||
| LocalDateTime localDateTime = Instant.ofEpochMilli(time).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime(); | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(HHMMSS); | |||
| return dateTimeFormatter.format(localDateTime); | |||
| } | |||
| /** | |||
| * 获取当前时间 yyyy-mm-dd | |||
| * @return | |||
| */ | |||
| public static String getTodayDate(){ | |||
| //获取当前时间 | |||
| LocalDateTime localDateTime = LocalDateTime.now().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime(); | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(YYYYMMDD); | |||
| return dateTimeFormatter.format(localDateTime); | |||
| } | |||
| /** | |||
| * 获取当前年月 yyyy-mm | |||
| * @return | |||
| */ | |||
| public static String getNowYearMonth(){ | |||
| //获取当前时间 | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(YYYYMM); | |||
| return dateTimeFormatter.format(localDateTime); | |||
| } | |||
| /** | |||
| * 获取当前月最大天数 | |||
| * @return | |||
| */ | |||
| public static String getNowMonthMaxDay(){ | |||
| //获取当前时间 | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(YYYYMMDD); | |||
| LocalDateTime with = localDateTime.with(TemporalAdjusters.lastDayOfMonth()); | |||
| String format = dateTimeFormatter.format(with); | |||
| return format.substring(8,10); | |||
| } | |||
| public static String getMaxDay(){ | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| LocalDateTime with = localDateTime.with(TemporalAdjusters.lastDayOfMonth()); | |||
| return null; | |||
| } | |||
| /** | |||
| * 将毫秒转为字符串 yyyy-mm-dd HH:mm:ss | |||
| * @param time | |||
| * @return | |||
| */ | |||
| public static String getmilsMoveDate(Long time){ | |||
| LocalDateTime localDateTime = Instant.ofEpochMilli(time).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime(); | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS); | |||
| return formatter.format(localDateTime); | |||
| } | |||
| /** | |||
| * 将毫秒转为字符串 yyyy-mm-dd | |||
| * @param time | |||
| * @return | |||
| */ | |||
| public static String getmilsMoveDates(Long time){ | |||
| LocalDateTime localDateTime = Instant.ofEpochMilli(time).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime(); | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDD); | |||
| return formatter.format(localDateTime); | |||
| } | |||
| /** | |||
| 将日期转为字符串 yyyy-mm-dd HH:mm:ss | |||
| @param date | |||
| @return | |||
| */ | |||
| public static String getYYYYMMDDHHMMSS(Date date) { | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS); | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| return formatter.format(localDateTime); | |||
| } | |||
| /** | |||
| 将日期转为字符串 yyyy-mm-dd 00:00:00 | |||
| @param date | |||
| @return | |||
| */ | |||
| public static String getYYYYMMDDHHMMSSFirst(Date date) { | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS); | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| String format = formatter.format(localDateTime); | |||
| format = format + " 00:00:00"; | |||
| return format; | |||
| } | |||
| /** | |||
| 将日期转为字符串 yyyy-mm-dd HH:mm:ss | |||
| @param date | |||
| @return | |||
| */ | |||
| public static String getYYYYMMDDHHMMSSLast(Date date) { | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS); | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| String format = formatter.format(localDateTime); | |||
| format = format + " 23:59:59"; | |||
| return format; | |||
| } | |||
| /** | |||
| 将字符串转为日期 | |||
| @param date | |||
| @return | |||
| */ | |||
| public static Date getDate(String date) { | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS); | |||
| LocalDateTime localDateTime = LocalDateTime.parse(date, dateTimeFormatter); | |||
| return dateTimeToDate(localDateTime); | |||
| } | |||
| /** | |||
| * 解析字符串为日期 | |||
| * <p> | |||
| * 说明: | |||
| * 1. 该方法主要解决yyyy-MM-dd格式文本字符串无法直接转换为LocalDateTime的问题 | |||
| * | |||
| * @param source 需要解析的日期字符串 | |||
| * @param formatPattern 日期格式化模式 | |||
| * @return 格式化后的日期 | |||
| */ | |||
| public static Date parseStringToLocalDateTime(String source, String formatPattern) { | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatPattern); | |||
| LocalDate localDate = LocalDate.parse(source, dateTimeFormatter); | |||
| LocalDateTime localDateTime = localDate.atStartOfDay(); | |||
| return dateTimeToDate(localDateTime); | |||
| } | |||
| /** | |||
| 将字符串转为日期 | |||
| @param date | |||
| @return | |||
| */ | |||
| public static Date getDate(String date, String pattern) { | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); | |||
| LocalDateTime localDateTime = LocalDateTime.parse(date, dateTimeFormatter); | |||
| return dateTimeToDate(localDateTime); | |||
| } | |||
| /** | |||
| LocalDateTime 和DateTime 互转 | |||
| @param date | |||
| @return | |||
| */ | |||
| public static LocalDateTime dateToDateTime(Date date) { | |||
| Instant instant = date.toInstant(); | |||
| return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); | |||
| } | |||
| public static Date dateTimeToDate(LocalDateTime localDateTime) { | |||
| Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); | |||
| return date; | |||
| } | |||
| /** | |||
| date 转LocalDate | |||
| @param date | |||
| @return | |||
| */ | |||
| public static LocalDate dateToLocalDate(Date date) { | |||
| Instant instant = date.toInstant(); | |||
| LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate(); | |||
| return localDate; | |||
| } | |||
| /** | |||
| date 转LocalDate | |||
| @param localDate | |||
| @return | |||
| */ | |||
| public static Date localToDate(LocalDate localDate) { | |||
| Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); | |||
| return date; | |||
| } | |||
| /** | |||
| date 转DateTime | |||
| @param date | |||
| @return | |||
| */ | |||
| public static LocalTime dateToLocalTime(Date date) { | |||
| Instant instant = date.toInstant(); | |||
| LocalTime localTime = instant.atZone(ZoneId.systemDefault()).toLocalTime(); | |||
| return localTime; | |||
| } | |||
| /** | |||
| date 转LocalTime | |||
| @param localTime | |||
| @return | |||
| */ | |||
| public static Date localToDate(LocalTime localTime) { | |||
| LocalDate localDate = LocalDate.now(); | |||
| LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime); | |||
| ZoneId zone = ZoneId.systemDefault(); | |||
| Instant instant = localDateTime.atZone(zone).toInstant(); | |||
| Date date = Date.from(instant); | |||
| return date; | |||
| } | |||
| /** | |||
| 将日期转为字符串 yyyy-mm-dd | |||
| @param date | |||
| @return | |||
| */ | |||
| public static String getYYYYMMDD(Date date) { | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDD); | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| return formatter.format(localDateTime); | |||
| } | |||
| /** | |||
| 将日期转为字符串 yyyy-mm-dd | |||
| @param date | |||
| @return | |||
| */ | |||
| public static String getYYYYMM(Date date) { | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMM); | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| return formatter.format(localDateTime); | |||
| } | |||
| /** | |||
| 获取当前时间之后的某一天的最小时间 | |||
| @param date | |||
| @return | |||
| */ | |||
| public static Date afterXDateTimeMIN(Date date, int after) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.plusDays(after); | |||
| localDateTime = localDateTime.with(LocalTime.MIN); | |||
| return dateTimeToDate(localDateTime); | |||
| } | |||
| /** | |||
| 获取当前时间之后的某一天的最大时间 | |||
| @param date | |||
| @return | |||
| */ | |||
| public static Date afterXDateTimeMAX(Date date, int after) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.plusDays(after); | |||
| localDateTime = localDateTime.with(LocalTime.MAX); | |||
| return dateTimeToDate(localDateTime); | |||
| } | |||
| /** | |||
| 获取当前时间之前的某一天的最小时间 | |||
| @param date | |||
| @return | |||
| */ | |||
| public static Date beforeXDateTimeMIN(Date date, int before) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.minusDays(before); | |||
| localDateTime = localDateTime.with(LocalTime.MIN); | |||
| return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); | |||
| } | |||
| /** | |||
| 获取当前时间之前的某一天的最大时间 | |||
| @param date | |||
| @return | |||
| */ | |||
| public static Date beforeXDateTimeMAX(Date date, int before) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.minusDays(before); | |||
| localDateTime = localDateTime.with(LocalTime.MAX); | |||
| return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); | |||
| } | |||
| /** | |||
| 获取本月的第一天 00:00:00 | |||
| @return | |||
| */ | |||
| public static String currentFirstDayOfMonth() { | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| localDateTime = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN); | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS); | |||
| Date date = dateTimeToDate(localDateTime); | |||
| localDateTime = dateToDateTime(date); | |||
| return formatter.format(localDateTime); | |||
| } | |||
| /** | |||
| 获取本月的最后一天 23:59:59 | |||
| @return | |||
| */ | |||
| public static String currentLastDayOfMonth() { | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| localDateTime = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX); | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS); | |||
| Date date = dateTimeToDate(localDateTime); | |||
| localDateTime = dateToDateTime(date); | |||
| return formatter.format(localDateTime); | |||
| } | |||
| /** | |||
| 获取传入时间月份的最后一天 | |||
| @return | |||
| */ | |||
| public static Date currentXDayOfMonth(Date date) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX); | |||
| return dateTimeToDate(localDateTime); | |||
| } | |||
| /** | |||
| 所选date的月份的第一天 | |||
| @return | |||
| */ | |||
| public static Date beforeXFirstDayOfMonth(Date date) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN); | |||
| return dateTimeToDate(localDateTime); | |||
| } | |||
| // public static void main(String[] args) { | |||
| // Date date = beforeXFirstDayOfMonth(new Date()); | |||
| // Date date = currentXDayOfMonth(new Date()); | |||
| // | |||
| // System.out.println(getYYYYMMDDHHMMSS(date)); | |||
| // } | |||
| /** | |||
| 获取前几个月的1号0点 00:00:00 | |||
| @return | |||
| */ | |||
| public static String preXDayOfMonthMIN(int month) { | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| localDateTime = localDateTime.minusMonths(month); | |||
| localDateTime = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN); | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS); | |||
| Date date = dateTimeToDate(localDateTime); | |||
| localDateTime = dateToDateTime(date); | |||
| return formatter.format(localDateTime); | |||
| } | |||
| /** | |||
| 获取前几个月的1号0点 00:00:00 | |||
| @return | |||
| */ | |||
| public static String preXDayOfMonthMIN2(int month) { | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| localDateTime = localDateTime.minusMonths(month); | |||
| localDateTime = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN); | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMM); | |||
| Date date = dateTimeToDate(localDateTime); | |||
| localDateTime = dateToDateTime(date); | |||
| return formatter.format(localDateTime); | |||
| } | |||
| /** | |||
| 获取前几个月的1号0点 00:00:00 | |||
| @return | |||
| */ | |||
| public static Date preXDayOfMonthMIN(Date date, int month) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.minusMonths(month); | |||
| localDateTime = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN); | |||
| return dateTimeToDate(localDateTime); | |||
| } | |||
| /** | |||
| 传入时间的的前几个月的时间 | |||
| @return | |||
| */ | |||
| public static Date preXDayOfMonth(Date date, int month) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.minusMonths(month); | |||
| return dateTimeToDate(localDateTime); | |||
| } | |||
| /** | |||
| 传入时间的的前几个月的时间 | |||
| @return | |||
| */ | |||
| public static Date afterXDayOfMonth(Date date, int month) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.plusMonths(month); | |||
| return dateTimeToDate(localDateTime); | |||
| } | |||
| /** | |||
| 获取前几个月的最后一天23:59:59 | |||
| @return | |||
| */ | |||
| public static String preXDayOfMonthMAX(int month) { | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| localDateTime = localDateTime.minusMonths(month); | |||
| localDateTime = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX); | |||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS); | |||
| Date date = dateTimeToDate(localDateTime); | |||
| localDateTime = dateToDateTime(date); | |||
| return formatter.format(localDateTime); | |||
| } | |||
| /** | |||
| 获取某个时间几个月的最后一天23:59:59 | |||
| @return | |||
| */ | |||
| public static Date preXDayOfMonthMAX(Date date, int month) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.minusMonths(month); | |||
| localDateTime = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX); | |||
| return dateTimeToDate(localDateTime); | |||
| } | |||
| /** | |||
| 两个日期相差多少个月 | |||
| @param date1 | |||
| @param date2 | |||
| @return | |||
| */ | |||
| public static Long getUntilMonth(Date date1, Date date2) { | |||
| LocalDate localDate1 = dateToLocalDate(date1); | |||
| LocalDate localDate2 = dateToLocalDate(date2); | |||
| return ChronoUnit.MONTHS.between(localDate1, localDate2); | |||
| } | |||
| /** | |||
| 两个日期相差多少天 | |||
| @param date1 | |||
| @param date2 | |||
| @return | |||
| */ | |||
| public static Long getUntilDay(Date date1, Date date2) { | |||
| LocalDate localDate1 = dateToLocalDate(date1); | |||
| LocalDate localDate2 = dateToLocalDate(date2); | |||
| return ChronoUnit.DAYS.between(localDate1, localDate2); | |||
| } | |||
| /** | |||
| 两个日期相差多少小时 | |||
| @param date1 | |||
| @param date2 | |||
| @return | |||
| */ | |||
| public static Long getUntilHours(Date date1, Date date2) { | |||
| LocalDateTime localDate1 = dateToDateTime(date1); | |||
| LocalDateTime localDate2 = dateToDateTime(date2); | |||
| Long senonds = Duration.between(localDate1, localDate2).get(ChronoUnit.SECONDS); | |||
| return senonds / 3600; | |||
| } | |||
| /** | |||
| 两个日期相差多少小时 double 约等于 | |||
| @param date1 | |||
| @param date2 | |||
| @return | |||
| */ | |||
| public static double getUntilHoursByDouble(Date date1, Date date2) { | |||
| LocalDateTime localDate1 = dateToDateTime(date1); | |||
| LocalDateTime localDate2 = dateToDateTime(date2); | |||
| Long seconds = Duration.between(localDate1, localDate2).get(ChronoUnit.SECONDS); | |||
| BigDecimal secondss = BigDecimal.valueOf(seconds); | |||
| BigDecimal hours = secondss.divide(BigDecimal.valueOf(3600), 2, BigDecimal.ROUND_HALF_UP); | |||
| return hours.doubleValue(); | |||
| } | |||
| /** | |||
| 两个日期相差多少秒 | |||
| @param date1 | |||
| @param date2 | |||
| @return | |||
| */ | |||
| public static Long getUntilSecond(Date date1, Date date2) { | |||
| LocalDateTime localDate1 = dateToDateTime(date1); | |||
| LocalDateTime localDate2 = dateToDateTime(date2); | |||
| return Duration.between(localDate1, localDate2).get(ChronoUnit.SECONDS); | |||
| } | |||
| /** | |||
| 当前时间23:59:59 | |||
| @param date | |||
| @return | |||
| */ | |||
| public static Date currentMax(Date date) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.with(LocalTime.MAX); | |||
| return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); | |||
| } | |||
| /** | |||
| 当前时间00:00:00 | |||
| @param date | |||
| @return | |||
| */ | |||
| public static Date currentMin(Date date) { | |||
| LocalDateTime localDateTime = dateToDateTime(date); | |||
| localDateTime = localDateTime.with(LocalTime.MIN); | |||
| return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); | |||
| } | |||
| /** | |||
| * 将日期格式的时间转化为毫秒 | |||
| * @param date | |||
| * @return | |||
| */ | |||
| public static Long getMilliSecond(String date){ | |||
| Timestamp timestamp = Timestamp.valueOf(date); | |||
| return timestamp.getTime(); | |||
| } | |||
| /** | |||
| * 获取本周第一天 | |||
| * @param num 0本周,1下周,-1上周,依次类推 | |||
| * @return | |||
| */ | |||
| public static final String getFirstWeekDay(int num){ | |||
| LocalDate localDate = LocalDate.now().plusWeeks(num); | |||
| LocalDate with = localDate.with(DayOfWeek.MONDAY); | |||
| String firstDay = with+" 00:00:00"; | |||
| return firstDay; | |||
| } | |||
| /** | |||
| * 获取本周最后一天 | |||
| * @param num 0本周,1下周,-1上周,依次类推 | |||
| * @return | |||
| */ | |||
| public static final String getLastWeekDay(int num){ | |||
| LocalDate localDate = LocalDate.now().plusWeeks(num); | |||
| LocalDate with = localDate.with(DayOfWeek.SUNDAY); | |||
| String firstDay = with+" 23:59:59"; | |||
| return firstDay; | |||
| } | |||
| /** | |||
| * 获取今天00:00:00毫秒数 | |||
| * @return | |||
| */ | |||
| public static final Long getTodayZero(){ | |||
| //当前时间毫秒数 | |||
| Long current=System.currentTimeMillis(); | |||
| //今天零点零分零秒的毫秒数 | |||
| Long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset(); | |||
| return zero; | |||
| } | |||
| /** | |||
| * 获取今天23点59分59秒的毫秒数 | |||
| * @return | |||
| */ | |||
| public static final Long getTodayTwelve(){ | |||
| //当前时间毫秒数 | |||
| Long current=System.currentTimeMillis(); | |||
| //今天零点零分零秒的毫秒数 | |||
| Long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset(); | |||
| //今天23点59分59秒的毫秒数 | |||
| Long twelve=zero+24*60*60*1000-1; | |||
| return twelve; | |||
| } | |||
| /**获取当前日期的前一天*/ | |||
| public static final String getYesterday(){ | |||
| // 获取当前日期 | |||
| LocalDate today = LocalDate.now(); | |||
| // 获取当前日期的前一天 | |||
| String yesterday = today.plusDays(-1).toString(); | |||
| return yesterday; | |||
| } | |||
| /** | |||
| * 将日期进行加减运算 | |||
| * @param startdate | |||
| * @param days 天数 | |||
| * @return | |||
| * @throws ParseException | |||
| */ | |||
| public static String plusDate(String startdate,int days){ | |||
| SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD); | |||
| try { | |||
| Date date = sdf.parse(startdate); | |||
| long ltime = date.getTime(); | |||
| long ldays = days * (1000L * 60L * 60L * 24L); | |||
| Date newDay = new Date(ltime + ldays); | |||
| return getDateString(newDay, YYYYMMDD); | |||
| } catch (ParseException e) { | |||
| return null; | |||
| } | |||
| } | |||
| /** | |||
| * 取得时间 | |||
| * @param date | |||
| * @param format | |||
| * @return | |||
| */ | |||
| public static String getDateString(Date date,String format){ | |||
| DateFormat toDateFormat = new SimpleDateFormat(format); | |||
| String d = toDateFormat.format(date); | |||
| return d; | |||
| } | |||
| /** | |||
| * 将日期进行加减运算 | |||
| * @param startdate | |||
| * @param days 天数 | |||
| * @return | |||
| * @throws ParseException | |||
| */ | |||
| public static String dellDate(String startdate,int days,String format){ | |||
| SimpleDateFormat sdf = new SimpleDateFormat(format); | |||
| try { | |||
| Date date = sdf.parse(startdate); | |||
| long ltime = date.getTime(); | |||
| long ldays = days * (1000L * 60L * 60L * 24L); | |||
| Date newDay = new Date(ltime + ldays); | |||
| return getDate(newDay, format); | |||
| } catch (ParseException e) { | |||
| return null; | |||
| } | |||
| } | |||
| public static String getDate(Date date,String format){ | |||
| DateFormat toDateFormat = new SimpleDateFormat(format); | |||
| String d = toDateFormat.format(date); | |||
| return d; | |||
| } | |||
| /** | |||
| * 将日期进行加减运算 | |||
| * @param startdate | |||
| * @param days 天数 | |||
| * @return | |||
| * @throws ParseException | |||
| */ | |||
| public static String dellDate(String startdate,int days){ | |||
| SimpleDateFormat sdf = new SimpleDateFormat(DAY_FORMAT); | |||
| try { | |||
| Date date = sdf.parse(startdate); | |||
| long ltime = date.getTime(); | |||
| long ldays = days * (1000L * 60L * 60L * 24L); | |||
| Date newDay = new Date(ltime + ldays); | |||
| return getDate(newDay, DAY_FORMAT); | |||
| } catch (ParseException e) { | |||
| return null; | |||
| } | |||
| } | |||
| /** | |||
| * 时间加减 | |||
| * @param startDate | |||
| * @param type 1年,2月,3日,4时,5分,6秒 | |||
| * @param num | |||
| * @param format | |||
| * @return | |||
| */ | |||
| public static String dellDate(String startDate, int type, int num, | |||
| String format) { | |||
| SimpleDateFormat f = new SimpleDateFormat(format); | |||
| Calendar c = Calendar.getInstance(); | |||
| Date s = getDate(startDate, format); | |||
| // log.info(s); | |||
| c.setTime(s); | |||
| if (type == 1) { | |||
| c.add(Calendar.YEAR, num); | |||
| } else if (type == 2) { | |||
| c.add(Calendar.MONTH, num); | |||
| } else if (type == 3) { | |||
| c.add(Calendar.DAY_OF_YEAR, num); | |||
| } else if (type == 4) { | |||
| c.add(Calendar.HOUR_OF_DAY, num); | |||
| } else if (type == 5) { | |||
| c.add(Calendar.MINUTE, num); | |||
| } else if (type == 6) { | |||
| c.add(Calendar.SECOND, num); | |||
| } | |||
| return f.format(c.getTime()); | |||
| } | |||
| /** | |||
| * 获取当前月份 MM | |||
| * @return | |||
| */ | |||
| public static String getNowMonth(){ | |||
| //获取当前时间 | |||
| LocalDateTime localDateTime = LocalDateTime.now(); | |||
| DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(MM); | |||
| return dateTimeFormatter.format(localDateTime); | |||
| } | |||
| } | |||