java代碼--Date類擷取目前時間-格式化輸出

來源:互聯網
上載者:User
package cn.Date;
import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;
/*
*DateFormat 是日期/時間格式化子類的抽象類別,因為他是一個抽象類別,所以要有具體構造方法  *public class SimpleDateFormatextends DateFormatSimpleDateFormat * 是一個以與語言環境有關的方式來格式化和解析日期的具體類
*public final String format(Date date)將一個 Date 格式化為日期/時間字串。 *SimpleDateFormat() 用預設的模式顯示時間 *SimpleDateFormat(String pattern) 用給定的模式顯示時間
  * *y  年  Year   *M  年中的月份  Month   *d  月份中的天數  Number   *H  一天中的小時數(0-23)  Number   *m 分 *s 秒 * * * */ public class DateformatDemo {
                 public static void main(String[] args) {                                                   //建立一個日期對象                 Date d=new Date();                 System.out.println(d);                 /*//建立一個格式化對象                 SimpleDateFormat sdf=new SimpleDateFormat();                 System.out.println(sdf);*/                 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                 //格式化為日期/時間字串                 String cc=sdf.format(d);                 System.out.println(cc);         }                            }

聯繫我們

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