zoukankan      html  css  js  c++  java
  • 关于日期通用方法源码


    根据时间,返回季度。

    /**
         * 根据时间,返回季度。
         * @param time
         * @return
         */
        public static String getQuarter(Timestamp time) {
            if (time == null)
                return null;
            int month = Integer.valueOf(time.toString().substring(5, 7));
            int quarter = (month - 1) / 3 + 1;
            if (quarter == 1)
                return Constant.QUARTER_1;
            else if (quarter == 2)
                return Constant.QUARTER_2;
            else if (quarter == 3)
                return Constant.QUARTER_3;
            else if (quarter == 4)
                return Constant.QUARTER_4;
            else
                return null;
        }
    
        public static Timestamp getQuarterFirst(Timestamp date, String quarter) {
            if (Constant.QUARTER_1.equals(quarter) || Constant.QUARTER_2.equals(quarter)
                    || Constant.QUARTER_3.equals(quarter) || Constant.QUARTER_4.equals(quarter))
                return Timestamp.valueOf(date.toString().substring(0, 5)
                        + String.format("%02d", ((Integer.valueOf(quarter) - 1) * 3 + 1)) + "-01 00:00:00.0");
            else
                return null;
        }
    
        public static Timestamp getQuarterLast(Timestamp date, String quarter) {
            Timestamp first = getQuarterFirst(date, quarter);
            if (first != null)
                return GjCommonUtil.getDayLast(CommonUtil.addDay(CommonUtil.addMonth(first, 3), -1));
            else
                return null;
        }
    
        public static Timestamp changeSecond(int addValue) {
            return addSecond(Timestamp.valueOf("1970-01-01 08:00:00.0"), addValue);
        }

    加秒

        /**
         * 加秒
         * @param val
         * @param addValue
         * @return
         */
        public static Timestamp addSecond(Timestamp val, int addValue) {
            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(val);
            calendar.add(Calendar.SECOND, addValue);
            return new Timestamp(calendar.getTime().getTime());
        }

    计算两个日期查多少天

        /**
         * 计算两个日期查多少天
         * @param beginTime
         * @param endTime
         * @return
         */
        public static long dateDiff(Timestamp beginTime, Timestamp endTime) {
            long rtn = 0;
            long diff = endTime.getTime() - beginTime.getTime();
            rtn = diff / (1000 * 60 * 60 * 24) + 1;
            return rtn;
        }
    
    /**
         * 计算两个日期查多少天
         * @param beginTime
         * @param endTime
         * @return
         */
        public static Integer yearDiff(Timestamp beginTime, Timestamp endTime) {
            if (beginTime == null || endTime == null)
                return null;
            int diff = Integer.valueOf(endTime.toString().substring(0, 4)) * 12
                    + Integer.valueOf(endTime.toString().substring(5, 7))
                    - Integer.valueOf(beginTime.toString().substring(0, 4)) * 12
                    - Integer.valueOf(beginTime.toString().substring(5, 7));
            diff = diff / 12;
            return diff;
        }

    计算两个日期查多少月

        /**
         * 计算两个日期查多少月
         * @param beginTime
         * @param endTime
         * @return
         */
        public static int monthDiff(Timestamp beginTime, Timestamp endTime) {
            int diff = Integer.valueOf(endTime.toString().substring(0, 4)) * 12
                    + Integer.valueOf(endTime.toString().substring(5, 7))
                    - Integer.valueOf(beginTime.toString().substring(0, 4)) * 12
                    - Integer.valueOf(beginTime.toString().substring(5, 7));
            return diff;
        }

    计算两个日期查多少年

        /**
         * 计算两个日期查多少年
         * @param beginTime
         * @param endTime
         * @return
         */
        public static Integer yearDiff1(Timestamp beginTime, Timestamp endTime) {
            if (beginTime == null || endTime == null)
                return null;
            int diff = Integer.valueOf(endTime.toString().substring(0, 4)) * 12
                    - Integer.valueOf(beginTime.toString().substring(0, 4)) * 12;
            diff = diff / 12;
            return diff;
        }

    获取当前日期是周几

        * @param dt
        * @return 当前日期是周几
        */
        public static String getWeekOfDate(Timestamp whenDate) {
            String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
            Calendar cal = Calendar.getInstance();
            cal.setTime(whenDate);
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (w < 0)
                w = 0;
            return weekDays[w];
        }

    得到当前月份的第一天

    /**
         * 得到当前月份的第一天
         * 
         * @return
         */
        public static Timestamp getMonthFirst() {
            Timestamp curMonFirst = getMonthFirst(CommonUtil.getDate());
            return curMonFirst;
        }

    获取当年第一天

    /**
         * 获取当年第一天
         * 
         * @return
         */
        public static Timestamp getYearFirst() {
            return getYearFirst(CommonUtil.getDate());
        }

    获取给定日期所在年份的第一天.

    /**
         * 获取给定日期所在年份的第一天.
         * 
         * @param whenDate
         *            the when date
         * @return the year first
         */
        public static Timestamp getYearFirst(Timestamp whenDate) {
            if (whenDate == null)
                return getYearFirst();
            Timestamp yearfirst = Timestamp.valueOf(whenDate.toString().substring(0, 4) + "-01-01 00:00:00.0");
            return yearfirst;
        }

    获取当前年的最后一天

    /**
         * 获取当前年的最后一天
         * 
         * @return
         */
        public static Timestamp getYearLast() {
            return getYearLast(CommonUtil.getDate());
        }

    获取给定日期所在年份的最后一天.

    /**
         * 获取给定日期所在年份的最后一天.
         * 
         * @param whenDate
         *            the when date
         * @return the year last
         */
        public static Timestamp getYearLast(Timestamp whenDate) {
            if (whenDate == null)
                return getYearLast();
            return CommonUtil.addDay(CommonUtil.addYear(getYearFirst(whenDate), 1), -1);
        }

    得到给定时间所在月份的第一天.

        /**
         * 得到给定时间所在月份的第一天.
         * 
         * @param timestamp
         *            the timestamp
         * @return the mon first
         */
        public static Timestamp getMonthFirst(Timestamp timestamp) {
            Timestamp monFirst = null;
            monFirst = Timestamp.valueOf(timestamp.toString().substring(0, 7) + "-01 00:00:00.0");
            return monFirst;
        }

    得到给定时间所在月份的最后一天.

        /**
         * 得到给定时间所在月份的最后一天.
         * 
         * @param timestamp
         *            the timestamp
         * @return the mon last
         */
        public static Timestamp getMonthLast(Timestamp timestamp) {
            Timestamp monLast = Timestamp.valueOf(timestamp.toString().substring(0, 7) + "-01 00:00:00.0");
            monLast = CommonDateUtil.addSecond(CommonUtil.addMonth(monLast, 1), -1);
            return monLast;
        }

    给定日期的日的0点0分0秒。

    /**
         * 给定日期的日的0点0分0秒。.
         * 
         * @param whenDate
         *            the when date
         * @return the day first
         */
        public static Timestamp getDayFirst(Timestamp whenDate) {
            if (whenDate == null)
                return null;
            return Timestamp.valueOf(whenDate.toString().substring(0, 10) + " 00:00:00.0");
        }

    给定日期的日的23点59分59秒。

    /**
         * 给定日期的日的23点59分59秒。.
         * 
         * @param whenDate
         *            the when date
         * @return the day last
         */
        public static Timestamp getDayLast(Timestamp whenDate) {
            if (whenDate == null) {
                return null;
            }
            return Timestamp.valueOf(whenDate.toString().substring(0, 10) + " 23:59:59.0");
        }

    计算时间差。

    /**
         * 计算时间差。.
         *
         * @param diffType  Y年  M月份   D天  H小时  MI分 SS秒
         * @param beginDate the begin date
         * @param endDate the end date
         * @return the date diff
         * @
         */
        public static Long getDateDiff(String diffType, Timestamp beginDate, Timestamp endDate) {
            Long diffValue = 0L;
            if (beginDate == null || endDate == null) {
                return diffValue;
            }
            if ("Y".equals(diffType)) {
                diffValue = Long.valueOf(endDate.toString().substring(0, 4))
                        - Long.valueOf(beginDate.toString().substring(0, 4));
            } else if ("M".equals(diffType)) {
                diffValue = Long.valueOf(endDate.toString().substring(0, 4)) * 12
                        - Long.valueOf(beginDate.toString().substring(0, 4)) * 12
                        + Long.valueOf(endDate.toString().substring(5, 7))
                        - Long.valueOf(beginDate.toString().substring(5, 7));
            } else if ("D".equals(diffType)) {
                Long diffNanos = endDate.getTime() - beginDate.getTime();
                diffValue = diffNanos / 1000 / 60 / 60 / 24;
            } else if ("H".equals(diffType)) {
                Long diffNanos = endDate.getTime() - beginDate.getTime();
                diffValue = diffNanos / 1000 / 60 / 60;
            } else if ("MI".equals(diffType)) {
                Long diffNanos = endDate.getTime() - beginDate.getTime();
                diffValue = diffNanos / 1000 / 60;
            } else if ("SS".equals(diffType)) {
                Long diffNanos = endDate.getTime() - beginDate.getTime();
                diffValue = diffNanos / 1000;
            }
            return diffValue;
        }

    取得timestamp到秒的字符串.

        /**
         * 取得timestamp到秒的字符串.
         *
         * @param date the date
         * @return the string
         */
        public static String timestampToStr(Timestamp date) {
            if (null == date)
                return "";
            String sdate = date.toString();
            return sdate.substring(0, 19);
        }

    将字符串转换为timestamp类型的.

        /**
         * 将字符串转换为timestamp类型的.
         * 
         * @param dateStr
         *            the date str
         * @return the timestamp
         */
        public static Timestamp strToTimeStamp(String dateStr) {
            try {
                if (dateStr.length() < 15)
                    dateStr += " 00:00:00";
                else if (dateStr.length() == 16)
                    dateStr += ":00";
                return Timestamp.valueOf(dateStr);
            } catch (Exception e) {
                return null;
            }
        }

    获取现在日期时间,返回字符串格式 yyyy-MM-dd HH:mm:ss

        public static String getNowDateTimeStr() {
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateTimeString = formatter.format(currentTime);
            return dateTimeString;
        }

    日期时间字符串转日期时间格式,返回日期时间格式

        public static Date strToDateTime(String strDateTime) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ParsePosition pos = new ParsePosition(0);
            Date strtodate = formatter.parse(strDateTime, pos);
            return strtodate;
        }

    两个日期时间是否在跨度之内

        /** 
         * @date: 2015年12月15日 上午9:22:47 
         * @description:两个日期时间是否在跨度之内 
         * @parameter:  gapType 跨度类型,如Calendar.YEAR,Calendar.MONTH,Calendar.DAY_OF_YEAR 
         * @parameter:  maxGap  最大跨度值 
         * @return:  返回日期格式
        **/
        public static boolean isWithInDateGap(String startDate, String endDate,  
                int gapType, int maxGap){  
            Date startDateTime = null;  
            Date endDateTime = null;  
            startDateTime = strToDateTime(startDate);  
            endDateTime = strToDateTime(endDate);  
            return isWithInDateGap(startDateTime,endDateTime, gapType, maxGap);  
        }
    
            public static boolean isWithInDateGap(Date startDate, Date endDate,  
                    int gapType, int maxGap) {  
            if (startDate == null) {  
                throw new IllegalArgumentException("The startDate must not be null");  
            }  
            if (endDate == null) {  
                throw new IllegalArgumentException("The endDate must not be null");  
            }  
            if (gapType != Calendar.YEAR && gapType != Calendar.MONTH  
                    && gapType != Calendar.DAY_OF_YEAR) {  
                throw new IllegalArgumentException(  
                        "The value of gapType is invalid");  
            }  
    
            Calendar start = Calendar.getInstance();  
            start.setTime(startDate);  
            start.add(gapType, maxGap);  
            int compare = start.getTime().compareTo(endDate);  
            return compare >= 0;  
        }  
    

    部分内容参考链接:
    Java我的高效编程之常用函数

  • 相关阅读:
    动态规划专题(二)——树形DP
    动态规划专题(一)——状压DP
    位运算相关(二)——位运算的简单变换操作
    位运算相关(一)——位运算学习笔记
    2018.10.05 TOPOI提高组模拟赛 解题报告
    【BZOJ1088】[SCOI2005] 扫雷Mine(分类讨论)
    【洛谷1273】有线电视网(树上背包)
    【洛谷2264】情书(字符串水题)
    【洛谷4287】[SHOI2011] 双倍回文(Manacher算法经典题)
    【洛谷2051】[AHOI2009] 中国象棋(烦人的动态规划)
  • 原文地址:https://www.cnblogs.com/aixing/p/13327575.html
Copyright © 2011-2022 走看看