/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/packageorg.apache.dolphinscheduler.common.utils;importorg.apache.dolphinscheduler.common.Constants;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importjava.time.Instant;importjava.time.LocalDateTime;importjava.time.ZoneId;importjava.time.format.DateTimeFormatter;importjava.util.Calendar;importjava.util.Date;/**
* date utils
*/publicclassDateUtils{privatestaticfinalLoggerlogger=LoggerFactory.getLogger(DateUtils.class);/**
* date to local datetime
*
* @param date date
* @return local datetime
*/privatestaticLocalDateTimedate2LocalDateTime(Datedate){returnLocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault());}/**
* local datetime to date
*
* @param localDateTime local datetime
* @return date
*/privatestaticDatelocalDateTime2Date(LocalDateTimelocalDateTime){Instantinstant=localDateTime.atZone(ZoneId.systemDefault()).toInstant();returnDate.from(instant);}/**
* get current date str
*
* @return date string
*/publicstaticStringgetCurrentTime(){returngetCurrentTime(Constants.YYYY_MM_DD_HH_MM_SS);}/**
* get the date string in the specified format of the current time
*
* @param format date format
* @return date string
*/publicstaticStringgetCurrentTime(Stringformat){returnLocalDateTime.now().format(DateTimeFormatter.ofPattern(format));}/**
* get the formatted date string
*
* @param date date
* @param format e.g. yyyy-MM-dd HH:mm:ss
* @return date string
*/publicstaticStringformat(Datedate,Stringformat){returnformat(date2LocalDateTime(date),format);}/**
* get the formatted date string
*
* @param localDateTime local data time
* @param format yyyy-MM-dd HH:mm:ss
* @return date string
*/publicstaticStringformat(LocalDateTimelocalDateTime,Stringformat){returnlocalDateTime.format(DateTimeFormatter.ofPattern(format));}/**
* convert time to yyyy-MM-dd HH:mm:ss format
*
* @param date date
* @return date string
*/publicstaticStringdateToString(Datedate){returnformat(date,Constants.YYYY_MM_DD_HH_MM_SS);}/**
* convert string to date and time
*
* @param date date
* @param format format
* @return date
*/publicstaticDateparse(Stringdate,Stringformat){try{LocalDateTimeldt=LocalDateTime.parse(date,DateTimeFormatter.ofPattern(format));returnlocalDateTime2Date(ldt);}catch(Exceptione){logger.error("error while parse date:"+date,e);}returnnull;}/**
* convert date str to yyyy-MM-dd HH:mm:ss format
*
* @param str date string
* @return yyyy-MM-dd HH:mm:ss format
*/publicstaticDatestringToDate(Stringstr){returnparse(str,Constants.YYYY_MM_DD_HH_MM_SS);}/**
* get seconds between two dates
*
* @param d1 date1
* @param d2 date2
* @return differ seconds
*/publicstaticlongdifferSec(Dated1,Dated2){if(d1==null||d2==null){return0;}return(long)Math.ceil(differMs(d1,d2)/1000.0);}/**
* get ms between two dates
*
* @param d1 date1
* @param d2 date2
* @return differ ms
*/publicstaticlongdifferMs(Dated1,Dated2){returnMath.abs(d1.getTime()-d2.getTime());}/**
* get hours between two dates
*
* @param d1 date1
* @param d2 date2
* @return differ hours
*/publicstaticlongdiffHours(Dated1,Dated2){return(long)Math.ceil(diffMin(d1,d2)/60.0);}/**
* get minutes between two dates
*
* @param d1 date1
* @param d2 date2
* @return differ minutes
*/publicstaticlongdiffMin(Dated1,Dated2){return(long)Math.ceil(differSec(d1,d2)/60.0);}/**
* get the date of the specified date in the days before and after
*
* @param date date
* @param day day
* @return the date of the specified date in the days before and after
*/publicstaticDategetSomeDay(Datedate,intday){Calendarcalendar=Calendar.getInstance();calendar.setTime(date);calendar.add(Calendar.DATE,day);returncalendar.getTime();}/**
* get the hour of day.
*
* @param date date
* @return hour of day
*/publicstaticintgetHourIndex(Datedate){Calendarcalendar=Calendar.getInstance();calendar.setTime(date);returncalendar.get(Calendar.HOUR_OF_DAY);}/**
* compare two dates
*
* @param future future date
* @param old old date
* @return true if future time greater than old time
*/publicstaticbooleancompare(Datefuture,Dateold){returnfuture.getTime()>old.getTime();}/**
* convert schedule string to date
*
* @param schedule schedule
* @return convert schedule string to date
*/publicstaticDategetScheduleDate(Stringschedule){returnstringToDate(schedule);}/**
* format time to readable
*
* @param ms ms
* @return format time
*/publicstaticStringformat2Readable(longms){longdays=ms/(1000*60*60*24);longhours=(ms%(1000*60*60*24))/(1000*60*60);longminutes=(ms%(1000*60*60))/(1000*60);longseconds=(ms%(1000*60))/1000;returnString.format("%02d %02d:%02d:%02d",days,hours,minutes,seconds);}/**
* get monday
*
* note: Set the first day of the week to Monday, the default is Sunday
* @param date date
* @return get monday
*/publicstaticDategetMonday(Datedate){Calendarcal=Calendar.getInstance();cal.setTime(date);cal.setFirstDayOfWeek(Calendar.MONDAY);cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);returncal.getTime();}/**
* get sunday
*
* note: Set the first day of the week to Monday, the default is Sunday
* @param date date
* @return get sunday
*/publicstaticDategetSunday(Datedate){Calendarcal=Calendar.getInstance();cal.setTime(date);cal.setFirstDayOfWeek(Calendar.MONDAY);cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);returncal.getTime();}/**
* get first day of month
*
* @param date date
* @return first day of month
* */publicstaticDategetFirstDayOfMonth(Datedate){Calendarcal=Calendar.getInstance();cal.setTime(date);cal.set(Calendar.DAY_OF_MONTH,1);returncal.getTime();}/**
* get some hour of day
*
* @param date date
* @param offsetHour hours
* @return some hour of day
* */publicstaticDategetSomeHourOfDay(Datedate,intoffsetHour){Calendarcal=Calendar.getInstance();cal.setTime(date);cal.set(Calendar.HOUR_OF_DAY,cal.get(Calendar.HOUR_OF_DAY)+offsetHour);cal.set(Calendar.MINUTE,0);cal.set(Calendar.SECOND,0);cal.set(Calendar.MILLISECOND,0);returncal.getTime();}/**
* get last day of month
*
* @param date date
* @return get last day of month
*/publicstaticDategetLastDayOfMonth(Datedate){Calendarcal=Calendar.getInstance();cal.setTime(date);cal.add(Calendar.MONTH,1);cal.set(Calendar.DAY_OF_MONTH,1);cal.add(Calendar.DAY_OF_MONTH,-1);returncal.getTime();}/**
* return YYYY-MM-DD 00:00:00
*
* @param inputDay date
* @return start day
*/publicstaticDategetStartOfDay(DateinputDay){Calendarcal=Calendar.getInstance();cal.setTime(inputDay);cal.set(Calendar.HOUR_OF_DAY,0);cal.set(Calendar.MINUTE,0);cal.set(Calendar.SECOND,0);cal.set(Calendar.MILLISECOND,0);returncal.getTime();}/**
* return YYYY-MM-DD 23:59:59
*
* @param inputDay day
* @return end of day
*/publicstaticDategetEndOfDay(DateinputDay){Calendarcal=Calendar.getInstance();cal.setTime(inputDay);cal.set(Calendar.HOUR_OF_DAY,23);cal.set(Calendar.MINUTE,59);cal.set(Calendar.SECOND,59);cal.set(Calendar.MILLISECOND,999);returncal.getTime();}/**
* return YYYY-MM-DD 00:00:00
*
* @param inputDay day
* @return start of hour
*/publicstaticDategetStartOfHour(DateinputDay){Calendarcal=Calendar.getInstance();cal.setTime(inputDay);cal.set(Calendar.MINUTE,0);cal.set(Calendar.SECOND,0);cal.set(Calendar.MILLISECOND,0);returncal.getTime();}/**
* return YYYY-MM-DD 23:59:59
*
* @param inputDay day
* @return end of hour
*/publicstaticDategetEndOfHour(DateinputDay){Calendarcal=Calendar.getInstance();cal.setTime(inputDay);cal.set(Calendar.MINUTE,59);cal.set(Calendar.SECOND,59);cal.set(Calendar.MILLISECOND,999);returncal.getTime();}/**
* get current date
* @return current date
*/publicstaticDategetCurrentDate(){returnDateUtils.parse(DateUtils.getCurrentTime(),Constants.YYYY_MM_DD_HH_MM_SS);}/**
* get date
* @param date date
* @param calendarField calendarField
* @param amount amount
* @return date
*/publicstaticDateadd(finalDatedate,finalintcalendarField,finalintamount){if(date==null){thrownewIllegalArgumentException("The date must not be null");}finalCalendarc=Calendar.getInstance();c.setTime(date);c.add(calendarField,amount);returnc.getTime();}}