java.util.Date java.sql.Date String 转换

java.util.Date java.sql.Date String 转换

package cn.csdn.web.Util;

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

public class DateUtil
{
    public static void main(String[] args) throws Exception
    {
        java.sql.Date sd = new java.sql.Date(System.currentTimeMillis());
        System.out.println("java.sql.date = " + sd);
        
        java.util.Date ud = new java.util.Date();
        System.out.println("java.util.date = " + ud);
        
        long longtime = System.currentTimeMillis();
        System.out.println("system date = " + longtime);
        
        String stringTime = "2013-05-09 22:33:33";
        String stringDate = "2013-01-02";
        System.out.println("stringTime = " + stringTime);
        System.out.println("stringDate = " + stringDate);
        
        System.out.println("=====================================>");
        
        //util to sql
        System.out.println("utilDateToSqlDate = " + DateUtil.utilDateToSqlDate(ud));
        
        //sql to util
        System.out.println("sqlDateToUtilDate = " + DateUtil.sqlDateToUtilDate(sd));
        
        //string to util 
        System.out.println("stringDateToUtilTime = " + DateUtil.stringDateToUtilTime(stringTime));
        System.out.println("stringDateToUtilDate = " + DateUtil.stringDateToUtilDate(stringDate));
        
        //string to sql 
        System.out.println("stringDateToSQLTime = " + DateUtil.stringDateToSQLTime(stringTime));
        System.out.println("stringDateToSQLDate = " + DateUtil.stringDateToSQLDate(stringDate));
        
        //util  to string 
        System.out.println("utilDateToStringDate = " + DateUtil.utilDateToStringDate(ud));
        
        //sql to string 
        System.out.println("sqlDateToStringDate = " + DateUtil.sqlDateToStringDate(sd));
        
        //long time to sql 
        System.out.println("longToSqlDate = " + DateUtil.longToSqlDate(longtime));
        
        //long time to util 
        System.out.println("longToUtilDate = " + DateUtil.longToUtilDate(longtime));
        
        System.out.println("===============end=====================>");
    }

    
    public static java.util.Date stringDateToSQLTime(String stringTime) throws ParseException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(stringTime);
        return date;
    }


    /**
     * @param longtime
     * @return
     */
    public static  java.util.Date longToUtilDate(long longtime)
    {
        return  new java.util.Date(longtime);
    }
    
    /**
     * @param longtime
     * @return
     */
    public static  java.sql.Date longToSqlDate(long longtime)
    {
        return  new java.sql.Date(longtime);
    }
    
    
   /**
    * 
    * @param date
    * @return
    * @throws ParseException
    */
    public static String utilDateToStringDate(java.util.Date date)
            throws ParseException
    {

        return date.toString();
    }

    /**
     * 
     * @param date
     * @return
     * @throws ParseException
     */
    public static String sqlDateToStringDate(java.sql.Date date)
            throws ParseException
    {
        return date.toString();
    }

    /**
     * 
     * @param dateString
     * @return
     * @throws ParseException
     */
    public static java.sql.Date stringDateToSQLDate(String dateString)
            throws ParseException
    {
        java.sql.Date date = java.sql.Date.valueOf(dateString);
        return date;
    }

    /**
     * 
     * @param dateString
     * @return
     * @throws ParseException
     */
    public static java.util.Date stringDateToUtilDate(String dateString)
            throws ParseException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dateString);
        return date;
    }
    
    /**
     * 
     * @param dateString
     * @return
     * @throws ParseException
     */
    public static java.util.Date stringDateToUtilTime(String timeString)
            throws ParseException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(timeString);
        return date;
    }

    /**
     * 
     * @param ud
     * @return
     */
    public static java.sql.Date utilDateToSqlDate(java.util.Date ud)
    {
        java.sql.Date d = new java.sql.Date(ud.getTime());

        return d;
    }

   /**
    * 
    * @param sd
    * @return
    */
    public static java.util.Date sqlDateToUtilDate(java.sql.Date sd)
    {
        java.util.Date d = new Date(sd.getTime());

        return d;
    }
}

 

你可能感兴趣的:(String,转换,java.util.Date,java.sql.Date)