一个处理日期的工具类

SimpleDateFormat( "yyyy-MM-dd", Locale.CHINESE );
 
    /**
     * Creates a new DateUtil object
     */

    public DateUtil()
    {
    }
 
    /**
     * Returns the date of the day in form of a String
     *
     * @return The Date of the day in a  "yyyy-MM-dd" format
     */

    public static String getCurrentDateString()
    {
        return _formatter.format( new java.util.Date() );
    }
 
    /**
     * Converts a String date in a "yyyy-MM-dd" format in a java.sql.Date type date
     *
     * @param strDate The String Date to convert, in a date in the "yyyy-MM-dd" format
     * @return The date in form of a java.sql.Date tyep date
     */

    public static java.sql.Date getDateSql( String strDate )
    {
        ParsePosition pos = new ParsePosition( 0 );
        java.util.Date date = _formatter.parse( strDate, pos );
        if( date != null )
        {
            return new java.sql.Date( date.getTime() );
        }
        return null;
    }
 
    /**
     * Converts a java.sql.Date type date in a String date with a "yyyy-MM-dd" format
     *
     * @param date java.sql.Date date to convert
     * @return strDate The date converted to String in a "yyyy-MM-dd" format or an empty String if
     *         the date is null
     */

    public static String getDateString( java.sql.Date date )
    {
        if( date != null )
        {
            StringBuffer strDate = new StringBuffer();
            _formatter.format( date, strDate, new FieldPosition( 0 ) );
            return strDate.toString();
        }
        return "";
    }
 
    /**
     * Converts une java.sql.Timestamp date in a String date in a "yyyy-MM-dd" format
     *
     * @param date java.sql.Timestamp date to convert
     * @return strDate The String date in a "yyyy-MM-dd" format or the emmpty String if the date is
     *         null
     */

    public static String getDateString( java.sql.Timestamp date )
    {
        if( date != null )
        {
            StringBuffer strDate = new StringBuffer();
            _formatter.format( date, strDate, new FieldPosition( 0 ) );
            return strDate.toString();
        }
        return "";
    }
 
    /**
     * Converts a java.util.Date date in a String date in a "yyyy-MM-dd" format
     *
     * @param date java.util.Date date to convert
     * @return strDate A String date in a "yyyy-MM-dd" format or an empty String if the date is null
     */

    public static String getDateString( java.util.Date date )
    {
        if( date != null )
        {
            StringBuffer strDate = new StringBuffer();
            _formatter.format( date, strDate, new FieldPosition( 0 ) );
            return strDate.toString();
        }
        return "";
    }
}
阅读更多
个人分类: JAVA
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