123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- package com.gyee.impala.common.util;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.time.DateFormatUtils;
- import org.apache.commons.lang3.time.DateUtils;
- import java.lang.management.ManagementFactory;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.TimeZone;
- @Slf4j
- public class DateUtil extends DateUtils {
- /**
- * 获取当前时间
- *
- * @return
- */
- public static String getCurrentDate() {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- String date = df.format(new Date());
- return date;
- }
- /**
- * 获取前 N 小时的时间
- *
- * @param hour
- * @return
- */
- public static String getPreviousDate(int hour) {
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String date = df.format(calendar.getTime());
- return date;
- }
- /**
- * 获取当前时间后 N 小时的时间
- *
- * @param hour
- * @return
- */
- public static String getNextDate(int hour) {
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String date = df.format(calendar.getTime());
- return date;
- }
- /**
- * 获取当前时间后 N 小时的时间
- *
- * @param hour
- * @return
- */
- public static Long getNextDateTimestamp(int hour) {
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
- long time = calendar.getTime().getTime();
- return time;
- }
- /**
- * 字符串时间转时间戳
- * @param time
- * @return
- */
- public static Long covertDateTimestamp(String time){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- long stamp = 0;
- try {
- stamp = sdf.parse(time).getTime();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return stamp;
- }
- /**
- * 时间格式(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";
- public static String format(Date date) {
- return format(date, DATE_PATTERN);
- }
- public static String formatDateTime(Date date) {
- return format(date, DATE_TIME_PATTERN);
- }
- /**
- * 时间转换
- *
- * @param date
- * @param pattern
- * @return
- */
- public static String format(Date date, String pattern) {
- if (date != null) {
- SimpleDateFormat df = new SimpleDateFormat(pattern);
- return df.format(date);
- }
- return null;
- }
- /**
- * 字符串格式化
- * @param time
- * @param pattern
- * @return
- */
- public static String format(String time,String pattern) {
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
- Date dateTime = null;
- try {
- dateTime = sdf.parse(time);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return format(dateTime, pattern);
- }
- /**
- * 时间转换
- *
- * @param time
- * @param pattern
- * @return
- */
- public static String format(Long time, String pattern) {
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
- Date date = new Date(time);
- String format = sdf.format(date);
- return format;
- }
- public static String YYYY = "yyyy";
- public static String YYYY_MM = "yyyy-MM";
- public static String YYYY_MM_DD = "yyyy-MM-dd";
- public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
- public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
- private static String[] parsePatterns = {
- "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
- "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
- "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
- /**
- * 获取当前Date型日期
- *
- * @return Date() 当前日期
- */
- public static Date getNowDate() {
- return new Date();
- }
- /**
- * 获取当前日期, 默认格式为yyyy-MM-dd
- *
- * @return String
- */
- public static String getDate() {
- return dateTimeNow(YYYY_MM_DD);
- }
- public static final String getTime() {
- return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
- }
- public static final String dateTimeNow() {
- return dateTimeNow(YYYYMMDDHHMMSS);
- }
- public static final String dateTimeNow(final String format) {
- return parseDateToStr(format, new Date());
- }
- public static final String dateTime(final Date date) {
- return parseDateToStr(YYYY_MM_DD, date);
- }
- public static final String parseDateToStr(final String format, final Date date) {
- return new SimpleDateFormat(format).format(date);
- }
- public static final Date dateTime(final String format, final String ts) {
- try {
- return new SimpleDateFormat(format).parse(ts);
- } catch (ParseException e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 日期路径 即年/月/日 如2018/08/08
- */
- public static final String datePath() {
- Date now = new Date();
- return DateFormatUtils.format(now, "yyyy/MM/dd");
- }
- /**
- * 日期路径 即年/月/日 如20180808
- */
- public static final String dateTime() {
- Date now = new Date();
- return DateFormatUtils.format(now, "yyyyMMdd");
- }
- /**
- * 日期型字符串转化为日期 格式
- */
- public static Date parseDate(Object str) {
- if (str == null) {
- return null;
- }
- try {
- return parseDate(str.toString(), parsePatterns);
- } catch (ParseException e) {
- return null;
- }
- }
- /**
- * 获取服务器启动时间
- */
- public static Date getServerStartDate() {
- long time = ManagementFactory.getRuntimeMXBean().getStartTime();
- return new Date(time);
- }
- /**
- * 计算两个时间差
- */
- public static String getDatePoor(Date endDate, Date nowDate) {
- long nd = 1000 * 24 * 60 * 60;
- long nh = 1000 * 60 * 60;
- long nm = 1000 * 60;
- // long ns = 1000;
- // 获得两个时间的毫秒时间差异
- long diff = endDate.getTime() - nowDate.getTime();
- // 计算差多少天
- long day = diff / nd;
- // 计算差多少小时
- long hour = diff % nd / nh;
- // 计算差多少分钟
- long min = diff % nd % nh / nm;
- // 计算差多少秒//输出结果
- // long sec = diff % nd % nh % nm / ns;
- return day + "天" + hour + "小时" + min + "分钟";
- }
- /**
- * 将时间的时分秒毫秒字段去掉
- *
- * @param date
- * @return
- */
- public static Date truncate(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.set(Calendar.HOUR_OF_DAY, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.SECOND, 0);
- cal.set(Calendar.MILLISECOND, 0);
- return cal.getTime();
- }
- /**
- * 计算两个时间之间差的天数(取整后)
- *
- * @param d1
- * @param d2
- * @return
- */
- public static int daysDiff(Date d1, Date d2) {
- return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 24 * 1000));
- }
- /**
- * 将字符串日期转换成日期类型
- *
- * @param time
- * @return
- */
- public static Date parseStrtoDate(String time) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Date dateTime = null;
- try {
- dateTime = sdf.parse(time);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return dateTime;
- }
- public static Date parseStrtoDate(String time,String pattern) {
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
- Date dateTime = null;
- try {
- dateTime = sdf.parse(time);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return dateTime;
- }
- public static Date str2DateTime(String time,String pattern) {
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
- Date dateTime = null;
- try {
- dateTime = sdf.parse(time);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return dateTime;
- }
- public static Date str2DateTime(String time) {
- return str2DateTime(time,DATE_TIME_PATTERN);
- }
- /**
- * 加减年份
- */
- public static Date dateTimeAddYear(Date date,int k) {
- Calendar instance = Calendar.getInstance();
- instance.setTime(date);
- instance.add(Calendar.YEAR,k);
- return instance.getTime();
- }
- public static String datetime2Str(Date time,String pattern) {
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
- return sdf.format(time);
- }
- public static String datetime2Str(Date time) {
- return datetime2Str(time,DATE_TIME_PATTERN);
- }
- public static double hoursDiff1(Date d1, Date d2) {
- return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000));
- }
- public static double hoursDiff2(Date d1, Date d2) {
- return Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000);
- }
- /**
- * 计算两个时间之间差的分钟数(取整后)
- *
- * @param d1
- * @param d2
- * @return
- */
- public static int minutesDiff(Date d1, Date d2) {
- return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
- }
- /**
- * 计算两个时间之间差的分钟数(取整后)
- *
- * @param d1
- * @param d2
- * @return
- */
- public static double minutesDiff2(Date d1, Date d2) {
- return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
- }
- /**
- * 获取系统时间
- *
- * @return
- */
- public static Date now() {
- return new Date();
- }
- /**
- * 获取当前月的第一天
- *
- * @return
- */
- public static String getCurrtenFirstDay() {
- Calendar c = Calendar.getInstance();
- // c.add(Calendar.MONTH, 0);
- c.set(Calendar.DAY_OF_MONTH, 1);
- return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
- }
- /**
- * 获取当前月的最后一天
- *
- * @return
- */
- public static String getCurrtenLastDay() {
- Calendar ca = Calendar.getInstance();
- ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
- return new SimpleDateFormat("yyyy-MM-dd").format(ca.getTime());
- }
- /**
- * 计算两个时间之间差的小时数(取整后)
- *
- * @param d1
- * @param d2
- * @return
- */
- public static int hoursDiff(Date d1, Date d2) {
- return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 1000));
- }
- /**
- * 获取当前整点数
- *
- * @return
- */
- public static Date getCurrHourTime(Date date) {
- Calendar ca = Calendar.getInstance();
- int minute = ca.get(Calendar.MINUTE);
- if (minute < 30) {
- ca.set(Calendar.MINUTE, 30);
- } else {
- ca.add(Calendar.HOUR, 1);
- ca.set(Calendar.MINUTE, 0);
- }
- ca.set(Calendar.SECOND, 5);
- date = ca.getTime();
- return date;
- }
- //普通时间转为UTC
- public static String localToUTC(String localTimeStr) {
- try {
- Date localDate = getLocalSDF().parse(localTimeStr);
- return getUTCSDF().format(localDate);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }
- //UTC转为普通时间
- public static String utcToLocal(String utcTimeStr) {
- try {
- Date date = getUTCSDF().parse(utcTimeStr);
- return getLocalSDF().format(date);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }
- private static SimpleDateFormat getLocalSDF() {
- return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
- }
- private static SimpleDateFormat getUTCSDF() {
- SimpleDateFormat utcSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
- utcSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
- return utcSDF;
- }
- public static void main(String[] args) {
- System.out.println(getCurrentDate());
- System.out.println(getPreviousDate(3 * 360 * 24));
- System.out.println(covertDateTimestamp("2021-12-30 00:00:00"));
- }
- /**
- * @return 当天零点
- */
- public static Date today0am() {
- Calendar day = Calendar.getInstance();
- day.set(Calendar.HOUR_OF_DAY, 0);
- day.set(Calendar.MINUTE, 0);
- day.set(Calendar.SECOND, 0);
- day.set(Calendar.MILLISECOND, 0);
- return day.getTime();
- }
- }
|