java中Date/DateFormat-java學習筆記
(1)Date是日期類,可以精確到毫秒。
A:構造方法
Date()
Date(long time)
Date d = new Date();
System.out.println("d:" + d);
// 建立物件
// long time = System.currentTimeMillis();
long time = 1000 * 60 * 60; // 1小時
Date d2 = new Date(time);
System.out.println("d2:" + d2);
B:成員方法
getTime()
setTime(long time)
C:日期和毫秒值的相互轉換
public long getTime():獲取時間,以毫秒為單位
* public void setTime(long time):設定時間
* 從Date得到一個毫秒值
* getTime()
long haomiao=d.getTime();//把Date轉換成毫秒值
* 把一個毫秒值轉換為Date
* 構造方法//Date(long time)
Date d1=new Date(System.currentTimeMillis());
System.out.println(d1);//把毫秒值轉換為Date
* setTime(long time)
d.setTime(10000);
案例:你來到這個世界多少天了?
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(line);
// 通過該日期得到一個毫秒值
long myTime = d.getTime();
// 獲取當前時間的毫秒值
- long nowTime = System.currentTimeMillis();
// 用D-C得到一個毫秒值
long time = nowTime - myTime;
// 把E的毫秒值轉換為年
long day = time / 1000 / 60 / 60 / 24;
System.out.println("你來到這個世界:" + day + "天");
}
(2)DateFormat針對日期進行格式化和針對字串進行解析的類,但是是抽象類,所以使用其子類SimpleDateFormat
A:SimpleDateFormat(String pattern) 給定模式
yyyy-MM-dd HH:mm:ss
SimpleDateFormat的構造方法:
* SimpleDateFormat():預設模式
* SimpleDateFormat(String pattern):給定的模式
* 這個模式字串該如何寫呢?
* 通過檢視API,我們就找到了對應的模式
* 年 y
* 月 M
* 日 d
* 時 H
* 分 m
* 秒 s
* 2014年12月12日 12:12:12
B:日期和字串的轉換
a:Date – String
- public final String format(Date date)
Date d=new Date();
SimpleDateFormat s=new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
System.out.println(s.format(d));//是Date-〉String
b:String – Date
- public Date parse(String source)
String s3="2016年04月20日";
SimpleDateFormat s1=new SimpleDateFormat("yyyy年MM月dd日");
System.out.println(s1.parse(s3));//String->Date
C:案例:
製作了一個針對日期操作的工具類。
8:Calendar(掌握)
(1)日曆類,封裝了所有的日曆欄位值,通過統一的方法根據傳入不同的日曆欄位可以獲取值。
(2)如何得到一個日曆物件呢?
Calendar rightNow = Calendar.getInstance();
本質返回的是子類物件
Calendar c=Calendar.getInstance();//多型用法
int wenk=c.get(Calendar.DAY_OF_WEEK_IN_MONTH);
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int data=c.get(Calendar.DATE);
int hour=c.get(Calendar.HOUR);
int min=c.get(Calendar.MINUTE);
System.out.println(year+"年"+(month+1)+"月"+data+"日"+hour+"時"+min+"分");
System.out.println(wenk);
- 成員方法
* public void add(int field,int amount):根據給定的日曆欄位和對應的時間,來對當前的日曆進行操作。
* public final void set(int year,int month,int date):設定當前日曆的年月日
A:根據日曆欄位得到對應的值
B:根據日曆欄位和一個正負數確定是新增還是減去對應日曆欄位的值
C:設定日曆物件的年月日
// 5年後的10天前
c.add(Calendar.YEAR, 5);
c.add(Calendar.DATE, -10);
// 獲取年
year = c.get(Calendar.YEAR);
// 獲取月
month = c.get(Calendar.MONTH);
// 獲取日
date = c.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
System.out.println("--------------");
c.set(2011, 11, 11);
// 獲取年
year = c.get(Calendar.YEAR);
// 獲取月
month = c.get(Calendar.MONTH);
// 獲取日
date = c.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
}
}
(4)案例:
計算任意一年的2月份有多少天?
Scanner sc = new Scanner(System.in);
System.out.println("請輸入年份:");
int year = sc.nextInt();
// 設定日曆物件的年月日
Calendar c = Calendar.getInstance();
c.set(year, 2, 1); // 其實是這一年的3月1日
// 把時間往前推一天,就是2月的最後一天
c.add(Calendar.DATE, -1);
// 獲取這一天輸出即可
System.out.println(c.get(Calendar.DATE));