| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- package com.goafanti.common.utils;
- import com.goafanti.common.constant.AFTConstants;
- import com.goafanti.common.error.BusinessException;
- import org.apache.commons.lang3.time.DateFormatUtils;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.*;
- import java.util.Calendar;
- import java.util.Date;
- public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
- public static final String YEAR_SPAN = "yearSpan";
- public static final String QUARTER_SPAN = "quarterSpan";
- public static final String MONTH_SPAN = "monthSpan";
- public static final String WEEK_SPAN = "weekSpan";
- public static final String DAY_SPAN = "daySpan";
- 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"};
- /**
- * Determines how two dates compare up to no more than the specified most
- * significant field.
- *
- * @param date1
- * the first date, not <code>null</code>
- * @param date2
- * the second date, not <code>null</code>
- * @param field
- * the field from <code>Calendar</code>
- * @return diff millis
- * @throws IllegalArgumentException
- * if any argument is <code>null</code>
- * @see #truncate(Calendar, int)
- * @see #truncatedCompareTo(Date, Date, int)
- * @since 3.0
- */
- public static long truncatedDiffTo(final Date date1, final Date date2, final int field) {
- final Date truncatedDate1 = truncate(date1, field);
- final Date truncatedDate2 = truncate(date2, field);
- long thisTime = truncatedDate1.getTime();
- long anotherTime = truncatedDate2.getTime();
- return Math.abs(thisTime - anotherTime);
- }
- /**
- * Determines how two dates compare up to no more than the specified most
- * significant field.
- *
- * @param date1
- * the first date, not <code>null</code>
- * @param date2
- * the second date, not <code>null</code>
- * the field from <code>Calendar</code>
- * @return diff millis
- * @throws IllegalArgumentException
- * if any argument is <code>null</code>
- * @see #truncate(Calendar, int)
- * @see #truncatedCompareTo(Date, Date, int)
- * @since 3.0
- */
- public static long truncatedHourDiffTo(final Date date1, final Date date2) {
- return truncatedDiffTo(date1, date2, Calendar.HOUR) / 3600000;
- }
- /**
- * Determines how two dates compare up to no more than the specified most
- * significant field.
- *
- * @param date1
- * the first date, not <code>null</code>
- * @param date2
- * the second date, not <code>null</code>
- * the field from <code>Calendar</code>
- * @return diff millis
- * @throws IllegalArgumentException
- * if any argument is <code>null</code>
- * @see #truncate(Calendar, int)
- * @see #truncatedCompareTo(Date, Date, int)
- * @since 3.0
- */
- public static long truncatedMinuteDiffTo(final Date date1, final Date date2) {
- return truncatedDiffTo(date1, date2, Calendar.MINUTE) / 60000;
- }
- /**
- * 得到某年某周的第一天
- *
- * @param year
- * @param week
- * @return
- */
- public static Date getFirstDayOfWeek(int year, int week) {
- week = week - 1;
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.YEAR, year);
- calendar.set(Calendar.MONTH, Calendar.JANUARY);
- calendar.set(Calendar.DATE, 1);
- Calendar cal = (Calendar) calendar.clone();
- cal.add(Calendar.DATE, week * 7);
- return getFirstDayOfWeek(cal.getTime());
- }
- /**
- * 得到某年某周的最后一天
- *
- * @param year
- * @param week
- * @return
- */
- public static Date getLastDayOfWeek(int year, int week) {
- week = week - 1;
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.YEAR, year);
- calendar.set(Calendar.MONTH, Calendar.JANUARY);
- calendar.set(Calendar.DATE, 1);
- Calendar cal = (Calendar) calendar.clone();
- cal.add(Calendar.DATE, week * 7);
- return getLastDayOfWeek(cal.getTime());
- }
- /**
- * 取得当前日期所在周的第一天
- *
- * @param date
- * @return
- */
- public static Date getFirstDayOfWeek(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setFirstDayOfWeek(Calendar.SUNDAY);
- calendar.setTime(date);
- calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); // Sunday
- setMidnight(calendar);
- return calendar.getTime();
- }
- /**
- * 取得当前日期所在周的最后一天
- *
- * @param date
- * @return
- */
- public static Date getLastDayOfWeek(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setFirstDayOfWeek(Calendar.SUNDAY);
- calendar.setTime(date);
- calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6); // Saturday
- setMidnight(calendar);
- return calendar.getTime();
- }
- /**
- * 取得当前日期所在周的前一周最后一天
- *
- * @param date
- * @return
- */
- public static Date getLastDayOfLastWeek(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return getLastDayOfWeek(calendar.get(Calendar.YEAR), calendar.get(Calendar.WEEK_OF_YEAR) - 1);
- }
- /**
- * 返回指定日期的月的第一天
- *
- * @return
- */
- public static Date getFirstDayOfMonth(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
- setMidnight(calendar);
- return calendar.getTime();
- }
- /**
- * 返回指定年月的月的第一天
- *
- * @param year
- * @param month
- * @return
- */
- public static Date getFirstDayOfMonth(Integer year, Integer month) {
- Calendar calendar = Calendar.getInstance();
- if (year == null) {
- year = calendar.get(Calendar.YEAR);
- }
- if (month == null) {
- month = calendar.get(Calendar.MONTH);
- }
- calendar.set(year, month, 1);
- setMidnight(calendar);
- return calendar.getTime();
- }
- /**
- * 返回指定日期的月的最后一天
- *
- * @return
- */
- public static Date getLastDayOfMonth(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
- calendar.roll(Calendar.DATE, -1);
- setMidnight(calendar);
- return calendar.getTime();
- }
- /**
- * 返回指定年月的月的最后一天
- *
- * @param year
- * @param month
- * @return
- */
- public static Date getLastDayOfMonth(Integer year, Integer month) {
- Calendar calendar = Calendar.getInstance();
- if (year == null) {
- year = calendar.get(Calendar.YEAR);
- }
- if (month == null) {
- month = calendar.get(Calendar.MONTH);
- }
- calendar.set(year, month, 1);
- calendar.roll(Calendar.DATE, -1);
- setMidnight(calendar);
- return calendar.getTime();
- }
- /**
- * 返回指定日期的上个月的最后一天
- *
- * @return
- */
- public static Date getLastDayOfLastMonth(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) - 1, 1);
- calendar.roll(Calendar.DATE, -1);
- setMidnight(calendar);
- return calendar.getTime();
- }
- /**
- * 返回指定日期的季的第一天
- *
- * @return
- */
- public static Date getFirstDayOfQuarter(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return getFirstDayOfQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
- }
- /**
- * 返回指定年季的季的第一天
- *
- * @param year
- * @param quarter
- * @return
- */
- public static Date getFirstDayOfQuarter(Integer year, Integer quarter) {
- Calendar calendar = Calendar.getInstance();
- Integer month = 0;
- if (quarter == 1) {
- month = 1 - 1;
- } else if (quarter == 2) {
- month = 4 - 1;
- } else if (quarter == 3) {
- month = 7 - 1;
- } else if (quarter == 4) {
- month = 10 - 1;
- } else {
- month = calendar.get(Calendar.MONTH);
- }
- return getFirstDayOfMonth(year, month);
- }
- /**
- * 返回指定日期的季的最后一天
- *
- * @return
- */
- public static Date getLastDayOfQuarter(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return getLastDayOfQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
- }
- /**
- * 返回指定年季的季的最后一天
- *
- * @param year
- * @param quarter
- * @return
- */
- public static Date getLastDayOfQuarter(Integer year, Integer quarter) {
- Calendar calendar = Calendar.getInstance();
- Integer month = 0;
- if (quarter == 1) {
- month = 3 - 1;
- } else if (quarter == 2) {
- month = 6 - 1;
- } else if (quarter == 3) {
- month = 9 - 1;
- } else if (quarter == 4) {
- month = 12 - 1;
- } else {
- month = calendar.get(Calendar.MONTH);
- }
- return getLastDayOfMonth(year, month);
- }
- /**
- * 返回指定日期的上一季的最后一天
- *
- * @return
- */
- public static Date getLastDayOfLastQuarter(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return getLastDayOfLastQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
- }
- /**
- * 返回指定年季的上一季的最后一天
- *
- * @param year
- * @param quarter
- * @return
- */
- public static Date getLastDayOfLastQuarter(Integer year, Integer quarter) {
- Calendar calendar = Calendar.getInstance();
- Integer month = 0;
- if (quarter == 1) {
- month = 12 - 1;
- } else if (quarter == 2) {
- month = 3 - 1;
- } else if (quarter == 3) {
- month = 6 - 1;
- } else if (quarter == 4) {
- month = 9 - 1;
- } else {
- month = calendar.get(Calendar.MONTH);
- }
- return getLastDayOfMonth(year, month);
- }
- /**
- * 返回指定日期的季度
- *
- * @param date
- * @return
- */
- public static int getQuarterOfYear(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.MONTH) / 3 + 1;
- }
- /**
- * 返回昨天日期
- * @return date
- */
- public static Date getYesterday() {
- Calendar calendar = Calendar.getInstance();
- calendar.add(Calendar.DATE, -1);
- setMidnight(calendar);
- return calendar.getTime();
- }
- /**
- * 返回前一年最后一天
- * @param date
- * @return
- */
- public static Date getLastDayOfLastYear(Date date) {
- Calendar calendar = Calendar.getInstance();
- return getLastDayOfYear(calendar.get(Calendar.YEAR) - 1);
- }
- /**
- * 返回指定年的最后一天
- * @param year
- * @return
- */
- public static Date getLastDayOfYear(int year) {
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.YEAR, year - 1);
- calendar.roll(Calendar.DAY_OF_YEAR, -1);
- setMidnight(calendar);
- return calendar.getTime();
- }
- /**
- * 格式化时间
- * @param date
- * @param pattern
- * @return
- */
- public static String formatDate(Date date, String pattern) {
- SimpleDateFormat format = new SimpleDateFormat(pattern);
- return format.format(date);
- }
- public static String formatDateYYYYMMddHHmm(Date date) {
- SimpleDateFormat format = new SimpleDateFormat(parsePatterns[2]);
- return format.format(date);
- }
- /**
- * 解析日期
- * @param source
- * @param pattern
- * @return
- * @throws ParseException
- */
- public static Date parseDate(String source, String pattern) throws ParseException {
- SimpleDateFormat format = new SimpleDateFormat(pattern);
- return format.parse(source);
- }
- /**
- * 时分秒毫秒归零
- * @param c
- * @return
- */
- public static Calendar setMidnight(Calendar c) {
- c.set(Calendar.HOUR_OF_DAY, 0);
- c.set(Calendar.MINUTE, 0);
- c.set(Calendar.SECOND, 0);
- c.set(Calendar.MILLISECOND, 0);
- return c;
- }
- /**
- * 时分秒毫秒归零
- * @return
- */
- public static Date getEarlyToday() {
- Calendar c = Calendar.getInstance();
- setMidnight(c);
- return c.getTime();
- }
- /**
- * 字符串转日期
- * @param date
- * @param format
- * @return
- * @throws ParseException
- */
- public static Date StringToDate( String date, String format) {
- Date date2;
- try {
- date2=new SimpleDateFormat(format).parse(date);
- } catch (ParseException e) {
- throw new BusinessException("时间转换失败,date="+date,e);
- }
- return date2;
- }
- public static final String parseDateToStr(final String format, final Date date) {
- return new SimpleDateFormat(format).format(date);
- }
- /**
- * 增加 LocalDate ==> Date
- */
- public static Date toDate(LocalDate temporalAccessor)
- {
- LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
- ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
- return Date.from(zdt.toInstant());
- }
- /**
- * 日期路径 即年/月/日 如2018/08/08
- */
- public static final String datePath()
- {
- Date now = new Date();
- return DateFormatUtils.format(now, "yyyy/MM/dd");
- }
- /**
- * 日期型字符串转化为日期 格式
- */
- public static Date parseDate(Object str)
- {
- if (str == null)
- {
- return null;
- }
- try
- {
- return parseDate(str.toString(), parsePatterns);
- }
- catch (ParseException e)
- {
- return null;
- }
- }
- /**
- * 计算距离今天天数
- * @param dates
- * @return
- */
- public static Integer sumDays(String dates) {
- Date date = StringToDate(dates, AFTConstants.YYYYMMDDHHMMSS);
- Date now=new Date();
- // long sum=now.getTime()-date.getTime();
- // long one =60*60*24*1000;
- // int days= Math.toIntExact(sum / one);
- int days=(int)((now.getTime()-date.getTime())/(1000*3600*24));
- return days;
- }
- }
|