`
services
  • 浏览: 78482 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

日期时间工具类

    博客分类:
  • java
SQL 
阅读更多
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;


public class DateBean {
    public static final String FORMAT_DAY = "yyyy-MM-dd";
    public static final String FORMAT_HOUR = "yyyy-MM-dd HH";
    public static final String FORMAT_MINUTE = "yyyy-MM-dd HH:mm";
    public static final String FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_MSEL = "yyyy-MM-dd HH:mm:ss.mmm";
    private static final int[] months = {
            31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };
    private static final int[] MONTH_DAYS = {29, 30};
    private final static int[][] DAYS_MONTH = {
            {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
            {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    private final static int[][] DATAS = {
            {23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0},
            {41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1},
            {30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1},
            {49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
            {38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
            {26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0},
            {45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},
            {35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
            {24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1},
            {43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1},
            {32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1},
            {21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
            {40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
            {28, 7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
            {47, 0, 6, 31, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1},
            {36, 0, 0, 36, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},
            {26, 5, 1, 41, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1},
            {44, 0, 3, 47, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1},
            {33, 0, 4, 52, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0},
            {23, 3, 5, 57, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1},
            {42, 0, 6, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1},
            {30, 8, 1, 8, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0},
            {48, 0, 2, 13, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0},
            {38, 0, 3, 18, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
            {27, 6, 4, 23, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0},
            {45, 0, 6, 29, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0},
            {35, 0, 0, 34, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1},
            {24, 4, 1, 39, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0},
            {43, 0, 2, 44, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0},
            {32, 0, 4, 50, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1},
            {20, 3, 5, 55, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0},
            {39, 0, 6, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0},
            {29, 7, 0, 5, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
            {47, 0, 2, 11, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
            {36, 0, 3, 16, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0},
            {26, 5, 4, 21, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1},
            {45, 0, 5, 26, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1},
            {33, 0, 0, 32, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1},
            {22, 4, 1, 37, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1},
            {41, 0, 2, 42, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1},
            {30, 8, 3, 47, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1},
            {48, 0, 5, 53, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1},
            {37, 0, 6, 58, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
            {27, 6, 0, 3, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0},
            {46, 0, 1, 8, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0},
            {35, 0, 3, 14, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1},
            {24, 4, 4, 19, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1},
            {43, 0, 5, 24, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1},
            {32, 10, 6, 29, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1},
            {50, 0, 1, 35, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0},
            {39, 0, 2, 40, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1},
            {28, 6, 3, 45, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0},
            {47, 0, 4, 50, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1},
            {36, 0, 6, 56, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0},
            {26, 5, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1},
            {45, 0, 1, 6, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0},
            {34, 0, 2, 11, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0},
            {22, 3, 4, 17, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0},
            {40, 0, 5, 22, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0},
            {30, 8, 6, 27, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1},
            {49, 0, 0, 32, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1},
            {37, 0, 2, 38, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1},
            {27, 5, 3, 43, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1},
            {46, 0, 4, 48, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1},
            {35, 0, 5, 53, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1},
            {23, 4, 0, 59, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
            {42, 0, 1, 4, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
            {31, 0, 2, 9, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0},
            {21, 2, 3, 14, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
            {39, 0, 5, 20, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
            {28, 7, 6, 25, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1},
            {48, 0, 0, 30, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1},
            {37, 0, 1, 35, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1},
            {25, 5, 3, 41, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
            {44, 0, 4, 46, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
            {33, 0, 5, 51, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
            {22, 4, 6, 56, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},
            {40, 0, 1, 2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},
            {30, 9, 2, 7, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1},
            {49, 0, 3, 12, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1},
            {38, 0, 4, 17, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0},
            {27, 6, 6, 23, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1},
            {46, 0, 0, 28, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0},
            {35, 0, 1, 33, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0},
            {24, 4, 2, 38, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
            {42, 0, 4, 44, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
            {31, 0, 5, 49, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0},
            {21, 2, 6, 54, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1},
            {40, 0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1},
            {28, 6, 2, 5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0},
            {47, 0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1},
            {36, 0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1},
            {25, 5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0},
            {43, 0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1},
            {32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0},
            {22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0}
    };

    /**
     * 根据指定的字符串用指定的格式进行转化
     *
     * @param format String 格式化字符串
     * @param dd     String      要格式化的日期
     * @return Date          返回java.util.Date日期格式
     * @throws BasicException
     */
    public static Date getDate(String format, String dd) {
        DateFormat formated = new SimpleDateFormat(format);
        Date date;

        try {
            date = formated.parse(dd);
        } catch (Exception e) {
            log.error("转化日期失败,'" + dd + "'格式不正确");

            return null;
        }

        return date;
    }

    /**
     * 日期格式化,根据字符串的格式自动格式化成日期格式(如果有分钟或者小时,则会自动转化),字符串必须合法
     *
     * @param dd String      要格式化的日期字符串
     * @return Date
     * @throws BasicException
     */
    public static Date getDate(String dd) {
        String format = FORMAT_DAY;
        String dd2 = dd;

        if (dd.indexOf(" ") != -1) {
            format = FORMAT_HOUR;
        }

        int index = dd.indexOf(":");
        int num = 1;

        while (index != -1) {
            switch (num) {
                case 1:
                    format = FORMAT_MINUTE;

                    break;

                case 2:
                    format = FORMAT_SECOND;

                    break;
            }

            dd2 = dd2.substring(index + 1);
            index = dd2.indexOf(":");
            num++;
        }

        return getDate(format, dd);
    }

    /**
     * 日期格式化,返回字符串
     *
     * @param date   Date 要格式化的日期
     * @param format String 格式化字符串
     * @return String
     */
    public static String getDateValue(Date date, String format) {
        if (date == null) {
            log.error("给定的日期为null");

            return "";
        }

        DateFormat formated = new SimpleDateFormat(format);

        return formated.format(date);
    }

    /**
     * 直接用默认的格式(yyyy-MM-dd)对日期进行格式化
     *
     * @param date Date
     * @return String
     */
    public static String getDateValue(Date date) {
        return getDateValue(date, "yyyy-MM-dd");
    }

    /**
     * 获取某个月指定的某天
     *
     * @param year int
     * @param month int
     * @param num   int 第几天
     * @return Date
     * @throws BasicException
     */
    public static java.sql.Date getDayOfMonth(int year, int month, int num) {
        if ((month == 2) && ((year % 4) == 0)) {
            if (num > 29) {
                num = 29;
            }
        } else {
            if (num > months[month - 1]) {
                num = months[month - 1];
            }
        }

        return getSqlDate(year + "-" + month + "-" + num);
    }

    /**
     * 根据每月指定的天数来获取对应的日期格式
     *
     * @param date 字符串日期
     * @param num 指定的天数(第几天)
     * @return
     * @throws BasicException
     */
    public static java.sql.Date getDayOfMonth(String date, int num) {
        Date dd = getDate(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dd);

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);

        return getDayOfMonth(year, month + 1, num);
    }

    /**
     * 取得某个月的第一天
     *
     * @param year int
     * @param month int
     * @return Date
     * @throws BasicException
     */
    public static java.sql.Date getFirstDayOfMonth(int year, int month) {
        return getDayOfMonth(year, month, 1);
    }

    /**
     * 获取某个月的第一天
     *
     * @param date String
     * @return Date
     * @throws BasicException
     */
    public static java.sql.Date getFirstDayOfMonth(String date) {
        return getDayOfMonth(date, 1);
    }

    /**
     * 根据年份、季度获取某个季度的第一天
     *
     * @param year    int
     * @param quarter int
     * @return Date
     */

    public static java.sql.Date getFirstDayOfQuarter(int year, int quarter) {
        int month;
        //得到季度的第一个月
        month = (quarter - 1) * 3 + 1;
        return getDayOfMonth(year, month, 1);
    }

    /**
     * 根据年份、季度获取某个季度的第一天
     *
     * @param year    int
     * @param quarter int
     * @return Date
     */

    public static java.sql.Date getLastDayOfQuarter(int year, int quarter) {
        int month;
        //得到季度的最后一个月
        month = quarter * 3;
        return getDayOfMonth(year, month, 31);
    }

    /**
     * 根据阴历取得对应的阳历日期(该日期必须在1936—2028年之间)
     *
     * @param date 阴历日期(字符串)
     * @return 返回对应的阳历日期
     * @throws Exception
     */
    public static Date getGregorianCalendar(String date) {
        Calendar c = getCalendar(date);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        int index = year - 1936;
        int endIndex = month;

        if ((DATAS[index][1] != 0) && (month > DATAS[index][1])) {
            endIndex++;
        }

        int dayNum = 0;

        for (int i = 0; i < (endIndex - 1); i++) {
            dayNum += MONTH_DAYS[DATAS[index][4 + i]];
        }

        dayNum += day;
        dayNum += DATAS[index][0];

        int year_days = 365;

        if (isLeapYear(year)) {
            year_days = 366;
        }

        if (dayNum > year_days) {
            year++;
            dayNum -= year_days;
        }

        month = 1;

        int[] dayOfMonth = DAYS_MONTH[0];

        if (isLeapYear(year)) {
            dayOfMonth = DAYS_MONTH[1];
        }

        int i = 0;

        for (; i < 12; i++) {
            dayNum -= dayOfMonth[i];

            if (dayNum <= 0) {
                break;
            }

            month++;
        }

        day = dayOfMonth[i] + dayNum;

        return getDate(year + "-" + month + "-" + day);
    }

    /**
     * 取得某个月的最后一天
     *
     * @param year int
     * @param month int
     * @return Date
     * @throws BasicException
     */
    public static java.sql.Date getLastDayOfMonth(int year, int month) {
        return getDayOfMonth(year, month, 32);
    }

    /**
     * 获取指定某个日期(date)所在的月份的最后一天
     *
     * @param date String 字符串日期格式
     * @return Date        java.sql.Date
     * @throws BasicException
     */
    public static java.sql.Date getLastDayOfMonth(String date) {
        return getDayOfMonth(date, 32);
    }

    /**
     * 根据阳历取得对应的阴历日期(该日期必须在1936—2028年之间)
     *
     * @param date 阳历日期(字符串)
     * @return 返回阴历日期
     * @throws Exception
     */
    public static Date getLunarCalendar(String date) throws Exception {
        Calendar calendar = getCalendar(date);
        int year = calendar.get(Calendar.YEAR);
        int month = 1;
        int day;

        if ((year < 1936) || (year > 2028)) {
            return null;
        }

        int index = year - 1936;
        int l_days = DATAS[index][0];
        int day_year = calendar.get(Calendar.DAY_OF_YEAR);
        int days;

        if (day_year >= l_days) {
            days = day_year - l_days;
        } else {
            index--;
            year--;

            Calendar c = getCalendar(year + "-12-31");
            days = (c.get((Calendar.DAY_OF_YEAR)) + day_year) -
                    DATAS[index][0];
        }

        int i = 0;
        int day_num = 0;

        for (; i < 13; i++) {
            day_num += MONTH_DAYS[DATAS[index][i + 4]];

            if (day_num >= days) {
                break;
            }

            month++;
        }

        day = MONTH_DAYS[DATAS[index][i + 4]] - (day_num - days);

        if ((DATAS[index][1] != 0) && (month > DATAS[index][1])) {
            month--;
        }

        return getDate(year + "-" + month + "-" + day);
    }

    /**
     * @param date String
     * @return Date
     */
    public static java.sql.Date getSqlDate(String date) {
        if(date==null||"".equals(date)) return null;
        Date dd = getDate("yyyy-MM-dd", date);
        //log.warn("没有指定格式化的类型,系统将按照默认的格式yyyy-MM-dd进行转换!");
        return new java.sql.Date(dd.getTime());
    }

    /**
     * @param s String
     * @return Timestamp
     * @throws BasicException
     */
    public static java.sql.Timestamp getTimestamp(String s) {
        Date date = getDate(s);

        return new java.sql.Timestamp(date.getTime());
    }

    /**
     * @param ts Timestamp
     * @return String
     */
    public static String getTimestampValue(java.sql.Timestamp ts) {
        if (ts == null) {
            return "";
        }

        return getDateValue(ts, "yyyy-MM-dd HH:mm:ss");
    }

    private static Calendar getCalendar(String date) {
        Date dd = getDate(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dd);

        return calendar;
    }

    private static boolean isLeapYear(int year) {
        if ((year % 400) == 0) {
            return true;
        } else if ((year % 100) == 0) {
            return false;
        } else if ((year % 4) == 0) {
            return true;
        }

        return false;
    }

    //获取两个日期间隔的天数
    public static int getDays(Date firstDate, Date endDate) {

        return (int) (endDate.getTime() - firstDate.getTime()) / (3600 * 24 * 1000);
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics