目錄
Date
Calendar
LocalDateTime
在運用中常見的計算時間差值的案例
Date;
Carlendar:
LocalDateTime:
總結:
Date
概述:Date代表了一個特定的時間,精確到毫秒
構造方法:

代碼演示:
Date d1 = new Date();
System.out.println(d1);
System.out.println("---------------------------------------");
long date = 1000*60*60;
Date d2 = new Date(date);
System.out.println(d2);
運行結果:
常用方法:

代碼演示:
public class DateDemo02 {
public static void main(String[] args) {
//創建日期物件
Date d = new Date();
//public long getTime():獲取的是日期物件從1970年1月1日 00:00:00到現在的毫秒值
// System.out.println(d.getTime());
// System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");
//public void setTime(long time):設定時間,給的是毫秒值
// long time = 1000*60*60;
long time = System.currentTimeMillis();
d.setTime(time);
System.out.println(d);
}
}
運行結果:

Calendar
概述:
- Calendar 為特定瞬間與一組日歷欄位之間的轉換提供了一些方法,并為操作日歷欄位提供了一些方法
- Calendar 提供了一個類方法 getInstance 用于獲取這種型別的一般有用的物件,其日歷欄位已使用當前日期和時間初始化:Calendar rightNow = Calendar.getInstance();
方法:

代碼演示:
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
System.out.println("------------------------------------");
c.set(2050,10,10);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH) + 1;
date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
運行結果:

LocalDateTime
使用新時間日期API的必要性
在java8以前,或許:
- 當你在做有關時間日期的操作時,你會想到用Date;
- 當你在做日期、月份、天數相加減時,你會想到用Calendar;
- 當你需要對時間日期進行格式化時,你會想到使用SimpleDateFormat或DateFormat下的其他子類;
但是,你必須知道,以上有關的時間日期操作物件,都是可變的、執行緒不安全的,同時,如果作為一個經常寫過類似代碼的人來說,盡管有相關物件提供某些操作,但并不能很快、很簡單的就能得到最終想要的結果,如:要計算兩個時間點之間相差的年、月、日、周、時、分、秒等,這些計算盡管原有API也能夠實作,但原有API除了執行緒不安全之外,另外一個不足之處就是代碼繁瑣,性能低!
沒錯,java8出的新的時間日期API都是執行緒安全的,并且性能更好,代碼更簡潔!
其中,LocalDate、LocalTime、LocalDateTime是新API里的基礎物件,絕大多數操作都是圍繞這幾個物件來進行的,有必要搞清楚:
LocalDate : 只含年月日的日期物件
LocalTime :只含時分秒的時間物件
LocalDateTime : 同時含有年月日時分秒的日期物件
三者的區別(LocalDate、LocalTime、 LocalDateTime ):
1.表示時間的區別
代碼演示:
LocalDate localDate = LocalDate.of(2018, 1, 13);
LocalTime localTime = LocalTime.of(9, 43, 20);
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 13, 9, 43, 20);
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
運行結果:
2.LocalDateTime對于時間的修改更加準確
兄弟們上代碼:
LocalDateTime current = LocalDateTime.now();
System.out.println("當前時刻 : " + current);
System.out.println("當前年份 : " + current.getYear());
System.out.println("當前月份 : " + current.getMonth());
System.out.println("當前日份 : " + current.getDayOfMonth());
System.out.println("當前時 : " + current.getHour());
System.out.println("當前分 : " + current.getMinute());
System.out.println("當前秒 : " + current.getSecond());

