Java 日期格式問題

來源:互聯網
上載者:User

代碼一:

通過Date類擷取當前日期

public class MyDate {public static void main(String[] args){Date mydateString=new Date();//執行個體化Date對象System.out.println("當前系統日期"+mydateString);//輸出日期}}

代碼二:

通過Calender類擷取當前日期

public class MyDate02 {public static void main(String[] args){Calendar calendar = null;//聲明一個Calendar對象calendar = new GregorianCalendar();//通過子類為其執行個體化System.out.println("年份:"+calendar.get(Calendar.YEAR));//擷取年份System.out.println("月份:"+(calendar.get(Calendar.MONTH)+1));//月份要加1System.out.println("日期:"+calendar.get(Calendar.DAY_OF_MONTH));//日期用DAY_OF_MONTHSystem.out.println("小時"+calendar.get(Calendar.HOUR_OF_DAY));//小時用HOUR_OF_DAYSystem.out.println("分鐘"+calendar.get(Calendar.MINUTE));//分鐘System.out.println("秒"+calendar.get(Calendar.SECOND));//秒System.out.println("毫秒"+calendar.get(Calendar.MILLISECOND));//毫秒}}

代碼三:

通過DateFormat類擷取當前日期

public class MyDate03 {public static void main(String[] args){DateFormat df1 = null;//聲明DateFormat對象DateFormat df2 = null;df1 = DateFormat.getDateInstance();//取得日期df2 = DateFormat.getDateTimeInstance();//取得日期時間System.out.println("DATE:"+df1.format(new Date()));//格式化日期System.out.println("DATETIME:"+df2.format(new Date()));}}

代碼四:

在代碼三的基礎上設定一下日期的格式:

public class MyDate04 {public static void main(String[] args){DateFormat df1 = null;//聲明DateFormat對象DateFormat df2 = null;df1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,new Locale("zh","ch"));                                    //取得日期,設定日期的風格df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","ch"));                                   //取得日期時間,設定日期的顯示格式、時間的顯示格式System.out.println("DATE:"+df1.format(new Date()));//格式化日期System.out.println("DATETIME:"+df2.format(new Date()));}}

未完待續。。。

相關文章

聯繫我們

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