zoukankan      html  css  js  c++  java
  • java Dateutil 操作类

    方法内容:

    1 yyyy-MM-dd 格式化时间

    2 yyyy-MM-dd HH:mm:ss 格式化时间

    3 获得年份

    4 获得月份

    5 获得日期

    6 当前时间的加减操作

    7 两个时间的差值操作

    package com.tenyears.common.utils;
     
    import org.apache.commons.lang3.StringUtils;
     
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.ZoneId;
    import java.time.temporal.ChronoUnit;
    import java.util.Calendar;
    import java.util.Date;
     
     
    public class DateUtil {
        public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        public static SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
     
        public static Date getCurDate() {
            Date currDate = new Date();
            return currDate;
        }
     
        public static String format(Date date) {
            if (date != null) {
                return format.format(date);
            }
            return null;
        }
     
        public static String format2(Date date) {
            if (date != null) {
                return format2.format(date);
            }
            return null;
        }
     
        public static Date parse2(String dateStr) {
            if (StringUtils.isNoneBlank(dateStr))
                try {
                    return format2.parse(dateStr);
                } catch (ParseException e) {
                    LogerUtil.error(LogerUtil.getPrintStr(e));
                }
            return null;
        }
     
        public static Date parse(String dateStr) {
            if (StringUtils.isNoneBlank(dateStr))
                try {
                    return format.parse(dateStr);
                } catch (ParseException e) {
                    LogerUtil.error(LogerUtil.getPrintStr(e));
                }
            return null;
        }
     
        /**
         * 
         * @param date
         * @return date对应的年
         */
        public static int getYear(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.YEAR);
        }
     
        public static int getNowYear() {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            return calendar.get(Calendar.YEAR);
        }
     
        /**
         * 
         * @param date
         * @return date对应的月
         */
        public static int getMonth(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.MONTH) + 1;
        }
     
        public static int getNowMonth() {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            return calendar.get(Calendar.MONTH) + 1;
        }
     
        /**
         * @param date
         * @return 对应的天
         */
        public static int getDay(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.DAY_OF_MONTH);
        }
     
        public static int getNowDay() {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            return calendar.get(Calendar.DAY_OF_MONTH);
        }
     
     
        /**
         * 时间加减
         * @param date 时间
         * @param calendarType Calendar.Year / Calendar.Month.....
         * @param value value
         * @return
         */
        public static Date add(Date date,int calendarType,int value)
        {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(calendarType, value);//当前时间减去一年,即一年前的时间
            return calendar.getTime();//获取一年前的时间,或者一个月前的时间
        }
     
        /**
         * 得到两个时间的差值
         * @param d1
         * @param d2
         * @param chronoUnit  ChronoUnit.Year / ChronoUnit.Month....
         * @return
         */
        public static long subDate(Date d1,Date d2,ChronoUnit chronoUnit)
        {
            LocalDate afterDate = d1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate beforeDate = d2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            long diff = chronoUnit.between(beforeDate, afterDate);
            return diff;
        }
     
        /**
         * 得到两个时间相减的年数
         * @param d1
         * @param d2
         * @return
         */
        public static long subYears(Date d1,Date d2)
        {
            LocalDate afterDate = d1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate beforeDate = d2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            long diff = ChronoUnit.YEARS.between(beforeDate, afterDate);
            return diff;
        }
     
        /**
         * 得到两个时间相减的月数
         * @param d1
         * @param d2
         * @return
         */
        public static long subMonths(Date d1,Date d2)
        {
            LocalDate afterDate = d1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate beforeDate = d2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            long diff = ChronoUnit.MONTHS.between(beforeDate, afterDate);
            return diff;
        }
     
        /**
         * 得到两个时间相减的天数
         * @param d1
         * @param d2
         * @return
         */
        public static long subDays(Date d1,Date d2)
        {
            LocalDate afterDate = d1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate beforeDate = d2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            long diff = ChronoUnit.DAYS.between(beforeDate, afterDate);
            return diff;
        }
     
        /**
         * 得到两个时间相减的小时数
         * @param d1
         * @param d2
         * @return
         */
        public static long subHours(Date d1,Date d2)
        {
            LocalDate afterDate = d1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate beforeDate = d2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            long diff = ChronoUnit.HOURS.between(beforeDate, afterDate);
            return diff;
        }
        /**
         * 得到两个时间相减的分钟数
         * @param d1
         * @param d2
         * @return
         */
        public static long subMinutes(Date d1,Date d2)
        {
            LocalDate afterDate = d1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate beforeDate = d2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            long diff = ChronoUnit.MINUTES.between(beforeDate, afterDate);
            return diff;
        }
     
        /**
         * 得到两个时间相减的秒钟数
         * @param d1
         * @param d2
         * @return
         */
        public static long subSeconds(Date d1,Date d2)
        {
            LocalDate afterDate = d1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate beforeDate = d2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            long diff = ChronoUnit.SECONDS.between(beforeDate, afterDate);
            return diff;
        }
    }
  • 相关阅读:
    Linux Shell脚本详细教程
    linux下错误代码E212: Can't open file for writing
    github仓库的基本使用-创建、上传文件、删除
    -bash: ifconfig: command not found解决办法
    Xshell能ping通但连不上CentOS 7
    devtools和vuex mutations
    Google Chrome谷歌浏览器安装devtools
    Vuex状态管理模式
    vue Promise all
    vue Promise链式调用
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/15069930.html
Copyright © 2011-2022 走看看