java學(xué)習(xí)——日期操作類

/*得到當(dāng)前系統(tǒng)日期 Date類*/

Date date =new Date();//實例化Date類對象? date當(dāng)前日前

System.out.println("當(dāng)前日前為:"+ date);//Wed May 17 10:31:48 GMT+08:00 2017

/*Calendar類*/

Calendar calendar =null;//聲明一個Calendar對象

calendar =new GregorianCalendar();// 通過子類為其實例化

System.out.println("年:"+ calendar.get(Calendar.YEAR));//年

System.out.println("月:"+ calendar.get(Calendar.MONTH));//月

System.out.println("日:"+ calendar.get(Calendar.DAY_OF_MONTH));//日

System.out.println("時:"+ calendar.get(Calendar.MINUTE));//時

System.out.println("分:"+ calendar.get(Calendar.SECOND));//分

System.out.println("秒:"+ calendar.get(Calendar.MILLISECOND));//秒


/*DateFormat類*/

DateFormat df1 =null;// 聲明DateFormat對象

DateFormat df2 =null;// 聲明DateFormat對象

df1 = DateFormat.getDateInstance();// 取得日期

df2 = DateFormat.getDateTimeInstance();// 取得日期時間

System.out.println("DATE1:"+df1.format(newDate()));// 格式化日期 //2017年5月17日

System.out.println("DATETIME1:"+ df2.format(newDate()));//2017年5月17日 中國標準時間 上午10:31:48

/*指定顯示風(fēng)格*/

DateFormat df3 =null;// 聲明DateFormat對象

DateFormat df4 =null;// 聲明DateFormat對象

df3 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,newLocale("zh","CN"));//取得日期,并設(shè)置日期顯示風(fēng)格

// 取得日期時間,設(shè)置日期的顯示格式、時間的顯示格式

df4 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,newLocale("zh","CN"));

System.out.println("DATE2:"+df3.format(newDate()));// 格式化日期

System.out.println("DATETIME2:"+ df4.format(newDate()));


/*SimpleDateFormat類

* 1.將String 變?yōu)镈ate型數(shù)據(jù)

* 2.將Date 變?yōu)?String型數(shù)據(jù)

* */

String strDate ="2017-5-17 10:23:30.345";

String pat1 ="yyyy-MM-dd HH:mm:ss.SSS";

String pat2 ="yyyy年MM月dd日HH時mm分ss秒SSS毫秒";

SimpleDateFormat sdf1 =newSimpleDateFormat(pat1);

SimpleDateFormat sdf2 =newSimpleDateFormat(pat2);

Date d =null;

try{

d = sdf1.parse(strDate);//將String 變?yōu)镈ate型數(shù)據(jù)

}catch(ParseException e) {

e.printStackTrace();

}

//將Date 變?yōu)?String型數(shù)據(jù)

System.out.println(sdf2.format(d));//2017年05月17日10時23分30秒345毫秒

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第05天API 今日內(nèi)容介紹 ·Object類& System類 ·日期相關(guān)類 ·包裝類&正則表達式 ·Date對...
    chcvn閱讀 521評論 0 1
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,706評論 18 399
  • import java.text.SimpleDateFormat;import java.util.Calend...
    霙愔閱讀 803評論 0 2
  • 一、 1、請用Java寫一個冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨云閱讀 1,499評論 0 6
  • 勇士殺死了惡龍,躺在堆積如山的金幣上,漸漸長出鱗片! 天空飄蕩著火山灰一般的懸浮物,黑壓壓的仿佛要貼在地面一般。 ...
    極限單兵閱讀 468評論 0 2

友情鏈接更多精彩內(nèi)容