DateUtil

public class GenernalDate {
    //一天秒数
    public final static long MILLISECONDS_OF_DAY = 1000 * 60 * 60 * 24;
    public final static int NULL_DATE = 19000101;
    private final static int days[] = {31,29,31,30,31,30,31,31,30,31,30,31};

    public final static int DATETIME_LENGTH = 14;
    public final static int DATE_LENGTH = 8;
    public final static int TIME_LENGTH = 6;
    public final static int TIME_LENGTH_2 = 5;//091213 中在数据库中存放的是91213



    /**
     * 日期转换
     * @param date 三种格式  20100421153055|20100421|153055
     * @return
     * @throws ParseException
     * @throws XdealException
     */
    public static Date parse(long date)throws BusinessException{
        String dateStr = String.valueOf(date);

        boolean isDate = false,isTime = false,isDateTime = false;
        int length = dateStr.length();
        if(length==DATETIME_LENGTH){
            isDateTime = true;
        }else if(length==DATE_LENGTH){
            isDate = true;
        }else if(length==TIME_LENGTH || length==TIME_LENGTH_2){
            isTime = true;
        }else {
            throw new BusinessException("Date format is not correct["+dateStr+"]");
        }
        SimpleDateFormat sf = (SimpleDateFormat) DateFormat.getDateInstance();
        try{
            if (isDateTime) { //日期时间
                sf.applyPattern("yyyyMMddHHmmss");
                return sf.parse(dateStr);
            } else if (isDate) {
                sf.applyPattern("yyyyMMdd");
                return sf.parse(dateStr);
            } else if (isTime) {
                sf.applyPattern("HHmmss");
                return sf.parse(dateStr);
            }
        }catch(ParseException pe){
            throw new BusinessException(pe.getMessage());
        }

        return new Date(0);
    }



    //--------------------显示格式化日期-------------------------------

    /**
     * 返回默认格式的字符串日期 时间
     * @param dateTime  日期+时间
     * @return yyyy-MM-dd HH:mm:ss格式的字符串日期 时间
     */
    public static String formatDateTime(Date dateTime){
        SimpleDateFormat sf = (SimpleDateFormat) DateFormat.getDateInstance();
        sf.applyPattern("yyyy-MM-dd HH:mm:ss");
        return sf.format(dateTime);
    }

    /**
     * 返回默认格式的字符串日期 时间
     * @param dateTime  日期+时间:20100421120101
     * @return yyyy-MM-dd HH:mm:ss格式的字符串日期 时间
     */
    public static String formatDateTime(long dateTime)throws BusinessException{
        SimpleDateFormat sf = (SimpleDateFormat) DateFormat.getDateInstance();
        sf.applyPattern("yyyy-MM-dd HH:mm:ss");
        return sf.format(parse(dateTime));
    }


    /**
     * 返回字符串日期
     * @param date
     * @return yyyy-MM-dd格式的字符串日期
     */
    public static String formatDate(Date date) {
        SimpleDateFormat sf = (SimpleDateFormat) DateFormat.getDateInstance();
        sf.applyPattern("yyyy-MM-dd");
        return sf.format(date);
    }

    /**
     * 返回字符串日期
     * @param date 日期 20100421
     * @return yyyy-MM-dd格式的字符串日期
     */
    public static String formatDate(int date)throws BusinessException {
        SimpleDateFormat sf = (SimpleDateFormat) DateFormat.getDateInstance();
        sf.applyPattern("yyyy-MM-dd");
        return sf.format(parse(date));
    }

    /**
     * 返回字符串时间
     * @param time
     * @return HH:mm:ss格式的字符串日期
     */
    public static String formatTime(Date time) {
        SimpleDateFormat sf = (SimpleDateFormat) DateFormat.getDateInstance();
        sf.applyPattern("HH:mm:ss");
        return sf.format(time);
    }

    /**
     * 返回字符串时间
     * @param time 时间 120130
     * @return HH:mm:ss格式的字符串日期
     */
    public static String formatTime(int time)throws BusinessException {
        StringBuffer buffer = new StringBuffer();
        buffer.append(time);
        int a = 6-buffer.length();
        for(int i=0;i<a;i++){
            buffer.insert(0, "0");        //在stringbuffer 的第一个位置添加  0
        }
         return formatTime(buffer.toString());
    }
    /**
     *
     * @param time
     * @return HH:mm:ss格式的字符串日期
     */
  private static String formatTime(String time){
    SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
    if(time.indexOf(":")!=-1){
        sf.applyPattern("HH:mm:ss");
    }else{
        sf.applyPattern("HHmmss");
    }
    Date date=null;
    try {
        date = sf.parse(time);
    } catch (ParseException e) {
        date=new Date();
    }
    sf.applyPattern("HH:mm:ss");
    return sf.format(date);
}

