• 日期工具类


      1 /**
      2  * 日期工具类
      3  *
      4  * @author 东篱
      5  * @date 2022-1-22
      6  */
      7 public class DateUtil {
      8 
      9     /**
     10      * 获取当前月开始时间
     11      *
     12      * @param date
     13      */
     14     public static Date beginOfMonth(Date date) {
     15         Calendar calendar = getCalendar();
     16         calendar.setTimeInMillis(date.getTime());
     17         calendar.add(Calendar.YEAR, 0);
     18         calendar.add(Calendar.MONTH, 0);
     19         calendar.set(Calendar.DAY_OF_MONTH, 1);
     20         calendar.set(Calendar.HOUR_OF_DAY, 0);
     21         calendar.set(Calendar.MINUTE, 0);
     22         calendar.set(Calendar.SECOND, 0);
     23 
     24         long timeInMillis = calendar.getTimeInMillis();
     25         return new Date(timeInMillis);
     26     }
     27 
     28     /**
     29      * 获取当前月结束时间
     30      *
     31      * @param date
     32      * @return
     33      */
     34     public static Date endOfMonth(Date date) {
     35         Calendar calendar = getCalendar();
     36         calendar.setTimeInMillis(date.getTime());
     37         calendar.add(Calendar.YEAR, 0);
     38         calendar.add(Calendar.MONTH, 0);
     39         calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
     40         calendar.set(Calendar.HOUR_OF_DAY, 23);
     41         calendar.set(Calendar.MINUTE, 59);
     42         calendar.set(Calendar.SECOND, 59);
     43 
     44         long timeInMillis = calendar.getTimeInMillis();
     45         return new Date(timeInMillis);
     46     }
     47 
     48     /**
     49      * 根据年月日获取日期
     50      *
     51      * @param year
     52      * @param month
     53      * @param day
     54      * @return
     55      */
     56     public static Date getDate(int year, int month, int day) {
     57         return dateStrParse(year + SettleConst.LINE + month + SettleConst.LINE + day, SettleConst.DATE_FORMAT);
     58     }
     59 
     60     /**
     61      * 根据年月日时分秒获取时间
     62      *
     63      * @param year
     64      * @param month
     65      * @param day
     66      * @param time
     67      * @return
     68      */
     69     public static Date getTime(int year, int month, int day, String time) {
     70         return dateStrParse(year + SettleConst.LINE + month + SettleConst.LINE + day + SettleConst.BLANK + time, SettleConst.TIME_FORMAT);
     71     }
     72 
     73     /**
     74      * 日期字符串转date
     75      *
     76      * @param str    日期字符串
     77      * @param format 格式
     78      * @return
     79      */
     80     public static Date dateStrParse(String str, String format) {
     81         SimpleDateFormat sdf = new SimpleDateFormat(format);
     82         Date parse = null;
     83         try {
     84             parse = sdf.parse(str);
     85         } catch (Exception e) {
     86             e.printStackTrace();
     87         }
     88         return parse;
     89     }
     90 
     91     /**
     92      * 日期转字符串
     93      *
     94      * @param date
     95      * @param format
     96      * @return
     97      */
     98     public static String dateFormat(Date date, String format) {
     99         SimpleDateFormat sdf = new SimpleDateFormat(format);
    100         String str = "";
    101         try {
    102             if (date != null){
    103                 str = sdf.format(date);
    104             }
    105         } catch (Exception e) {
    106             e.printStackTrace();
    107         }
    108         return str;
    109     }
    110 
    111     /**
    112      * 获取当前日期前某天或后某天
    113      *
    114      * @param date
    115      * @param days
    116      * @return
    117      */
    118     public static Date offsetDate(Date date, int days) {
    119         Calendar calendar = Calendar.getInstance();
    120         calendar.setTime(date);
    121         calendar.add(Calendar.DAY_OF_MONTH, days);
    122         return calendar.getTime();
    123     }
    124 
    125     /**
    126      * 获取当前日期月份
    127      *
    128      * @param date
    129      * @return
    130      */
    131     public static Integer month(Date date) {
    132         if (date == null) {
    133             return -1;
    134         }
    135         Calendar calendar = Calendar.getInstance();
    136         calendar.setTimeInMillis(date.getTime());
    137         return calendar.get(Calendar.MONTH) + 1;
    138     }
    139 
    140     /**
    141      * 获取当前日期是几号
    142      *
    143      * @param date
    144      * @return
    145      */
    146     public static Integer day(Date date) {
    147         Calendar calendar = Calendar.getInstance();
    148         calendar.setTimeInMillis(date.getTime());
    149         return calendar.get(Calendar.DATE);
    150     }
    151 
    152     /**
    153      * 获取当前日期年份
    154      *
    155      * @param date
    156      * @return
    157      */
    158     public static Integer year(Date date) {
    159         Calendar calendar = Calendar.getInstance();
    160         calendar.setTimeInMillis(date.getTime());
    161         return calendar.get(Calendar.YEAR);
    162     }
    163 
    164     /**
    165      * 获取当前季度开始时间
    166      *
    167      * @param date
    168      * @return
    169      */
    170     public static Date beginOfQuart(Date date) {
    171         Integer month = month(date);
    172         Integer quartStartMonth = 1;
    173 
    174         if (month >= 4 && month <= 6) {
    175             quartStartMonth = 4;
    176         } else if (month >= 7 && month <= 9) {
    177             quartStartMonth = 7;
    178         } else if (month >= 10 && month <= 12) {
    179             quartStartMonth = 10;
    180         }
    181 
    182         Calendar calendar = getCalendar();
    183         calendar.setTimeInMillis(date.getTime());
    184         calendar.add(Calendar.YEAR, 0);
    185         calendar.set(Calendar.MONTH, quartStartMonth - 1);
    186         calendar.set(Calendar.DAY_OF_MONTH, 1);
    187         calendar.set(Calendar.HOUR_OF_DAY, 0);
    188         calendar.set(Calendar.MINUTE, 0);
    189         calendar.set(Calendar.SECOND, 0);
    190 
    191         return new Date(calendar.getTimeInMillis());
    192     }
    193 
    194     /**
    195      * 获取当前季度结束时间
    196      *
    197      * @param date
    198      * @return
    199      */
    200     public static Date endOfQuart(Date date) {
    201         Integer month = month(date);
    202         Integer quartStartMonth = 3;
    203 
    204         if (month >= 4 && month <= 6) {
    205             quartStartMonth = 6;
    206         } else if (month >= 7 && month <= 9) {
    207             quartStartMonth = 9;
    208         } else if (month >= 10 && month <= 12) {
    209             quartStartMonth = 12;
    210         }
    211 
    212         Calendar calendar = getCalendar();
    213         calendar.setTimeInMillis(date.getTime());
    214         calendar.add(Calendar.YEAR, 0);
    215         calendar.set(Calendar.MONTH, quartStartMonth - 1);
    216         calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    217         calendar.set(Calendar.HOUR_OF_DAY, 23);
    218         calendar.set(Calendar.MINUTE, 59);
    219         calendar.set(Calendar.SECOND, 59);
    220 
    221         return new Date(calendar.getTimeInMillis());
    222     }
    223 
    224     public static Calendar getCalendar() {
    225         Calendar calendar = Calendar.getInstance();
    226         calendar.setTimeZone(TimeZone.getTimeZone(SettleConst.TIMEZONE));
    227         return calendar;
    228     }
    229 
    230     /**
    231      * 获取两个日期间隔的天数
    232      * @param minDate
    233      * @param maxDate
    234      * @return
    235      */
    236     public static Long betweenDay(Date minDate, Date maxDate) {
    237         long maxTime = maxDate == null ? 0L : maxDate.getTime();
    238         long minTime = minDate == null ? 0L : minDate.getTime();
    239 
    240         Long day = (maxTime - minTime) / (1000 * 3600 * 24);
    241         return day + 1;
    242     }
    243 
    244     /**
    245      * 向前/向后偏移月份
    246      * @param date
    247      * @param month
    248      * @return
    249      */
    250     public static Date offsetMonth(Date date, Integer month) {
    251         Calendar calendar = Calendar.getInstance();
    252         calendar.setTimeInMillis(date.getTime());
    253         calendar.add(Calendar.MONTH, month);
    254 
    255         return new Date(calendar.getTimeInMillis());
    256     }
    257 
    258 
    259     public static void main(String[] args) {
    260     }
    261 
    262 }
  • 相关阅读:
    项目常见异常
    mysql 存储过程中使用事物+事件定时执行存储过程
    Spring Mvc 配置 之 ContextLoaderListener
    Spring Boot 之 https
    Spring Boot 之 annotation注解
    用python打印99乘法口诀表
    gerrit代码审核工具之“error unpack failed error Missing unknown”错误解决思路
    在eclipse搭建python开发环境
    python2与python3语法区别之_重定向
    2_jenkins_git创建创建及项目构建
  • 原文地址:https://www.cnblogs.com/xuqiang7/p/16382260.html
Copyright © 2020-2023  润新知