java Date日期類和SimpleDateFormat日期類格式

來源:互聯網
上載者:User

標籤:util   參數   日期格式轉換   out   time()   方式   auto   import   格式化   

~Date表示特定的時間,精確到毫秒
~構造方法:
public Date()//構造Date對象並初始化為當前系統的時間
public Date(long date) //1970-1-1 0:0:0到指定的時間的毫秒數

~常用方法:
public long getTime() //1970-1-1 0:0:0到當前的毫秒數
public long setTime() //設定日期時間
public boolean befor(Date when) //測試此日期是否在指定日期之前
public boolean after(Date when) //測試此日期是否在指定日期之後
public int compareTo (Date anotherDate) //假設當前Date在Date參數之前,則返回<0;當前Date在Date參數之後,則返回>0
public String toString() //將日期格式轉換為字串格式輸出

~DateFormat是日期/時間格式化抽象類別,它以語言無關的方式格式化並分析日期或時間
~日期/時間格式化子類(如SimpleDateFormat)允許進行格式化(也就是日期->文本)、分析(文本->日期)
~構造方法:
public SimlpeDateFormat()
public SimpleDateFormat(String pattern)
~常用方法:
public final String format(Date date) //Date轉為字串
public Date parse(String source) //字串轉為Date

import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;public class DateDemo {    public static void main(String[] args) {        // TODO Auto-generated method stub        Date date = new Date();        System.out.println(date);        System.out.println(date.getTime());//返回從1970.1.1到現在的毫秒數        date.setTime(1519807159999L);//修改時間        System.out.println(date);                DateFormat df1 = null;//DateFormat為抽象類別不可以執行個體化        DateFormat df2 = null;//同上        df1 = DateFormat.getDateInstance();//實現子類對象,get日期        df2 = DateFormat.getDateTimeInstance();//get日期+時間        System.out.println("Date:"+df1.format(date));//將字串轉換為日期格式,格式固定        System.out.println("DateTime:"+df2.format(date));//將字串轉換為日期+時間格式,格式固定                DateFormat df3 = null;        df3 = DateFormat.getDateInstance(DateFormat.FULL,new Locale("zh","CN"));//格式化為中國日期方式。有SHORT,MEDIUM,LONG,FULL四種格式        System.out.println("Date:"+df3.format(date));//將字串轉換為日期+時間格式,格式固定        DateFormat df4 = null;        df4 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,new Locale("zh","CN"));        System.out.println("DateTime:"+df4.format(date));//將字串轉換為日期+時間格式,格式固定                //SimpleDateFormat子類可以自訂輸出格式,更靈活        String strDate = "2018-2-29 18:30:00.123";        Date d= null;        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH-mm-ss.SSS");        try{//如果有格式書寫異常,則把異常拋出            d = sdf1.parse(strDate);        }catch(Exception e){        }        System.out.println(d);        String str = sdf2.format(d);//把日期按指定格式輸出        System.out.println(str);    }}

 

java Date日期類和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.