JDK8特性之時間API
LocalDate筐骇、LocalDateTIme、LocalTIme
方法 |
描述 |
now()/* now(Zoneld zone) |
靜態(tài)方法,根據(jù)當前時間創(chuàng)建對象/指定時區(qū)的對象 |
of() |
靜態(tài)方法,根據(jù)指定日期/時間創(chuàng)建對象 |
getDayOfMonth()/getDayOfYear() |
獲取月份天數(shù)(1-31)/獲取年份天數(shù)(1~366) |
getDayOfWeek() |
獲取星期幾(返回一個DayOfWeek枚舉值) |
getMonth() |
獲取月份腊瑟,返回一個Month枚舉值 |
getMonthValue()/getYear() |
獲取魚粉(1-12)/獲取年份 |
getHour()/getMinute()/getSeconde() |
獲取當前對象對應的小時聚假、分鐘块蚌、秒 |
withDayOfMonth()/withDayOfYear()/withMonty()/withYear() |
將月份天數(shù)、年份天數(shù)膘格、月份峭范、年份修改為指定的值并返回新的對象 |
plusDays()/plusWeeks()/plusMonths()/plusYears()/plusHours() |
向當前對象添加幾天、幾周瘪贱、幾個月纱控、幾年、幾小時 |
minusMonths()/minusWeeks()/minusDays()/minusYears()/minusHours() |
從當前對象減去幾月菜秦、幾周甜害、幾天、幾年球昨、幾小時 |
package com.felixfei.study.test;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
/**
* 為什么會出現(xiàn)新的日期API
* 1. 可變性:像日期和時間這樣的類應該是不可變的(calendar是可變的尔店,修改的是本身的值)
* 2. 偏移性:Date中的年份是從1900年開始的,而月份都從0開始
* 3. 格式化:格式化只對Date有用主慰,Calendar則不行
* 4. 此外嚣州,他們也不是線程安全的,不能處理閏秒等
*/
public class LocalDateTimeTest {
public static void main(String[] args) {
// 獲取2021年3月22號的時間共螺,要進行偏移量計算
Date date = new Date(2021 - 1900, 3 - 1, 22);
System.out.println("2021年3月22號時間為:" + date);
// LocalDate该肴、LocalDateTime、LocalTime
LocalDate now = LocalDate.now();
LocalDateTime now1 = LocalDateTime.now();
LocalTime now2 = LocalTime.now();
System.out.println("當前日期:" + now);
System.out.println("當時間:" + now1);
System.out.println("當前日期時間:" + now2);
// 獲取指定的日期時間藐不,沒有偏移量
LocalDateTime of = LocalDateTime.of(2020, 3, 10, 22, 12, 13);
System.out.println("指定的日期時:" + of);
System.out.println("當月的第幾天:" + now.getDayOfMonth());
System.out.println("這周的第幾天:" + now.getDayOfWeek());
System.out.println("第幾月:" + now.getMonth());
System.out.println("月份數(shù)值:" + now.getMonthValue());
System.out.println("今天的第幾天:" + now.getDayOfYear());
// 不可變性匀哄,修改的返回是新的值
LocalDate localDate = now.withDayOfMonth(23);
System.out.println("修改天數(shù)后的值:" + localDate);
LocalDate localDate1 = now.plusDays(2);
System.out.println("增加1天后的值:" + localDate1);
LocalDate localDate2 = now.minusDays(6);
System.out.println("減6天后的值:" + localDate2);
}
}
Instant
- Instant:時間線上的一個瞬時點秦效。這可能被用來記錄應用程序中的事件時間戳。
- 在處理時間和日期的時候拱雏,我們通常會想到年棉安,月,日铸抑,時贡耽,分,秒鹊汛。然而蒲赂,這只是事件的一個模型,是面向人類的刁憋。第二種通用模型是面向機器的滥嘴,或者說是連續(xù)的。在此模型中至耻,時間線中的一個點表示為一個很大的數(shù)若皱,這有利于計算機處理。在UNIX中尘颓,這個數(shù)從1970年開始走触,以秒為單位:同樣的,在Java中疤苹,也是從1970年開始互广,但以毫秒為單位。
- java.time包通過值類型Instant提供機器試圖卧土,不提供處理人類意義上的時間單位惫皱。Instant表示時間線上的一點,而不需要任何上下文信息尤莺,例如:時區(qū)旅敷。概念上講,它只是簡單的表示自1970年1月1日0時0分0秒(UTC)開始的秒數(shù)颤霎,因為java.time包是基于納秒計算的媳谁,所以Instant的精度可以達到納秒級。
方法 |
描述 |
now() |
靜態(tài)方法捷绑,返回默認UTC時區(qū)的Instant類的對象 |
ofEpochMilli(long epochMilli) |
靜態(tài)方法韩脑,返回在1970-01-01 00:00:00基礎(chǔ)上加上指定毫秒數(shù)之后的Instant類的對象 |
atOffset(ZoneOffset offset) |
結(jié)合即時的創(chuàng)建一個OffsetDateTime |
toEpochMilli() |
返回1970-01-01 00:00:00到當前時間的毫秒數(shù),即為時間戳 |
- 時間戳是指格林威治時間1970年01月01日00時00分00起至現(xiàn)在的總秒數(shù)
package com.felixfei.study.test;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
/**
* Instant:時間線上的一個瞬時點粹污,這可能被用來記錄應用程序中的事件時間戳段多。
* 類似于java.util.Date類
*/
public class LocalDateTimeTest {
public static void main(String[] args) {
Instant now = Instant.now(); // 獲取的是本初子午線的時間
System.out.println("獲取的是本初子午線的時間=" + now);
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.of("+8"));
System.out.println("獲取的東八區(qū)的時間=" + offsetDateTime);
long l = now.toEpochMilli();
System.out.println("獲取自1970年1月1日0時0分0秒(UTC)開始的毫秒數(shù)=" + l);
Instant instant = Instant.ofEpochMilli(l);
System.out.println("通過給定的毫秒數(shù),獲取的Instant實例=" + instant);
}
}
DateTimeFormatter類
- 該類提供了三種格式化時間方法
- 自動以格式壮吩,如:
ofPattern("yyyy-MM-dd hh:mm:ss E")
方法 |
描述 |
ofPattern(String pattern) |
靜態(tài)方法进苍,返回一個指定字符串格式的DateTImeFormatter |
format(TemproalAccessor t) |
格式化一個日期加缘、時間,返回字符串 |
parse(CharSequence text) |
將指定格式的字符序列解析為一個日期觉啊、時間 |
package com.felixfei.study.test;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
public class LocalDateTimeTest {
public static void main(String[] args) {
DateTimeFormatter isoDate = DateTimeFormatter.ISO_DATE;
String str1 = isoDate.format(LocalDateTime.now());
System.out.println("格式化后的字符串日期=" + str1);
TemporalAccessor parse = isoDate.parse(str1);
System.out.println("字符串解析后的日期=" + parse);
// FormatStyle.LONG 2021年3月24日 下午08時53分19秒 可使用 ofLocalizedDateTime 方法
// FormatStyle.MEDIUM 2021-3-24 20:55:10 可使用 ofLocalizedDateTime 方法
// FormatStyle.SHORT 21-3-24 下午8:55 可使用 ofLocalizedDateTime 方法
// FormatStyle.FULL 2021年3月24日 星期三 這種要使用 ofLocalizedDate 方法
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
String format = formatter.format(LocalDateTime.now());
System.out.println("格式化對應樣式后的日期時間字符串=" + format);
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String format1 = formatter1.format(LocalDateTime.now());
System.out.println("格式化對應樣式后的日期字符串=" + format1);
TemporalAccessor parse1 = formatter1.parse(format1);
System.out.println("字符串解析后的日期=" + parse1); // 注意字符串的格式必須和ofPattern中的一致
System.out.println("獲取解析后的年="+parse1.get(ChronoField.YEAR));
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者