DateFormater.java 13 KB
package com.sitech.util;

import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Calendar;

public class DateFormater {

	 public final static String INVALIDDATE = "1950-01-01";
	    public final static String SQLTYPE = "ORACLE";

	    public final static String getFormatTime(String formatType) {
	        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
	        return formatter.format(new java.util.Date());
	    }

	    public final static String getFormatTime(String formatType, java.util.Date formatDate) {
	        if (null == formatDate) {
	            return "";
	        }
	        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
	        return formatter.format(formatDate);
	    }

	    //判断日期字符串(yyyymmdd)是否正确(不是很严禁!没有对日期的30、31、28、29进行判断)
	    public final static boolean isRightDate(String date_s) {
	        boolean isRight = true;
	        if (date_s.length() != 8) {
	            isRight = false;
	        } else {
	            String year_s = date_s.substring(0, 4);
	            String month_s = date_s.substring(4, 6);
	            String date_ss = date_s.substring(6, 8);
	            try {
	                int year_i = Integer.parseInt(year_s);
	                int month_i = Integer.parseInt(month_s);
	                int date_i = Integer.parseInt(date_ss);
	                if (year_i < 1000) {
	                    isRight = false;
	                }
	                if (month_i < 1 || month_i > 12) {
	                    isRight = false;
	                }
	                if (date_i < 1 || date_i > 31) {
	                    isRight = false;
	                }
	            } catch (Exception e) {
	                isRight = false;
	                e.printStackTrace();
	            }
	        }
	        return isRight;
	    }

	    public final static String getNowTime() {
	        SimpleDateFormat formatter1
	                = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        return formatter1.format(new Date());
	    }

	    public final static String getToday() {
	        SimpleDateFormat formatter1
	                = new SimpleDateFormat("yyyy-MM-dd");
	        return formatter1.format(new Date());
	    }

	    /*
	        如果时间为默认值INVALIDDATE,则在前台显示""
	    */
	    public final static String dateToString(Date dDate) throws RuntimeException {
	        if (null == dDate) {
	            return "";
	        }

	        SimpleDateFormat formatter1
	                = new SimpleDateFormat("yyyy-MM-dd");
	        if (formatter1.format(dDate).equals(INVALIDDATE)) {
	            return "";
	        }
	        return formatter1.format(dDate);
	    }

	    /*
	        如果时间为默认值INVALIDDATE,则在前台显示""
	    */
	    public final static String datetimeToString(Date dDate) throws RuntimeException {
	        if (null == dDate) {
	            return "";
	        }
	        if (dateToString(dDate).equals("")) {
	            return "";
	        }
	        SimpleDateFormat formatter1
	                = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        return formatter1.format(dDate);
	    }

	    public final static Date stringToDate(String cDate, String cFormat) throws ParseException {
	        SimpleDateFormat formatter1 = new SimpleDateFormat(cFormat);
	        return formatter1.parse(cDate);
	    }

	    public final static Date stringToDate(String cDate) throws ParseException {
	        try {
	            SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
	            return formatter1.parse(cDate);
	        }
	        catch (ParseException e) {
	            try {
	                SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	                return formatter1.parse(cDate);
	            }
	            catch (ParseException ee) {
	                SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	                return formatter1.parse(cDate);
	            }
	        }
	    }

	    public final static Date stringToDateTime(String cDate) throws ParseException {
	        try {
	            SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	            return formatter1.parse(cDate);
	        }
	        catch (ParseException e) {
	            try {
	                SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	                return formatter1.parse(cDate);
	            }
	            catch (ParseException ee) {
	                SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
	                return formatter1.parse(cDate);
	            }
	        }
	    }

	    private final static String toOracleDate(Date dDate) throws ParseException {
	        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
	        return " to_date('" + formatter1.format(dDate) + "','yyyy-mm-dd') ";
	    }