    /**
     * 返回日期
     * @param
     * @return int 20140523
     */
    public static int formatDateStrToDateInt(String datestr)throws RuntimeException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(datestr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(date == null){
            return 0;
        }
        return getNumericDate(date);
    }

    //------------------------读取日期--------------------------------------

    /**
     * 读取当前的日期+时间
     * return long 20100421132630
     */
    public static long getNumericDateTime(){
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("yyyyMMddHHmmss");
        String dateTime = sf.format(Calendar.getInstance().getTime());
        return Long.parseLong(dateTime);
    }

    /**
     * 读取当前的日期+时间
     * return String 2010-04-21 13:26:30
     */
    public static String getStringDateTime(){
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("yyyy-MM-dd HH:mm:ss");
        return sf.format(Calendar.getInstance().getTime());
    }

    /**
     * Date类型(日期+时间) 转换为 long 类型
     * @param dateTime
     * @return long 20100421132630
     */
    public static long getNumericDateTime(Date dateTime){
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("yyyyMMddHHmmss");
        return Long.parseLong(sf.format(dateTime));
    }

    /**
     * 读取当前日期
     * @return int 20100421
     */
    public static int getNumericDate(){
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("yyyyMMdd");
        String date = sf.format(Calendar.getInstance().getTime());
        return Integer.parseInt(date);
    }

    /**
     * 读取当前的日期
     * return String 2010-04-21
     */
    public static String getStringDate(){
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("yyyy-MM-dd");
        return sf.format(Calendar.getInstance().getTime());
    }

    /**
     * Date类型  转换为 int 类型
     * @param date
     * @return int 20100421
     */
    public static int getNumericDate(Date dateTime){
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("yyyyMMdd");
        return Integer.parseInt(sf.format(dateTime));
    }

    /**
     * String类型  转换为 int 类型
     * @param date
     * @return int 20100421
     */
    public static int getDateStrToNumeric(String dateTimeStr)throws ParseException {
        Date date = parseDate(dateTimeStr);
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("yyyyMMdd");
        return Integer.parseInt(sf.format(date));
    }

    /**
     * String类型  转换为 int 类型
     * @param time
     * @return int 131223
     */
    public static int getDateStrToTimeInt(String timeStr)throws ParseException {
        Date date = parseTime(timeStr);
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("HHmmss");
        return Integer.parseInt(sf.format(date));
    }

    /**
     * 读取当前时间
     * @return int 132630
     */
    public static int getNumericTime(){
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("HHmmss");
        String date = sf.format(Calendar.getInstance().getTime());
        return Integer.parseInt(date);
    }

    /**
     * 读取当前时间
     * return String 13:26:30
     */
    public static String getStringTime(){
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("HH:mm:ss");
        return sf.format(Calendar.getInstance().getTime());
    }

    /**
     * Date类型  转换为 int 类型
     * @param time
     * @return int 132630
     */
    public static int getNumericTime(Date dateTime){
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("HHmmss");
        return Integer.parseInt(sf.format(dateTime));
    }


    /**
     * + 表达式 d,w,m,y
     * @param date 起始日期
     * @param exp 表达式类型:d/D 天; w/W 周; m/M 月; y/Y 年
     * @param mul 乘数,一般是第mul期,如exp:7d,mul:2,则为加7*2d
     * @param monthEnd 月末是否对齐
     * @return 计算后的Date日期
     * @throws ParseException
     * @throws NumberFormatException
     */
    public static int add(int date,String exp, int mul, boolean monthEnd)throws BusinessException{
        if(null==exp||exp.length() <= 1 ) {
            throw new BusinessException("payFreq is null!");
        }
        exp=exp.trim();
        int len = exp.length();
        int num = Integer.parseInt(exp.substring(0, len - 1)) * mul;
        int ndate = 0;
        switch (exp.charAt(len - 1)) {
        case 'd':
        case 'D':
            return addNumericDays(date, num);
        case 'w':
        case 'W':
            return addNumericDays(date, num * 7);
        case 'm':
        case 'M':
            ndate =  addNumericMonths(date, num);
            break;
        case 'y':
        case 'Y':
            ndate =   addNumericYears(date, num);
            break;
        default:
            return addNumericDays(date, Integer.parseInt(exp));
        }
        if( monthEnd && ndate != 0  ) {//&& isMonthEnd(date)
            return getMonthEnd(ndate);
        } else {
            return ndate;
        }
    }

