Java通用工具类(二)-----日期处理

来源:互联网 发布:安卓上传图片 php 编辑:程序博客网 时间:2024/04/30 01:54

日期处理

    /**
     * 中文格式日期
     * @param d 日期
     * @return
     */
    public static String getChineseDate(Date d)
    {
        if (d == null)
        {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd",
                new DateFormatSymbols());
        String dtrDate = df.format(d);
        return dtrDate.substring(0, 4) + "/u5E74"
                + Integer.parseInt(dtrDate.substring(4, 6)) + "/u6708"
                + Integer.parseInt(dtrDate.substring(6, 8)) + "/u65E5";

    }

    /**
     * 当前日期
     * @return
     */
    public static String getCurrentDate_String()
    {
        Calendar cal = Calendar.getInstance();
        String currentDate = null;

        String currentYear = (new Integer(cal.get(1))).toString();
        String currentMonth = null;
        String currentDay = null;
        if (cal.get(2) < 9)
            currentMonth = "0" + (new Integer(cal.get(2) + 1)).toString();
        else
            currentMonth = (new Integer(cal.get(2) + 1)).toString();
        if (cal.get(5) < 10)
            currentDay = "0" + (new Integer(cal.get(5))).toString();
        else
            currentDay = (new Integer(cal.get(5))).toString();
        currentDate = currentYear + currentMonth + currentDay;
        return currentDate;
    }

    /**
     * 取当前日期
     * @param strFormat 日期格式
     * @return
     */
    public static String getCurrentDate_String(String strFormat)
    {
        Calendar cal = Calendar.getInstance();
        Date d = cal.getTime();
        return getDate(d, strFormat);
    }

    /**
     * 返回两个年月之间间隔的月数
     * @param dealMonth
     * @param alterMonth
     * @return
     * @pre alterMonth != null
     * @pre dealMonth != null
     */
    public static int calBetweenTwoMonth(String dealMonth, String alterMonth)
    {
        int length = 0;
        if (dealMonth.length() != 6 || alterMonth.length() != 6)
        {
            // Helper.println("/u6BD4/u8F83/u5E74/u6708/u5B57/u7B26/u4E32/u7684/u957F/u5EA6/u4E0D/u6B63/u786E");
            length = -1;
        } else
        {
            int dealInt = Integer.parseInt(dealMonth);
            int alterInt = Integer.parseInt(alterMonth);
            if (dealInt < alterInt)
            {
                //  Helper.println("/u7B2C/u4E00/u4E2A/u5E74/u6708/u53D8/u91CF/u5E94/u5927/u4E8E/u6216/u7B49/u4E8E/u7B2C/u4E8C/u4E2A/u5E74/u6708/u53D8/u91CF");
                length = -2;
            } else
            {
                int dealYearInt = Integer.parseInt(dealMonth.substring(0, 4));
                int dealMonthInt = Integer.parseInt(dealMonth.substring(4, 6));
                int alterYearInt = Integer.parseInt(alterMonth.substring(0, 4));
                int alterMonthInt = Integer
                        .parseInt(alterMonth.substring(4, 6));
                length = (dealYearInt - alterYearInt) * 12
                        + (dealMonthInt - alterMonthInt);
            }
        }
        return length;
    }

    /**
     * 返回年
     * @param date
     * @return
     * @pre date != null
     */
    public static int convertDateToYear(Date date)
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy",
                new DateFormatSymbols());
        return Integer.parseInt(df.format(date));
    }

    /**
     * 返回年月
     * @param d
     * @return
     * @pre d != null
     */
    public static String convertDateToYearMonth(Date d)
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM",
                new DateFormatSymbols());
        return df.format(d);
    }

    /**
     * 返回年月日
     * @param d
     * @return
     * @pre d != null
     */
    public static String convertDateToYearMonthDay(Date d)
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd",
                new DateFormatSymbols());
        return df.format(d);
    }

    /**
     * 计算日期间隔的天数
     * @param beginDate 开始日期
     * @param endDate 结束日期
     * @return
     * @pre beginDate != null
     */
    public static int daysBetweenDates(Date beginDate, Date endDate)
    {
        int days = 0;
        Calendar calo = Calendar.getInstance();
        Calendar caln = Calendar.getInstance();
        calo.setTime(beginDate);
        caln.setTime(endDate);
        int oday = calo.get(6);
        int nyear = caln.get(1);
        for (int oyear = calo.get(1); nyear > oyear;)
        {
            calo.set(2, 11);
            calo.set(5, 31);
            days += calo.get(6);
            oyear++;
            calo.set(1, oyear);
        }

        int nday = caln.get(6);
        days = (days + nday) - oday;
        return days;
    }

    /**
     * 计算间隔天数后的日期
     * @param date
     * @param intBetween
     * @return
     * @pre date != null
     */
    public static Date getDateBetween(Date date, int intBetween)
    {
        Calendar calo = Calendar.getInstance();
        calo.setTime(date);
        calo.add(Calendar.DATE, intBetween);
        return calo.getTime();
    }

    /**
     * 计算指定天数后的日期
     * @param date
     * @param intBetween
     * @param strFromat 日期格式
     * @return
     */
    public static String getDateBetween_String(Date date, int intBetween,
            String strFromat)
    {
        Date dateOld = getDateBetween(date, intBetween);
        return getDate(dateOld, strFromat);
    }

    /**
     * 年月值增1
     * @param yearMonth
     * @return
     * @pre yearMonth != null
     * @throws java.lang.StringIndexOutOfBoundsException
     */
    public static String increaseYearMonth(String yearMonth)
    {
        int year = (new Integer(yearMonth.substring(0, 4))).intValue();
        int month = (new Integer(yearMonth.substring(4, 6))).intValue();
        if (++month <= 12 && month >= 10)
            return yearMonth.substring(0, 4) + (new Integer(month)).toString();
        if (month < 10)
            return yearMonth.substring(0, 4) + "0"
                    + (new Integer(month)).toString();

        return (new Integer(year + 1)).toString() + "0"
                + (new Integer(month - 12)).toString();
    }

    /**
     * 年月值增加指定的值
     * @param yearMonth 初始年月
     * @param addMonth 指定的值
     * @return
     * @pre yearMonth != null
     * @throws java.lang.StringIndexOutOfBoundsException
     */
    public static String increaseYearMonth(String yearMonth, int addMonth)
    {
        int year = (new Integer(yearMonth.substring(0, 4))).intValue();
        int month = (new Integer(yearMonth.substring(4, 6))).intValue();
        month += addMonth;
        year += month / 12;
        month %= 12;
        if (month <= 12 && month >= 10)
            return year + (new Integer(month)).toString();

        return year + "0" + (new Integer(month)).toString();
    }

    /**
     * 年月值减1
     * @param yearMonth
     * @return
     * @pre yearMonth != null
     * @throws java.lang.StringIndexOutOfBoundsException
     */
    public static String descreaseYearMonth(String yearMonth)
    {
        int year = (new Integer(yearMonth.substring(0, 4))).intValue();
        int month = (new Integer(yearMonth.substring(4, 6))).intValue();
        if (--month >= 10)
            return yearMonth.substring(0, 4) + (new Integer(month)).toString();
        if (month > 0 && month < 10)
            return yearMonth.substring(0, 4) + "0"
                    + (new Integer(month)).toString();

        return (new Integer(year - 1)).toString()
                + (new Integer(month + 12)).toString();
    }

    /**
     * 年月值增1
     * @param yearMonth
     * @return
     */
    public static String addYearMonth(String yearMonth)
    {
        int year = (new Integer(yearMonth.substring(0, 4))).intValue();
        int month = (new Integer(yearMonth.substring(4, 6))).intValue();
        if (++month >= 10 && month < 12)
            return yearMonth.substring(0, 4) + (new Integer(month)).toString();
        if (month > 0 && month < 10)
            return yearMonth.substring(0, 4) + "0"
                    + (new Integer(month)).toString();

        return (new Integer(year + 1)).toString() + "0"
                + (new Integer(month - 12)).toString();
    }

    /**
     * 转化成中文格式日期
     * @param d
     * @return
     */
    public static String getChineseDate_Date(Date d)
    {
        if (d == null)
        {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd",
                new DateFormatSymbols());
        String dtrDate = df.format(d);
        return dtrDate.substring(0, 4) + "/u5E74"
                + Integer.parseInt(dtrDate.substring(4, 6)) + "/u6708"
                + Integer.parseInt(dtrDate.substring(6, 8)) + "/u65E5";

    }

    /**
     * 当前日期
     * @return
     */
    public static Date getCurrentDate()
    {
        Calendar cal = Calendar.getInstance();
        Date d = cal.getTime();
        return d;
    }

    /**
     * 当前年月
     * @return
     */
    public static String getCurrentYearMonth()
    {
        Calendar cal = Calendar.getInstance();
        String currentYear = (new Integer(cal.get(1))).toString();
        String currentMonth = null;
        if (cal.get(2) < 9)
            currentMonth = "0" + (new Integer(cal.get(2) + 1)).toString();
        else
            currentMonth = (new Integer(cal.get(2) + 1)).toString();
        return currentYear + currentMonth;
    }

    /**
     * 当前年
     * @return
     */
    public static int getCurrentYear()
    {
        Calendar cal = Calendar.getInstance();
        int currentYear = cal.get(1);
        return currentYear;
    }

    /**
     * 字符转化成时间
     * @param strDate 时间字符串
     * @param oracleFormat 格式
     * @return
     */
    public static Date stringToDate(String strDate, String oracleFormat)
    {
        if (strDate == null)
            return null;
        Hashtable h = new Hashtable();
        String javaFormat = "";
        String s = oracleFormat.toLowerCase();
        if (s.indexOf("yyyy") != -1)
            h.put(new Integer(s.indexOf("yyyy")), "yyyy");
        else if (s.indexOf("yy") != -1)
            h.put(new Integer(s.indexOf("yy")), "yy");
        if (s.indexOf("mm") != -1)
            h.put(new Integer(s.indexOf("mm")), "MM");
        if (s.indexOf("dd") != -1)
            h.put(new Integer(s.indexOf("dd")), "dd");
        if (s.indexOf("hh24") != -1)
            h.put(new Integer(s.indexOf("hh24")), "HH");
        if (s.indexOf("mi") != -1)
            h.put(new Integer(s.indexOf("mi")), "mm");
        if (s.indexOf("ss") != -1)
            h.put(new Integer(s.indexOf("ss")), "ss");
        for (int intStart = 0; s.indexOf("-", intStart) != -1; intStart++)
        {
            intStart = s.indexOf("-", intStart);
            h.put(new Integer(intStart), "-");
        }

        for (int intStart = 0; s.indexOf("/", intStart) != -1; intStart++)
        {
            intStart = s.indexOf("/", intStart);
            h.put(new Integer(intStart), "/");
        }

        for (int intStart = 0; s.indexOf(" ", intStart) != -1; intStart++)
        {
            intStart = s.indexOf(" ", intStart);
            h.put(new Integer(intStart), " ");
        }

        for (int intStart = 0; s.indexOf(":", intStart) != -1; intStart++)
        {
            intStart = s.indexOf(":", intStart);
            h.put(new Integer(intStart), ":");
        }

        if (s.indexOf("/u5E74") != -1)
            h.put(new Integer(s.indexOf("/u5E74")), "/u5E74");
        if (s.indexOf("/u6708") != -1)
            h.put(new Integer(s.indexOf("/u6708")), "/u6708");
        if (s.indexOf("/u65E5") != -1)
            h.put(new Integer(s.indexOf("/u65E5")), "/u65E5");
        if (s.indexOf("/u65F6") != -1)
            h.put(new Integer(s.indexOf("/u65F6")), "/u65F6");
        if (s.indexOf("/u5206") != -1)
            h.put(new Integer(s.indexOf("/u5206")), "/u5206");
        if (s.indexOf("/u79D2") != -1)
            h.put(new Integer(s.indexOf("/u79D2")), "/u79D2");
        int i = 0;
        while (h.size() != 0)
        {
            Enumeration e = h.keys();
            int n = 0;
            while (e.hasMoreElements())
            {
                i = ((Integer) e.nextElement()).intValue();
                if (i >= n)
                    n = i;
            }
            String temp = (String) h.get(new Integer(n));
            h.remove(new Integer(n));
            javaFormat = temp + javaFormat;
        }
        SimpleDateFormat df = new SimpleDateFormat(javaFormat);
        Date myDate = new Date();
        try
        {
            myDate = df.parse(strDate);
        } catch (Exception e)
        {
            return null;
        }
        return myDate;
    }

    /**
     * 日期转化成字符串
     * @param d 日期
     * @param format 格式
     * @return
     */
    public static String dateToString(Date d, String format)
    {
        if (d == null)
            return "";
        Hashtable h = new Hashtable();
        String javaFormat = "";
        String s = format.toLowerCase();
        if (s.indexOf("yyyy") != -1)
            h.put(new Integer(s.indexOf("yyyy")), "yyyy");
        else if (s.indexOf("yy") != -1)
            h.put(new Integer(s.indexOf("yy")), "yy");
        if (s.indexOf("mm") != -1)
            h.put(new Integer(s.indexOf("mm")), "MM");
        if (s.indexOf("dd") != -1)
            h.put(new Integer(s.indexOf("dd")), "dd");
        if (s.indexOf("hh24") != -1)
            h.put(new Integer(s.indexOf("hh24")), "HH");
        if (s.indexOf("mi") != -1)
            h.put(new Integer(s.indexOf("mi")), "mm");
        if (s.indexOf("ss") != -1)
            h.put(new Integer(s.indexOf("ss")), "ss");
        for (int intStart = 0; s.indexOf("-", intStart) != -1; intStart++)
        {
            intStart = s.indexOf("-", intStart);
            h.put(new Integer(intStart), "-");
        }

        for (int intStart = 0; s.indexOf("/", intStart) != -1; intStart++)
        {
            intStart = s.indexOf("/", intStart);
            h.put(new Integer(intStart), "/");
        }

        for (int intStart = 0; s.indexOf(" ", intStart) != -1; intStart++)
        {
            intStart = s.indexOf(" ", intStart);
            h.put(new Integer(intStart), " ");
        }

        for (int intStart = 0; s.indexOf(":", intStart) != -1; intStart++)
        {
            intStart = s.indexOf(":", intStart);
            h.put(new Integer(intStart), ":");
        }

        if (s.indexOf("/u5E74") != -1)
            h.put(new Integer(s.indexOf("/u5E74")), "/u5E74");
        if (s.indexOf("/u6708") != -1)
            h.put(new Integer(s.indexOf("/u6708")), "/u6708");
        if (s.indexOf("/u65E5") != -1)
            h.put(new Integer(s.indexOf("/u65E5")), "/u65E5");
        if (s.indexOf("/u65F6") != -1)
            h.put(new Integer(s.indexOf("/u65F6")), "/u65F6");
        if (s.indexOf("/u5206") != -1)
            h.put(new Integer(s.indexOf("/u5206")), "/u5206");
        if (s.indexOf("/u79D2") != -1)
            h.put(new Integer(s.indexOf("/u79D2")), "/u79D2");
        int i = 0;
        while (h.size() != 0)
        {
            Enumeration e = h.keys();
            int n = 0;
            while (e.hasMoreElements())
            {
                i = ((Integer) e.nextElement()).intValue();
                if (i >= n)
                    n = i;
            }
            String temp = (String) h.get(new Integer(n));
            h.remove(new Integer(n));
            javaFormat = temp + javaFormat;
        }
        SimpleDateFormat df = new SimpleDateFormat(javaFormat,
                new DateFormatSymbols());
        return df.format(d);
    }

    /**
     * 格式化日期
     * @param d 日期
     * @param format 格式
     * @return
     */
    public static String getDate(Date d, String format)
    {
        if (d == null)
            return "";
        Hashtable h = new Hashtable();
        String javaFormat = "" ;
        String s = format.toLowerCase();
        if (s.indexOf("yyyy") != -1)
            h.put(new Integer(s.indexOf("yyyy")), "yyyy");
        else if (s.indexOf("yy") != -1)
            h.put(new Integer(s.indexOf("yy")), "yy");
        if (s.indexOf("mm") != -1)
            h.put(new Integer(s.indexOf("mm")), "MM");
        if (s.indexOf("dd") != -1)
            h.put(new Integer(s.indexOf("dd")), "dd");
        if (s.indexOf("hh24") != -1)
            h.put(new Integer(s.indexOf("hh24")), "HH");
        if (s.indexOf("mi") != -1)
            h.put(new Integer(s.indexOf("mi")), "mm");
        if (s.indexOf("ss") != -1)
            h.put(new Integer(s.indexOf("ss")), "ss");
        for (int intStart = 0; s.indexOf("-", intStart) != -1; intStart++)
        {
            intStart = s.indexOf("-", intStart);
            h.put(new Integer(intStart), "-");
        }

        for (int intStart = 0; s.indexOf("/", intStart) != -1; intStart++)
        {
            intStart = s.indexOf("/", intStart);
            h.put(new Integer(intStart), "/");
        }

        for (int intStart = 0; s.indexOf(" ", intStart) != -1; intStart++)
        {
            intStart = s.indexOf(" ", intStart);
            h.put(new Integer(intStart), " ");
        }

        for (int intStart = 0; s.indexOf(":", intStart) != -1; intStart++)
        {
            intStart = s.indexOf(":", intStart);
            h.put(new Integer(intStart), ":");
        }

        if (s.indexOf("/u5E74") != -1)
            h.put(new Integer(s.indexOf("/u5E74")), "/u5E74");
        if (s.indexOf("/u6708") != -1)
            h.put(new Integer(s.indexOf("/u6708")), "/u6708");
        if (s.indexOf("/u65E5") != -1)
            h.put(new Integer(s.indexOf("/u65E5")), "/u65E5");
        if (s.indexOf("/u65F6") != -1)
            h.put(new Integer(s.indexOf("/u65F6")), "/u65F6");
        if (s.indexOf("/u5206") != -1)
            h.put(new Integer(s.indexOf("/u5206")), "/u5206");
        if (s.indexOf("/u79D2") != -1)
            h.put(new Integer(s.indexOf("/u79D2")), "/u79D2");
        int i = 0;
        while (h.size() != 0)
        {
            Enumeration e = h.keys();
            int n = 0;
            while (e.hasMoreElements())
            {
                i = ((Integer) e.nextElement()).intValue();
                if (i >= n)
                    n = i;
            }
            String temp = (String) h.get(new Integer(n));
            h.remove(new Integer(n));
            javaFormat = temp + javaFormat;
        }
        SimpleDateFormat df = new SimpleDateFormat(javaFormat,
                new DateFormatSymbols());
        return df.format(d);
    }

    /**
     * 比较日期1是否大于等于日期2
     * @param s1 日期1
     * @param s2 日期2
     * @return
     */
    public static boolean yearMonthGreatEqual(String s1, String s2)
    {
        String temp1 = s1.substring(0, 4);
        String temp2 = s2.substring(0, 4);
        String temp3 = s1.substring(4, 6);
        String temp4 = s2.substring(4, 6);
        if (Integer.parseInt(temp1) > Integer.parseInt(temp2))
            return true;
        if (Integer.parseInt(temp1) == Integer.parseInt(temp2))
            return Integer.parseInt(temp3) >= Integer.parseInt(temp4);

        return false;
    }

    /**
     * 获得oracle格式的日期字符串
     * @param d 日期
     * @return
     */
    public static String getOracleFormatDateStr(Date d)
    {
        return getDate(d, "YYYY-MM-DD HH24:MI:SS");
    }

    public static String getLastDay(String term)
    {
        int getYear = Integer.parseInt(term.substring(0, 4));
        int getMonth = Integer.parseInt(term.substring(4, 6));
        String getLastDay = "";
        if (getMonth == 2)
        {
            if (getYear % 4 == 0 && getYear % 100 != 0 || getYear % 400 == 0)
                getLastDay = "29";
            else
                getLastDay = "28";
        } else if (getMonth == 4 || getMonth == 6 || getMonth == 9
                || getMonth == 11)
            getLastDay = "30";
        else
            getLastDay = "31";
        return String.valueOf(getYear) + "/u5E74" + String.valueOf(getMonth)
                + "/u6708" + getLastDay + "/u65E5";
    }

    /**
     * 增加月
     * @param strDate 初始年月
     * @param intDiff 增加的数量
     * @return
     */
    public static String getMonthBetween(String strDate, int intDiff)
    {
        try
        {
            int intYear = Integer.parseInt(strDate.substring(0, 4));
            int intMonth = Integer.parseInt(strDate.substring(4, 6));
            String strDay = "";
            if (strDate.length() > 6)
                strDay = strDate.substring(6, strDate.length());
            for (intMonth += intDiff; intMonth <= 0; intMonth += 12)
                intYear--;

            for (; intMonth > 12; intMonth -= 12)
                intYear++;

            if (intMonth < 10)
            {
                return Integer.toString(intYear) + "0"
                        + Integer.toString(intMonth) + strDay;
            }
            return Integer.toString(intYear) + Integer.toString(intMonth)
                    + strDay;
        } catch (Exception e)
        {
            return "";
        }
    }

    /**
     * 计算两个年月之间的相差月数
     * @param strDateBegin
     * @param strDateEnd
     * @return
     */
    public static String getMonthBetween(String strDateBegin, String strDateEnd)
    {
        try
        {
            String strOut;
            if (strDateBegin.equals("") || strDateEnd.equals("")
                    || strDateBegin.length() != 6 || strDateEnd.length() != 6)
            {
                strOut = "";
            } else
            {
                int intMonthBegin = Integer.parseInt(strDateBegin.substring(0,
                        4))
                        * 12 + Integer.parseInt(strDateBegin.substring(4, 6));
                int intMonthEnd = Integer.parseInt(strDateEnd.substring(0, 4))
                        * 12 + Integer.parseInt(strDateEnd.substring(4, 6));
                strOut = String.valueOf(intMonthBegin - intMonthEnd);
            }
            return strOut;
        } catch (Exception e)
        {
            return "0";
        }
    }

    /**
     * 将“yyyymmdd”格式的日期转化成“yyyy-mm-dd”格式
     * @param strDate
     * @return
     */
    public static String getStrHaveAcross(String strDate)
    {
        try
        {
            return strDate.substring(0, 4) + "-" + strDate.substring(4, 6)
                    + "-" + strDate.substring(6, 8);
        } catch (Exception e)
        {
            return strDate;
        }
    }

    /**
     * 计算当前日期的下个月的第一天
     * @return
     */
    public static String getFirstDayOfNextMonth()
    {
        String strToday = getCurrentDate_String();
        return increaseYearMonth(strToday.substring(0, 6)) + "01";
    }

 

 
原创粉丝点击