	    public final static String toOracleDateTime(Date dDate) throws ParseException {
	        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        return " to_date('" + formatter1.format(dDate) + "','yyyy-MM-dd HH24:mi:ss') ";
	    }


	    private final static String toSybaseDate(Date dDate) throws ParseException {
	        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
	        return " '" + formatter1.format(dDate) + "' ";
	    }

	    private final static String toSybaseDateTime(Date dDate) throws ParseException {
	        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
	        return " '" + formatter1.format(dDate) + "' ";
	    }

	    public final static String toDBDate(Date dDate) throws ParseException {
	        if (null == dDate) {
	            dDate = stringToDate(INVALIDDATE);
	        }
	        String cSQLDate = null;
	        if (SQLTYPE.equals("ORACLE"))
	            cSQLDate = toOracleDate(dDate);
	        if (SQLTYPE.equals("SYBASE"))
	            cSQLDate = toSybaseDate(dDate);
	        return cSQLDate;
	    }

	    public final static String toDBDate(String cDate) throws ParseException {
	        if (null == cDate) {
	            cDate = INVALIDDATE;
	        }
	        return toDBDate(stringToDate(cDate));
	    }


	    public final static String toDBDateTime(Date dDate) throws ParseException {
	        if (null == dDate) {
	            dDate = stringToDate(INVALIDDATE);
	        }
	        String cSQLDate = null;
	        if (SQLTYPE.equals("ORACLE"))
	            cSQLDate = toOracleDateTime(dDate);
	        if (SQLTYPE.equals("SYBASE"))
	            cSQLDate = toSybaseDateTime(dDate);
	        return cSQLDate;
	    }

	    public final static String toDBDateTime(String cDate) throws ParseException {
	        if (null == cDate) {
	            cDate = INVALIDDATE;
	        }
	        return toDBDateTime(stringToDateTime(cDate));
	    }

	    /**
	     * (1) parameter description
	     *
	     * @param date
	     * @param type
	     * @param timeInterval
	     * @return (2)function description
	     *         realize the time plus and minus
	     *         (3) loop condition
	     *         y: year
	     *         m: month
	     *         d: date
	     *         h:hour
	     *         f: minute(fenzhong)
	     *         s: second
	     */
	    public static Date computeDate(Date date, char type, int timeInterval) {
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(date);

	        int Time_year = cal.get(Calendar.YEAR);
	        int Time_month = cal.get(Calendar.MONTH);
	        int Time_day = cal.get(Calendar.DAY_OF_MONTH);
	        int Time_hour = cal.get(Calendar.HOUR_OF_DAY);
	        int Time_minute = cal.get(Calendar.MINUTE);
	        int Time_second = cal.get(Calendar.SECOND);

	        switch (type) {
	            case 'y': {
	                Time_year = Time_year + timeInterval;
	                cal.set(Calendar.YEAR, Time_year);
	            }
	            break;

	            case 'm': {
	                Time_month = Time_month + timeInterval;
	                cal.set(Calendar.MONTH, Time_month);
	            }
	            break;

	            case 'd': {
	                Time_day = Time_day + timeInterval;
	                cal.set(Calendar.DAY_OF_MONTH, Time_day);
	            }
	            break;

	            case 'h': {
	                Time_hour = Time_hour + timeInterval;
	                cal.set(Calendar.HOUR_OF_DAY, Time_hour);
	            }
	            break;

	            case 'f': {
	                Time_minute = Time_minute + timeInterval;
	                cal.set(Calendar.MINUTE, Time_minute);
	            }
	            break;

	            case 's': {
	                Time_second = Time_second + timeInterval;
	                cal.set(Calendar.SECOND, Time_second);
	            }
	            break;
	            default:
	                break;
	        }
	        date = cal.getTime();
	        return date;
	    }

	    public static Date computeDate(String cDate, char type, int timeInterval) {
	        java.util.Date dDate = null;
	        try {
	            dDate = stringToDateTime(cDate);
	        }
	        catch (ParseException e) {
	            dDate = new Date();
	        }
	        return computeDate(dDate, type, timeInterval);
	    }