    /**
     * 增加清算速度或期限
     * @param date 日期 20100421
     * @param term 0|1|1d|1w|1w|1m|1y
     * @return 日期 20100421
     * @throws Exception
     */
    public static int addTerm(int date,String term)throws BusinessException{
        if(term == null || term.trim().length()==0){
            throw new BusinessException("payFreq is null!");
        }else if(term.trim().length()==1){
            int days = Integer.parseInt(term);
            return addNumericDays(date,days);
        }else {
            return add(date,term,1,false);
        }
    }


    /**
     * 返回日期的月末时间
     * @param date
     * @return Date
     */
    public static int getMonthEnd(int date)throws BusinessException {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(parse(date));
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
        return getNumericDate(cal.getTime());
    }


    /**
     * 返回日期的月初时间
     * @param date
     * @return Date
     */
    public static int getMonthStart(int date)throws BusinessException {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(parse(date));
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(GregorianCalendar.DAY_OF_MONTH));
        return getNumericDate(cal.getTime());
    }

    /**
     * 两日期相减
     * @param endDate 到期日期
     * @param startDate 开始日期
     * @return 天数
     * @throws ParseException
     */
    public static int offsetDays(int endDate,int startDate) throws BusinessException{
        return offsetDays(parse(endDate),parse(startDate));
    }

    /**
     * 两个日期相减,参数为Date型
     * @param endDate
     * @param startDate
     * @return int 天数
     */
    public static int offsetDays(Date endDate, Date startDate) {
        return (int) ((endDate.getTime() - startDate.getTime()) / MILLISECONDS_OF_DAY);
    }

    /**
     * 读取月份
     * @param date
     * @return
     * @throws ParseException
     */
    public static int getMonth(int date) throws BusinessException{
        Calendar cl = Calendar.getInstance();
        cl.setTime(parse(date));
        return cl.get(Calendar.MONTH);
    }

    //-----------------------日期运算-------------------------------

    /**
     * 增加年
     * @param date 20100421
     * @param years 年数
     * @return int 20100421
     * @throws ParseException
     * @throws Exception
     */
    public static int addNumericYears(int date,int years) throws BusinessException{
        Calendar cl = Calendar.getInstance();
        cl.setTime(parse(date));
        cl.add(Calendar.YEAR, years);
        return getNumericDate(cl.getTime());
    }

    /**
     * 增加年
     * @param date 20100421143022
     * @param years 年数
     * @return long 20100421143022
     * @throws Exception
     */
    public static long addNumericYears(long dateTime,int years)throws BusinessException{
        Calendar cl = Calendar.getInstance();
        cl.setTime(parse(dateTime));
        cl.add(Calendar.YEAR, years);
        return getNumericDateTime(cl.getTime());
    }


    /**
     * 增加月
     * @param date 20100421
     * @param months 月数
     * @return int 20100421
     * @throws ParseParseException
     * @throws ParseException
     */
    public static int addNumericMonths(int date,int months) throws BusinessException{
        Calendar cl = Calendar.getInstance();
        cl.setTime(parse(date));
        cl.add(Calendar.MONTH, months);
        return getNumericDate(cl.getTime());
    }

    /**
     * 增加月
     * @param date 20100421143022
     * @param months 月数
     * @return long 20100421143022
     * @throws ParseException
     */
    public static long addNumericMonths(long dateTime,int months)throws BusinessException{
        Calendar cl = Calendar.getInstance();
        cl.setTime(parse(dateTime));
        cl.add(Calendar.MONTH, months);
        return getNumericDateTime(cl.getTime());
    }

    /**
     * 增加日
     * @param date 20100421
     * @param days 日数
     * @return int 20100421
     * @throws ParseParseException
     * @throws ParseException
     */
    public static int addNumericDays(int date,int days) throws BusinessException{
        return getNumericDate(new Date(parse(date).getTime()+days*MILLISECONDS_OF_DAY));
    }

    /**
     * 增加日
     * @param date 20100421
     * @param days 日数
     * @return int 20100421
     * @throws ParseParseException
     * @throws ParseException
     */
    public static int addDays(int date,String exp) throws BusinessException{
        int days=getDays(exp);
        return getNumericDate(new Date(parse(date).getTime()+days*MILLISECONDS_OF_DAY));
    }

    /**
     * 增加日
     * @param date 20100421143022
     * @param days 日数
     * @return long 20100421143022
     * @throws ParseException
     */
    public static long addNumericDays(long dateTime,int days)throws BusinessException{
        return getNumericDateTime(new Date(parse(dateTime).getTime()+days*MILLISECONDS_OF_DAY));
    }

    /**
     * 增加小时
     * @param dateTime 20100421143022
     * @param hours 小时数
     * @return long 20100421143022
     * @throws ParseException
     */
    public static long addNumericHours(long dateTime,int hours)throws BusinessException{
        Calendar cl = Calendar.getInstance();
        cl.setTime(parse(dateTime));
        cl.add(Calendar.HOUR, hours);
        return getNumericDateTime(cl.getTime());
    }


    /**
     * 增加分
     * @param dateTime 20100421143022
     * @param minutes 小时数
     * @return long 20100421143022
     * @throws ParseException
     */
    public static long addNumericMinutes(long dateTime,int minutes)throws BusinessException{
        Calendar cl = Calendar.getInstance();
        cl.setTime(parse(dateTime));
        cl.add(Calendar.MINUTE, minutes);
        return getNumericDateTime(cl.getTime());
    }

    /**
     * 增加秒
     * @param dateTime 20100421143022
     * @param seconds 秒
     * @return long 20100421143022
     * @throws ParseException
     */
    public static long addNumericSeconds(long dateTime,int seconds)throws BusinessException{
        Calendar cl = Calendar.getInstance();
        cl.setTime(parse(dateTime));
        cl.add(Calendar.SECOND, seconds);
        return getNumericDateTime(cl.getTime());
    }


    /**
     * 用于有效时间
     * 时间 + 秒数  如果增加的秒数使日期大于24小时,则返回235959
     * @param time 时间
     * @param seconds 秒数
     * @return
     */
    public static int addValidatTimeSeconds(int time,int seconds){
        int sec = time % 100;
        int min = (time/100)%100;
        int hour = time/10000;

        System.out.println("Now Time:Hour["+hour+"]Min["+min+"]Sec["+sec+"]");

        int t_sec = seconds%60;
        int t_min = (seconds%3600)/60;
        int t_hour = seconds/3600;

        System.out.println("Add Time:Hour["+t_hour+"]Min["+t_min+"]Sec["+t_sec+"]");

        if((sec+t_sec)>=60){
            min+=1;
            sec = sec+t_sec - 60;
        }else{
            sec = sec+t_sec ;
        }

        if((min+t_min)>=60){
            hour+=1;
            min = min+t_min-60;
        }else{
            min = min+t_min;
        }

        hour+=t_hour;
        if(hour>=24){
            return 235959;
        }

        return hour*10000+min*100+sec;
    }

    //---------------------日期检查------------------------------------

    /**
     * 检查日期时间
     * @param dateTime 20100421152622
     * @throws ParseException
     */
    public static void checkDateTime(long dateTime)throws BusinessException{
        long orginDateTime = dateTime;
        long newDateTime = getNumericDateTime(parse(dateTime));
        if(orginDateTime!=newDateTime){
            throw new BusinessException("DateTime format not correct["+dateTime+"]");
        }
    }


    /**
     * 检查日期
     * @param date 20100421
     * @throws ParseException
     */
    public static void checkDate(int date)throws BusinessException{
        long orginDate = date;
        long newDate = getNumericDate(parse(date));
        if(orginDate!=newDate){
            throw new BusinessException("Date format not correct["+date+"]");
        }
    }

    /**
     * 检查日期
     * @param date 20100421
     * @return true:格式正确,false:格式错误
     */
    public static boolean checkValueDate(int date){
        boolean flag=true;
        try {
            long orginDate = date;
            long newDate = getNumericDate(parse(date));
            if(orginDate!=newDate){
                flag=false;
            }
        } catch (Exception e) {
            flag=false;
        }
        return flag;
    }


    /**
     * 检查时间
     * @param time 152622
     * @throws ParseException
     */
    public static void checkTime(int time)throws BusinessException{
        long orginTime = time;
        long newTime = getNumericTime(parse(time));
        if(orginTime!=newTime){
            throw new BusinessException("Time format not correct["+time+"]");
        }
    }
    /**
     * 根据int 结构的日期获取Calendar
     * @param date
     * @return
     */
    public static Calendar getCalendarByInt(int date){
        Calendar time=Calendar.getInstance();
        time.clear();
        time.setTime(parse(date));
        return time;
    }
    /**
     * 根据calendar 结构的日期获取int
     * @param date
     * @return
     */
    public static int getIntByCalendar(Calendar calendar){
        return getNumericDate(calendar.getTime());
    }
    // 检验日期格式 年-月- 日
    public static boolean isRightDate(int date) {
        int year = date / 10000;
        int month = (date - year * 10000) / 100;
        int day = date - year * 10000 - month * 100;
        if (year < 1900 || year > 3000) {
            return false;
        }
        if (month > 12 || month < 1) {
            return false;
        }
        if ((year % 4 == 0 || (year % 100 == 0 && year % 400 == 0)) && month == 2) {
            days[1] = 29;
        }
        if (day < 1 || day > days[month - 1]) {
            return false;
        }
        return true;
    }
    /**
     *
     * @param date
     * @param j
     * @return Date
     */
    public static int rollMonth(int date ,int j){
        int tmp = addNumericMonths(date, j - 1);
        return getMonthEnd(tmp);
    }

    /**
     * +月 自然月
     * @param date 起始日期
     * @param months 月数
     * @return 日期加月数后的Date日期
     * @throws ParseException
     */
    public static int addMonths(Date date, int months) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, months);
        return getNumericDate(cal.getTime());
    }
    /**
     * +月
     * @param date 起始日期
     * @param months 月数
     * @return 日期加月数后的String日期
     * @throws ParseException
     */
    public static int addMonths(int date, int months) throws ParseException {
        return addNumericMonths((date), months);
    }
    /**
     * 返回日期
     * @param date yyyy-MM-dd
     * @return Date yyyyMMdd
     * @throws ParseException
     */
    public static Date parseDate(String date) throws ParseException {
        SimpleDateFormat dateF = new SimpleDateFormat("yyyy-MM-dd");
        return dateF.parse(date);
    }

    /**
     * 返回时间
     * @param date 12:12:12
     * @return Date
     * @throws ParseException
     */
    public static Date parseTime(String time) throws ParseException {
        SimpleDateFormat dateF = new SimpleDateFormat("HH:mm:ss");
        return dateF.parse(time);
    }

    /**
     * 返回日期
     * @param date
     * @return Date yyyyMMdd
     * @throws ParseException
     */
    public static Date parseDate(int date) throws ParseException {
        SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMdd");
        return dateF.parse(String.valueOf(date));
    }
    /**
     * 返回日期的月末时间
     * @param date
     * @return Date
     */
    public static Date getMonthEnd(Date date) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    public static boolean isZero(int date) {
        return date==0||date==19000101||date==19700101||parse(date).getTime() <= parse(19010101).getTime()||parse(date).getTime() <= parse(19700101).getTime();

    }
    /**
     * 返回两个日期相差年数
     * @param start
     * @param end
     * @return 相差年数
     * @throws ParseException
     */
    public static int yearsOf(int start, int end)throws BusinessException{
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(parse(start));
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(parse(end));
        return cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR) + 1;
    }

    /**
     * +表达式,d,w,m,y
     * @param date
     * @param exp
     * @return String类型的日期
     * @throws ParseException
     */
    public static int add(int date, String exp) throws BusinessException {
        return  add(date, exp, 1, false);
    }

    /**
     * 比较两个时间大小
     * @param date1
     * @param date2
     * @return true date1>=date2; false date1 < date2
     * @throws ParseException
     */
    public static boolean greatEqual(int date1, int date2)  {
        return date1 >= date2;
    }
    /**
     *
     * @param date1
     * @param date2
     * @return date1  >  date2;
     */
    public static boolean great(int date1, int date2) {
        return  date1  >  date2;
    }
    /**
     * 日期是否一致
     * @param date1
     * @param date2
     * @return
     */
    public static boolean equal(int date1,int date2){
        return  date1  ==  date2;
    }

    /**
     * 比较两个时间大小(无日期)
     * @param date1
     * @param date2
     * @return true date1>=date2; false date1 < date2
     * @throws ParseException
     */
    public static boolean greatTime(int date1, int date2)  {
        return  date1 >= date2;
    }
    /**
     * 比较两个时间大小
     * @param date1
     * @param date2
     * @return
     * true : date1 < date2; false : date1 >= date2
     * @throws ParseException
     */
    public static boolean less(int date1, int date2) {
        return  date1  <  date2 ;
    }

    /**
     * 比较表达式的大小
     * @param exp1
     * @param exp2
     * @return 表达式(折算成天)相减后的值
     */
    public static int compare(String exp1, String exp2){
        return getDays(exp1) - getDays(exp2);
    }

    public static int getDays(String exp) {
        int days = 0;
        try {
            int len = exp.length();
            if("ON".equals(exp)){
                days = 0;
            }else if("TN".equals(exp)){
                days = 1;
            }else if("SP".equals(exp)){
                days = 2;
            }else if("SN".equals(exp)){
                days = 3;
            }else{
                int num = Integer.parseInt(exp.substring(0, len - 1));
                char c = exp.charAt(len - 1);
                if( c == 'd' || c == 'D'){
                    days = num;
                } else if( c == 'w' || c == 'W'){
                    days = num * 7;
                }else if( c == 'm' || c == 'M'){
                    days = num * 30;
                }else if( c == 'y' || c == 'Y'){
                    days = num * 365;
                }
            }
        } catch (Exception e) {
            throw new BusinessException("error.ParamTerm.paramTerm",
                    exp);
        }
        return days;
    }
    /**
     * 返回年
     * @param date
     * @return 年
     * @throws ParseException
     */
    public static int getYear(int date){
        int year=1900;
        try {
            year= getYear(parseDate(date));
        } catch (ParseException e) {
            year=getYear(parse(date));
        }
        return year;
    }
    /**
     * 返回年
     * @param date
     * @return 年
     * @throws ParseException
     */
    public static int getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }
    /**
     * 返回月
     * @param date
     * @return 月
     * @throws ParseException
     */
    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH)+1;
    }
 /**
  * 返回日期的年月
  * @param date
  * @return
  */
    public static int getYearMonth(int date){
        int yearMonth=190001;
        try {
            Date date1=parseDate(date);
             SimpleDateFormat sdf=new SimpleDateFormat("yyyyMM");
             yearMonth=Integer.valueOf( sdf.format(date1));
        } catch (ParseException e) {
            yearMonth=date/100;
        }
        return yearMonth;
    }

    /**
     * 获取日期week信息
     * @param date
     * @return
     */
    public static int getDayWeek(int date){
        Calendar cal=getCalendarByInt(date);
        return    cal.get(Calendar.DAY_OF_WEEK);

    }

    /**
     * 读取当前的日期+时间
     * return long 20100421132630
     */
    public static long getNumericDateTimeMill(){
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("yyyyMMddHHmmssSSS");
        String dateTime = sf.format(Calendar.getInstance().getTime());
        return Long.parseLong(dateTime);
    }
    public static long getNumericDateTime(int date,int time){
        String dateString=date+"000000";
        long datetime=Long.parseLong(dateString);
        return datetime+time;
    }
    
    /**
     * 获取 yyyyMMdd-HH:mm:ss.SSS 类型
     * @param dateTime
     * @return String 20150706-11:08:21.251
     */
    public static String getNumericDateTimeStr(){
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("yyyyMMdd-HH:mm:ss.SSS");
        String dateTime = sf.format(Calendar.getInstance().getTime());
        return dateTime;
    }
    /**
     * yyyyMMdd-HH:mm:ss.SSS 转换为 long 类型
     * @param dateTime
     * @return long 20100421132630333
     */
    public static long getNumericDateTime(String  dateTimeStr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");
        Date date = null;
        try {
            date = sdf.parse(dateTimeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sf = (SimpleDateFormat)DateFormat.getInstance();
        sf.applyPattern("yyyyMMddHHmmssSSS");
        return Long.parseLong(sf.format(date));
    }
}

 

posted @ 2017-12-07 22:25  Onehee  阅读(174)  评论(0编辑  收藏