String、java.util.Date、java.sql.Date、java.sql.Timestamp相互変換

1594 ワード


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

public class DateTest {
    public static void main(String[] args){
        //------------java.lang.String   java.util.Date---------------
        String dStr = "2001-12-12 00:00:00";
        Date d = null;
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
          d = sdf.parse(dStr);
        } catch (ParseException pe) {
            System.out.println(pe.getMessage());
        }
        System.out.println(d);
        
       //------------  java.util.Date   java.lang.String---------------
        Date date = new Date();
        System.out.println(sdf.format(d));
        System.out.println(sdf.format(date));
        
       //------------  java.util.Date   java.sql.Date---------------
        
       java.sql.Date sqldate = new java.sql.Date(d.getTime());
       System.out.println(sqldate);

       //------------  java.util.Date   java.sql.Timestamp---------------
        
      java.sql.Timestamp sqltsp = new java.sql.Timestamp (d.getTime());
       System.out.println(sqltsp);
       
        
    }
}

--------------------------------------------------------------------------
ACグループ:81552084
--------------------------------------------------------------------------