JAVA學習筆記-DateFormat和SimpleDateFormat

來源:互聯網
上載者:User

標籤:cat   sys   static   bsp   nbsp   2016年   new   rac   com   

SimpleDateFormat是DateFormat的子類,它給我提供了一些方法完成字串和時間對象的轉化!

DateFormat為抽象類別,不能自己建立自己的執行個體。它只有一個子類SimpleDateFormat

DateFormat屬於java.text類,在使用前應該加import java.text.DateFormat;

SimpleDateFormat同理,在使用前應該加import java.text.SimpleDateFormat;

 

format

 

package MyDate;

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

public class Mycode02 {

  public static void main(String[] args) {
    DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
    Date a = new Date();
    String str = df.format(a);    //將時間對象按照格式化字串,轉換成字串
    System.out.println(str);

  }

}

輸出結果:

2016年11月13日    02:45:06

 

 

parse(解析)

 

package MyDate;

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

public class Mycode02 {

public static void main(String[] args) {
  String str = "1977-7-7";    //  綠色部分必須格式一致,否則報錯!
  DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  //建立一個格式化器
  Date a;
  try {
    a = df.parse(str);
    System.out.println(a);
    } catch (ParseException e) {
    e.printStackTrace();
    }
}

}

JAVA學習筆記-DateFormat和SimpleDateFormat

聯繫我們

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