java.util.Date、java.sql.Date、java.sql.Time、java.sql.Timestamp區別和總結

來源:互聯網
上載者:User

標籤:

java.sql.Date、java.sql.Timestamp區別總結

 

java.lang.Object
....|__java.sql.Date/java.sql.Time


【父類】java.sql.Date日期格式為:年月日[只儲存日期資料不儲存時間資料]
【子類】java.sql.Timestamp日期格式為:年月日時分秒納秒(毫微秒)

 

針對不同的資料庫選用不同的日期類型
·Oracle的Date類型,只需要年月日,選擇使用java.sql.Timestamp類型


------------------------------------------

四種對象內部均使用系統時間作為標準資料
·系統時間:自 1970 年 1 月 1 日 00:00:00 GMT 以來的毫秒數,即格林尼治標準時間(GMT)
·本地時間:根據時區不同列印出來的時間[當時區為GMT+0時,系統時間與本地時間相同]

我們使用的是以本地時間為參考標準的
------------------------------------------

String日期格式轉換成Date日期格式

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class a {
 
   //主函數
   public static void main(String[] args)
   {
    //  String str="2011-5-31 14:40:50";
  try {
   java.util.Date utilDate=f_utilDate.parse(str);
   System.out.println(f_utilDate.format(utilDate));
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

    //    java.sql.Date.valueOf("2010-08-20");
    System.out.println(f_sqlDate.format(sqlDate));
   
    //    java.sql.Time.valueOf("13:44:53");
    System.out.println(f_sqlTime.format(sqltime));
   
    //    java.sql.Timestamp.valueOf("2010-08-20 14:06:27.186");
    System.out.println(f_timestamp.format(timestamp));
   }
}

 

 *******************************************************************************

 

     //java.sql.Date 格式
       try{
        SimpleDateFormat DateFormate =   new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
               java.util.Date date1 = DateFormate.parse("2011-5-31 14:40:50");
               java.sql.Date(date1.getTime());
               System.out.println(DateFormate.format(sqlDate));
       }catch (Exception ex) {
            System.out.println(ex.getMessage());
       }

       //java.util.Date 格式
       java.sql.Date.valueOf("2005-12-12");
       java.util.Date(sqlDate1.getTime());
       System.out.println("java.util.Date 格式:"+f.format(utilDate1));

    //java.sql.Timestamp
    new java.util.Date().getTime());//此處IDE報錯

    //java.sql.Time
    new java.util.Date().getTime());

    

    Timestamp timestamp  = new Timestamp(System.currentTimeMillis());

   我們可以使用DateFormat處理字串來定義時間日期的格式
   註:String都是先轉換為java.util.Date,然後再轉換成所需的格式

   

樣本:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class a  {  
    public static void main(String[] args) throws ParseException{  
        try{  
             String dateString = "2010-08-20 12:00:00.125";   
              DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS",Locale.ENGLISH);//設定格式  
             dateFormat.setLenient(false);  
             java.util.Date utilDate = dateFormat.parse(dateString);//util類型  
             java.sql.Timestamp(utilDate.getTime());//Timestamp類型,timeDate.getTime()返回一個long型  
             System.out.println(dateTime);  
        }catch(Exception ex){  
            ex.printStackTrace();  
        }       
    }  
}  

java.util.Date、java.sql.Date、java.sql.Time、java.sql.Timestamp區別和總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.