| | |
| | | package com.panzhihua.common.utlis; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.text.DateFormat; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.*; |
| | |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | @Slf4j |
| | | public class DateUtils { |
| | | |
| | | |
| | | public static DateTimeFormatter format_ymdhms = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | private static DateTimeFormatter format_ymdhmssss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); |
| | | |
| | | private static DateTimeFormatter format_ymds = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | |
| | | public static DateTimeFormatter format_ymd = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | |
| | | public static DateTimeFormatter format_ymdhms_string = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); |
| | | |
| | | public static DateTimeFormatter format_ymdhms_no_signal = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); |
| | | |
| | | public static SimpleDateFormat yyyyMMdd_format = new SimpleDateFormat("yyyy-MM-dd"); |
| | | |
| | | public static SimpleDateFormat yyyy_MM_dd_format = new SimpleDateFormat("yyyy/MM/ddHH:mm:ss"); |
| | | |
| | | public static SimpleDateFormat ymdhms_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | public static SimpleDateFormat ymdhm_format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| | | |
| | | public static DateTimeFormatter format_ymd_String = DateTimeFormatter.ofPattern("yyMMdd"); |
| | | |
| | | public static SimpleDateFormat yyyyMM_format = new SimpleDateFormat("yyyy-MM"); |
| | | |
| | | |
| | | private static DateTimeFormatter format_ymdhmssss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); |
| | | private static DateTimeFormatter format_ymds = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | |
| | | /** |
| | | * 将日期对象格式化成指定的字符串格式 |
| | | * |
| | | * @param date 日期对象 |
| | | * @param formatString 格式化字符串 |
| | | * @param date |
| | | * 日期对象 |
| | | * @param formatString |
| | | * 格式化字符串 |
| | | * @return String |
| | | */ |
| | | public static String getDateFormatString(Date date, String formatString) { |
| | |
| | | |
| | | /** |
| | | * 字符串转date |
| | | * @param date 时间戳 |
| | | * @param format format |
| | | * |
| | | * @param date |
| | | * 时间戳 |
| | | * @param format |
| | | * format |
| | | * @return 时间 |
| | | */ |
| | | public static Date stringToDate(String date,SimpleDateFormat format){ |
| | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 字符串转date |
| | | * @param date 时间戳 |
| | | * |
| | | * @param date |
| | | * 时间戳 |
| | | * @return 时间 |
| | | */ |
| | | public static Date stringToDateStandard (String date){ |
| | |
| | | |
| | | /** |
| | | * 字符串转date |
| | | * @param date 时间戳 |
| | | * |
| | | * @param date |
| | | * 时间戳 |
| | | * @return 时间 |
| | | */ |
| | | public static Date stringToDateStandardByYYYY_MM_dd (String date){ |
| | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前时间 |
| | | * |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 返回当前事件的时间戳 |
| | | * |
| | | * @return |
| | | */ |
| | | public static Long getCurrentDateTimeStamp(){ |
| | | return LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前时间字符串,格式为yyyy-MM-dd HH:mm:ss |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentDateStr(){ |
| | |
| | | |
| | | /** |
| | | * 获取当前时间字符串,格式为yyyyMMdd |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentDateString(){ |
| | |
| | | return nowtime; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前时间字符串,格式为yyyy-MM-dd |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentDateStrymd(){ |
| | |
| | | |
| | | /** |
| | | * 获取当前时间long值 |
| | | * |
| | | * @return |
| | | */ |
| | | public static long getCurrentDataLong(){ |
| | | return new Date().getTime(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取当前时间 |
| | | * |
| | | * @param format |
| | | * @return |
| | | * @throws Exception |
| | |
| | | public static String formatSecond_ZH_HMS(Integer second) { |
| | | String result = "0秒"; |
| | | if (null != second) { |
| | | int hours = (int) (second / (60 * 60)); |
| | | int minutes = (int) (second / 60 - hours * 60); |
| | | int seconds = (int) (second - minutes * 60 - hours * 60 * 60); |
| | | int hours = second / (60 * 60); |
| | | int minutes = second / 60 - hours * 60; |
| | | int seconds = second - minutes * 60 - hours * 60 * 60; |
| | | |
| | | String format; |
| | | Object[] array; |
| | |
| | | |
| | | /** |
| | | * 对日期进行加法操作 |
| | | * |
| | | * @param date |
| | | * @param hours |
| | | * @return |
| | |
| | | |
| | | /** |
| | | * 对日期的分钟进行加法操作 |
| | | * |
| | | * @param date |
| | | * @param minutes |
| | | * @return |
| | |
| | | |
| | | /** |
| | | * 计算两个日期先差多少秒 |
| | | * @param pre 前一个日期 |
| | | * @param after 后一个日期 |
| | | * |
| | | * @param pre |
| | | * 前一个日期 |
| | | * @param after |
| | | * 后一个日期 |
| | | * @return |
| | | */ |
| | | public static long calTimeDifference(Date pre,Date after){ |
| | | return (after.getTime()-pre.getTime())/1000; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 计算两个时间相差的天数 |
| | | * |
| | | * @param stardate |
| | | * @param enddate |
| | | * @return |
| | |
| | | |
| | | /** |
| | | * 结束时间与当前时间计算相差的月数 |
| | | * |
| | | * @param enddate |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 获取某个时间段之前的时间点 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getSomeTimeStapStr(String sign){ |
| | |
| | | return beginTime; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取失效时间点(在什么时间失效) |
| | | * @param minutes 有效分钟数 |
| | | * |
| | | * @param minutes |
| | | * 有效分钟数 |
| | | * @return |
| | | */ |
| | | public static LocalDateTime getInvalidLocalDateTime(Long minutes){ |
| | | return LocalDateTime.now().minusMinutes(minutes); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前年份 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentYear(){ |
| | |
| | | return instance.get(Calendar.YEAR)+""; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前月份 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentMonth(){ |
| | |
| | | return result; |
| | | } |
| | | |
| | | /** 获取无符号的当前时间 |
| | | /** |
| | | * 获取无符号的当前时间 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentDateStrNoSignal(){ |
| | | return getCurrentDate().format(format_ymdhms_no_signal); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取前几个小时的时间 |
| | | * |
| | | * @param hours |
| | | * @return |
| | | */ |
| | | public static String getNextHourDateStrNoSignal(long hours){ |
| | | return LocalDateTime.now().plusHours(hours).format(format_ymdhms_no_signal); |
| | | } |
| | | |
| | | |
| | | public static Date localDateTimeToDate(LocalDateTime localDateTime) { |
| | | ZoneId zone = ZoneId.systemDefault(); |
| | |
| | | |
| | | /** |
| | | * 小时取整 |
| | | * |
| | | * @param date |
| | | * @param hour |
| | | * @return |
| | |
| | | } |
| | | |
| | | /** |
| | | * 分钟取整 |
| | | * 以十分钟为单位 ,去除尾端,加上 参数 46->40+minutes*10 |
| | | * 分钟取整 以十分钟为单位 ,去除尾端,加上 参数 46->40+minutes*10 |
| | | * |
| | | * @param date |
| | | * @param minutes |
| | | * @return |
| | |
| | | |
| | | /** |
| | | * 天数取整 |
| | | * |
| | | * @param date |
| | | * @param day |
| | | * @return |
| | |
| | | |
| | | /** |
| | | * 日期相加减 |
| | | * |
| | | * @param time |
| | | * 时间字符串 yyyy-MM-dd HH:mm:ss |
| | | * @param num |
| | | * 加的数,-num就是减去 |
| | | * @return |
| | | * 减去相应的数量的年的日期 |
| | | * @return 减去相应的数量的年的日期 |
| | | * @throws ParseException |
| | | */ |
| | | public static Date yearAddNum(Date time, Integer num) { |
| | |
| | | return date; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将日期对象格式化成yyyy-MM-dd格式的字符串 |
| | | * |
| | | * @param date 日期对象 |
| | | * @param date |
| | | * 日期对象 |
| | | * @return String |
| | | */ |
| | | public static String getDateStringYMD(Date date) { |
| | |
| | | |
| | | /** |
| | | * 获取当前月第一天 |
| | | * |
| | | * @return |
| | | */ |
| | | public static Date getFirstDayOfMonth() { |
| | |
| | | |
| | | /** |
| | | * 获取当前年月的第一天时间 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * |
| | | * @param year |
| | | * 年份 |
| | | * @param month |
| | | * 月份 |
| | | * @return 时间 |
| | | */ |
| | | public static Date getYearMonthStart(Integer year,Integer month) { |
| | |
| | | |
| | | /** |
| | | * 获取当前年月的最后一天时间 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * |
| | | * @param year |
| | | * 年份 |
| | | * @param month |
| | | * 月份 |
| | | * @return 时间 |
| | | */ |
| | | public static Date getYearMonthEnd(Integer year,Integer month) { |
| | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static int getMonth(Date date){ |
| | | Calendar instance = Calendar.getInstance(); |
| | | instance.setTime(date); |
| | |
| | | |
| | | /** |
| | | * date2比date1多的天数 |
| | | * |
| | | * @param date1 |
| | | * @param date2 |
| | | * @return |
| | | */ |
| | | public static int differentDays(Date date1,Date date2) |
| | | { |
| | | public static int differentDays(Date date1, Date date2) { |
| | | Calendar cal1 = Calendar.getInstance(); |
| | | cal1.setTime(date1); |
| | | |
| | |
| | | if(year1 != year2) //同一年 |
| | | { |
| | | int timeDistance = 0 ; |
| | | for(int i = year1 ; i < year2 ; i ++) |
| | | { |
| | | for (int i = year1; i < year2; i++) { |
| | | if(i%4==0 && i%100!=0 || i%400==0) //闰年 |
| | | { |
| | | timeDistance += 366; |
| | | } |
| | | else //不是闰年 |
| | | } else // 不是闰年 |
| | | { |
| | | timeDistance += 365; |
| | | } |
| | | } |
| | | |
| | | return timeDistance + (day2-day1) ; |
| | | } |
| | | else //不同年 |
| | | } else // 不同年 |
| | | { |
| | | // System.out.println("判断day2 - day1 : " + (day2-day1)); |
| | | return day2-day1; |
| | |
| | | |
| | | /** |
| | | * 获取当前月第一天 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getFirstDayOfMonthString() { |
| | |
| | | |
| | | /** |
| | | * 获取当前月最后一天 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getLastDayOfMonthString() { |
| | |
| | | |
| | | /** |
| | | * 获取当前时间(年月日) |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getDayOfMonthString() { |
| | |
| | | return weekDays[w]; |
| | | } |
| | | |
| | | |
| | | |
| | | public static void main(String[]args)throws Exception{ |
| | | // Date date= new Date(); |
| | | // Date after = new Date(); |
| | |
| | | |
| | | } |
| | | } |
| | | |