	    public final static String getHISTableName(Date dDate) throws ParseException {
	        if (null == dDate) {
	            dDate = new Date();
	        }
	        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd");
	        return formatter1.format(dDate);
	    }

	    /**
	     * 根据传入的时间和需计算的差值,获取计算后的时间
	     * 算法:通过转化成标准毫秒进行转换。
	     * add by qiuzq 20080611.
	     *
	     * @param dDate    传入的时间
	     * @param calValue 需计算的差值
	     * @return 返回字符串
	     * @throws ParseException
	     */
	    public final static String getCalDateStr(Date dDate, int calValue) throws ParseException {
	        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        long myTime = dDate.getTime() + calValue;
	        dDate.setTime(myTime);
	        return bartDateFormat.format(dDate);
	    }

	    /**
	     * 根据传入的时间、类型和需计算的差值,获取计算后的时间
	     * 算法:通过转化成标准毫秒进行转换。
	     *
	     * @param sDate
	     * @param type     类型,最大粒度到天;目前不支持年月
	     * @param calValue
	     * @return
	     * @throws ParseException
	     */
	    public final static String getCalDateStr(String sDate, String type, int calValue) throws ParseException {
	        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        int iValue = 0;
	        if ("day".equals(type.toLowerCase())) {
	            iValue = 24 * 60 * 60 * 1000 * calValue;
	        } else if ("hour".equals(type.toLowerCase())) {
	            iValue = 60 * 60 * 1000 * calValue;
	        } else if ("minute".equals(type.toLowerCase())) {
	            iValue = 60 * 1000 * calValue;
	        } else if ("second".equals(type.toLowerCase())) {
	            iValue = 1000 * calValue;
	        }
	        return getCalDateStr(bartDateFormat.parse(sDate), iValue);
	    }

	    /**
	     * 根据传入的时间、类型和需计算的差值,获取计算后的时间
	     * 算法:通过转化成标准毫秒进行转换。
	     *
	     * @param dDate
	     * @param type     类型,最大粒度到天;目前不支持年月
	     * @param calValue
	     * @return
	     * @throws ParseException
	     */
	    public final static String getCalDateStr(Date dDate, String type, int calValue) throws ParseException {
	        int iValue = 0;
	        if ("day".equals(type.toLowerCase())) {
	            iValue = 24 * 60 * 60 * 1000 * calValue;
	        } else if ("hour".equals(type.toLowerCase())) {
	            iValue = 60 * 60 * 1000 * calValue;
	        } else if ("minute".equals(type.toLowerCase())) {
	            iValue = 60 * 1000 * calValue;
	        } else if ("second".equals(type.toLowerCase())) {
	            iValue = 1000 * calValue;
	        }
	        return getCalDateStr(dDate, iValue);
	    }

	    /**
	     * 根据传入的时间、类型和需计算的差值,获取计算后的时间
	     * 算法:通过转化成标准毫秒进行转换。
	     *
	     * @param dDate
	     * @param type     类型,最大粒度到天;目前不支持年月
	     * @param calValue
	     * @return
	     * @throws ParseException
	     */
	    public final static Date getCalDateTime(Date dDate, String type, int calValue) throws ParseException {
	        int iValue = 0;
	        if ("day".equals(type.toLowerCase())) {
	            iValue = 24 * 60 * 60 * 1000 * calValue;
	        } else if ("hour".equals(type.toLowerCase())) {
	            iValue = 60 * 60 * 1000 * calValue;
	        } else if ("minute".equals(type.toLowerCase())) {
	            iValue = 60 * 1000 * calValue;
	        } else if ("second".equals(type.toLowerCase())) {
	            iValue = 1000 * calValue;
	        }

	        long myTime = dDate.getTime() + iValue;
	        dDate.setTime(myTime);
	        return dDate;
	    }
}