修改具體的值:
LocalDateTime localDateTime = LocalDateTime.now();
//以下方法的引數都是long型,回傳值都是LocalDateTime
LocalDateTime plusYearsResult = localDateTime.plusYears(2L);
LocalDateTime plusMonthsResult = localDateTime.plusMonths(3L);
LocalDateTime plusDaysResult = localDateTime.plusDays(7L);
LocalDateTime plusHoursResult = localDateTime.plusHours(2L);
LocalDateTime plusMinutesResult = localDateTime.plusMinutes(10L);
LocalDateTime plusSecondsResult = localDateTime.plusSeconds(10L);
System.out.println("當前時間是 : " + localDateTime + "\n"
+ "當前時間加2年后為 : " + plusYearsResult + "\n"
+ "當前時間加3個月后為 : " + plusMonthsResult + "\n"
+ "當前時間加7日后為 : " + plusDaysResult + "\n"
+ "當前時間加2小時后為 : " + plusHoursResult + "\n"
+ "當前時間加10分鐘后為 : " + plusMinutesResult + "\n"
+ "當前時間加10秒后為 : " + plusSecondsResult + "\n"
);
//也可以以另一種方式來相加減日期,即plus(long amountToAdd, TemporalUnit unit)
// 引數1 : 相加的數量, 引數2 : 相加的單位
LocalDateTime nextMonth = localDateTime.plus(1, ChronoUnit.MONTHS);
LocalDateTime nextYear = localDateTime.plus(1, ChronoUnit.YEARS);
LocalDateTime nextWeek = localDateTime.plus(1, ChronoUnit.WEEKS);
System.out.println("now : " + localDateTime + "\n"
+ "nextYear : " + nextYear + "\n"
+ "nextMonth : " + nextMonth + "\n"
+ "nextWeek :" + nextWeek + "\n"
);
//日期的減法用法一樣,在此不再舉例
運行結果:
在運用中常見的計算時間差值的案例
Date;
/*
* 算一下你來到這個世界多少天?
*
* 分析:
* A:鍵盤錄入你的出生的年月日
* B:把該字串轉換為一個日期
* C:通過該日期得到一個毫秒值
* D:獲取當前時間的毫秒值
* E:用D-C得到一個毫秒值
* F:把E的毫秒值轉換為年
* /1000/60/60/24
*/
public class MyYearOldDemo {
public static void main(String[] args) throws ParseException {
// 鍵盤錄入你的出生的年月日
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你的出生年月日:");
String line = sc.nextLine();
// 把該字串轉換為一個日期
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 + "天");
}
}
Carlendar:
Calendar c = Calendar.getInstance();
int n = 0;
//public int get(int field):回傳給定日歷欄位的值
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
Calendar c1 = Calendar.getInstance();
c1.set(2021, 10, 25);
year = c1.get(Calendar.YEAR);
month = c1.get(Calendar.MONTH) + 1;
date = c1.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
while (c.before(c1)) {
n++;
c.add(Calendar.DATE, 1);
}
System.out.println(n);
LocalDateTime:
//計算兩個日期的日期間隔-年月日
LocalDate date1 = LocalDate.of(2020, 10, 21);
LocalDate date2 = LocalDate.of(2019, 9, 11);
//內部是用date2-date1,所以得到的結果是負數
Period period = Period.between(date1, date2);
System.out.println("相差年數 : " + period.getYears());
System.out.println("相差月數 : " + period.getMonths());
System.out.println("相差日數 : " + period.getDays());
//另一種計算方式和表現形式
System.out.println("-------------------------------");
long years = period.get(ChronoUnit.YEARS);
long months = period.get(ChronoUnit.MONTHS);
long days = period.get(ChronoUnit.DAYS);
System.out.println("相差的年月日分別為 : " + years + "," + months + "," + days);
總結:
1.出現的差異:
Date是JDK1.0提供的,
Calendar是在JDK1.1提供的,它替代了Date中的很多方法,
LocalDate是在JDK1.8提供的,
2.準確度的差異
Date和Calendar在實體化時會有偏移量(例如實體化"2000-1-2"),Date的year偏移量是1900,month偏移量是1(0~11月,在指定月份時要-1),Calendar的月份有偏移量1,和Date一樣,年份沒有偏移量,
LocalDate(LocalTime LocalDateTime)沒有偏移量,
3.底層原理:
Date和Calendar如果修改了它們的某個日期值,是直接改變其原本的值,類比對基礎資料型別的修改,
LocalDate(LocalTime LocalDateTime)對它們的修改不會改變其原本的值,而是會回傳一個新物件,類比String的不可變性,

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/332132.html
標籤:其他


