本地日期()、 本地時(shí)間()、 本地日期時(shí)間()
1.創(chuàng)建時(shí)間,使用時(shí)間的類的靜態(tài)方法now/of
public void test1() {
// now() 獲取當(dāng)前時(shí)間
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDate:" + localDate);
System.out.println("localTime:" + localTime);
System.out.println("localDateTime:" + localDateTime);
// of 根據(jù)指定年月日時(shí)分秒創(chuàng)建指定的時(shí)間
LocalDate customDate = LocalDate.of(2022, 5, 3);
LocalTime customTime = LocalTime.of(21, 43, 0);
LocalDateTime customDateTime = LocalDateTime.of(2022, 5, 3, 21, 43, 0);
System.out.println("customDate:" + customDate);
System.out.println("customTime:" + customTime);
System.out.println("customDateTime:" + customDateTime);
}
結(jié)果:
: 2022-05-03
: 22:59:30.
: 2022-05-03T22:59:30.
: 2022-05-03
: 21:43
: 2022-05-03T21:43
2.增加減少時(shí)間 plus/minus
public void test3() {
// 獲取時(shí)間參數(shù)的年、月、日(
System.out.println("獲取時(shí)間參數(shù)的年、月、日:");
LocalDateTime param = LocalDateTime.now();
System.out.println("year:" + param.getYear());
System.out.println("month:" + param.getMonth());
System.out.println("day:" + param.getDayOfMonth());
System.out.println("hour:" + param.getHour());
System.out.println("minute:" + param.getMinute());
System.out.println("second:" + param.getSecond() + "\n");
?
System.out.println("獲取后面的時(shí)刻:");
LocalDateTime today = LocalDateTime.now();
LocalDateTime tomorrow = today.plus(1, ChronoUnit.DAYS);
System.out.println("today:" + today);
System.out.println("tomorrow:" + tomorrow);
System.out.println("same object:" + today.equals(tomorrow) + "\n");
?
?
System.out.println("獲取以前的時(shí)刻:");
LocalDateTime oneWeekAgo = today.minus(1, ChronoUnit.WEEKS);
LocalDateTime oneMonthAgo = today.minus(1, ChronoUnit.MONTHS);
LocalDateTime oneYearAgo = today.minus(1, ChronoUnit.YEARS);
System.out.println("oneWeekAgo:" + oneWeekAgo);
System.out.println("oneMonthAgo:" + oneMonthAgo);
System.out.println("oneYearAgo:" + oneYearAgo + "\n");

}
結(jié)果:
獲取時(shí)間參數(shù)的年、月、日:
year:2022
month:MAY
day:3
hour:23
:7
:30
獲取后面的時(shí)刻:
today:2022-05-03T23:07:30.
:2022-05-04T23:07:30.
same :false
獲取以前的時(shí)刻:
:2022-04-26T23:07:30.
:2022-04-03T23:07:30. o
:2021-05-03T23:07:30.
3.修改時(shí)間 with
public void test4(){
LocalDateTime now = LocalDateTime.now();
System.out.println("today:" + now);
// 修改日期為本月的某天
LocalDateTime modifiedDateTime = now.with(ChronoField.DAY_OF_MONTH, 6);
System.out.println("modifiedDateTime:" + modifiedDateTime);
}
注意一下,之前我們plus時(shí)用的是,表示增幅單位,而這里則是指定修改的字段。
4.時(shí)區(qū) 和
public void test5() {
// 當(dāng)前時(shí)間
LocalDateTime now = LocalDateTime.now();
System.out.println("localDateTime:" + now);
// 獲取本地時(shí)區(qū)
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("local zone:" + zoneId);
// 當(dāng)前時(shí)間轉(zhuǎn)為時(shí)區(qū)時(shí)間。
ZonedDateTime beijingTime = now.atZone(zoneId);
System.out.println("beijingTime:" + beijingTime);
//北京時(shí)區(qū)的當(dāng)前時(shí)間對(duì)應(yīng)的東京時(shí)區(qū)的時(shí)間
ZoneId tokyoZone = ZoneId.of("Asia/Tokyo");
ZonedDateTime tokyoTime = beijingTime.withZoneSameInstant(tokyoZone);
System.out.println("tokyoTime:"+tokyoTime);
}
5.時(shí)間段(),時(shí)間點(diǎn)()和時(shí)間戳()
表示:時(shí)間的區(qū)間,用來度量秒和納秒之間的時(shí)間值
表示:一段時(shí)間的區(qū)間,用來度量年月日和幾天之間的時(shí)間值
表示:時(shí)間線上的一個(gè)瞬點(diǎn),用來記錄事件的時(shí)間戳
public void test6() {
// 當(dāng)前時(shí)間
LocalDateTime now = LocalDateTime.now();
System.out.println("localDateTime:" + now);//localDateTime:2022-05-03T23:38:56.377002400
LocalDateTime tomorrow = now.plus(1, ChronoUnit.DAYS);
Duration duration = Duration.between(now, tomorrow);

System.out.println("duration:" + duration);//duration:PT24H
System.out.println("diff:" + duration.getSeconds());//diff:86400
System.out.println("day:"+duration.toDays());//day:1
System.out.println("day:"+duration.toDaysPart());//day:1
LocalDate now = LocalDate.now();
System.out.println("localDate:" + now); //localDate:2022-05-03
LocalDate tomorrow = now.plus(1, ChronoUnit.DAYS);
Period period = Period.between(now, tomorrow);
System.out.println("period:" + period);//period:P1D
System.out.println("diff:"+period.getDays());//diff:1
System.out.println("diff:"+period.getMonths());//diff:0
}
public void test8() {
//now獲取的是本初子午線的時(shí)間
Instant now = Instant.now();
System.out.println(now); //2022-05-03T15:48:41.414906100Z
?
//添加時(shí)間的偏移量
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime); //2022-05-03T23:48:41.414906100+08:00
?
//獲取瞬時(shí)點(diǎn)對(duì)應(yīng)的毫秒數(shù)
long l = now.toEpochMilli();

System.out.println(l); //1651592921414
?
//通過給定的毫秒數(shù),獲取Instant實(shí)例
Instant instant = Instant.ofEpochMilli(1599831484077L);
System.out.println(instant);//2020-09-11T13:38:04.077Z
?
ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Shanghai"));
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
System.out.println("localDateTime:" + localDateTime);//localDateTime:2022-05-04T00:15:48.693292500
LocalDateTime localDateTime1 = LocalDateTime.now();
Instant instant1 = localDateTime1.toInstant(ZoneOffset.ofHours(0));
System.out.println("instant1:" + instant1.getEpochSecond());//instant1:1651623348
?
}
可見,對(duì)于某一個(gè)時(shí)間戳,給它關(guān)聯(lián)上指定的java獲取當(dāng)天零點(diǎn)的時(shí)間戳java獲取當(dāng)天零點(diǎn)的時(shí)間戳,就得到了,繼而可以獲得了對(duì)應(yīng)時(shí)區(qū)的。所以,,,,和long都可以互相轉(zhuǎn)換。
6.時(shí)間的格式化
public void test9() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
?
//必須得為1999-09-21,不能是1999-9-21
LocalDate localDate = LocalDate.parse("2022-05-03", formatter);
System.out.println("localDate:" + localDate);
String format1 = formatter.format(LocalDate.now());
System.out.println("format1:"+format1);
?
}