java中內建時間類用法執行個體 Date,Timestamp,DateFormat

來源:互聯網
上載者:User

標籤:java   date   timestamp   calendar   simpledateformat   

我們將以Java內建的時間日期類和其中的處理函數進行分析。

一、與時間日期有關的類。

  java.util.Date。實作類別,其對象具有時間、日期組件。

  java.util.Calendar。抽象類別,其對象具有時間、日期組件。

  java.sql.Date。實作類別,其對象具有日期組件。

  java.sql.Time。實作類別,其對象具有時間組件。

  java.sql.Timestamp。實作類別,其對象具有時間日期組件。

  java.text.DateFormat。抽象類別,其對象格式化時間日期。


 

我們一般會使用的類java.util.Date、java.util.Calendar、java.sql.Timestamp、java.text.DateFormat進行時間日期操作,因為他們有完全的時間日期組件和全面的格式化功能。值得注意的是java.sql.Date沒有時間組件,而java.sql.Time沒有日期組件!

下面是用法舉例

public static void main(String[] args) {/***展示各個日期時間組件的輸出類型*/java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());System.out.println(sqlDate.toString()); // 輸出結果:2015-06-25java.sql.Time sqlTime = new java.sql.Time(System.currentTimeMillis());System.out.println(sqlTime.toString()); // 輸出結果:09:13:43java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(System.currentTimeMillis());System.out.println(sqlTimestamp.toString()); // 輸出結果:2015-06-25 09:13:43.561java.util.Date utilDate = new java.util.Date(System.currentTimeMillis());System.out.println(utilDate.toString()); // 輸出結果:Thu Jun 25 09:13:43 CST 2015java.util.Calendar cl = java.util.Calendar.getInstance();System.out.println(cl.getTime().toString()); // 輸出結果:Thu Jun 25 09:13:43 CST 2015}


public static void main(String[] args) {/** * java.sql.Timestamp的用法, */@SuppressWarnings("deprecation")java.sql.Timestamp sqlTimestamp1 = new java.sql.Timestamp(2015,6,25,9,13,43,561);java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(System.currentTimeMillis());System.out.println(sqlTimestamp); Boolean isAfter = sqlTimestamp.after( sqlTimestamp1 );System.out.println( isAfter );    //falseBoolean isBefore = sqlTimestamp.before( sqlTimestamp1 );System.out.println( isBefore );   //true//用於比較兩個時間點的長度,單位毫秒Long gettimes = sqlTimestamp.getTime(); System.out.println( gettimes );   //輸出 :1435196988250//將字串類型轉為 Timestamp 類型java.sql.Timestamp sqlTimestamp2 = java.sql.Timestamp.valueOf( "2015-06-25 09:48:16.524" );System.out.println( sqlTimestamp2 );   //輸出:2015-06-25 09:48:16.524}


public static void main(String[] args) {/** * java.text.SimpleDateFormat的用法 */SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Timestamp now = new Timestamp(System.currentTimeMillis());System.out.println(now); // 2015-06-25 14:27:41.477String time = df.format(now);System.out.println(time); // 2015-06-25 14:27:41System.out.println(Timestamp.valueOf(time)); // 2015-06-25 14:27:41.0Date now1 = new Date();System.out.println(now); // 2015-06-25 14:27:41.477String time1 = df.format(now1);System.out.println(time1); // 2015-06-25 14:27:41System.out.println(Timestamp.valueOf(time1)); // 2015-06-25 14:27:41.0}


public static void main(String[] args) {/** * java.util.Calendar的用法 */Calendar calendar1 = Calendar.getInstance();System.out.println(calendar1); // java.util.GregorianCalendar[time=1435214975097,areFieldsSet=true,// areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",// offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],// firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2015,MONTH=5,// WEEK_OF_YEAR=26,WEEK_OF_MONTH=4,DAY_OF_MONTH=25,DAY_OF_YEAR=176,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,// AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=49,SECOND=35,MILLISECOND=97,ZONE_OFFSET=28800000,DST_OFFSET=0]// 擷取時間Date date1 = calendar1.getTime();System.out.println(date1); // Thu Jun 25 14:49:35 CST 2015System.out.println(calendar1.getWeeksInWeekYear()); // 52System.out.println(calendar1.get(Calendar.DAY_OF_MONTH)); // 25System.out.println(calendar1.get(Calendar.HOUR_OF_DAY)); // 14}


java中內建時間類用法執行個體 Date,Timestamp,DateFormat

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.