1. 在 Java 8 中獲取今天的日期
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),萬山企業(yè)網(wǎng)站建設(shè),萬山品牌網(wǎng)站建設(shè),網(wǎng)站定制,萬山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,萬山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
LocalDate now = LocalDate.now();
System.out.println(now);
//結(jié)果
2018-06-20
2. 在 Java 8 中獲取年、月、日信息
LocalDate now = LocalDate.now();
int year = now.getYear();
int monthValue = now.getMonthValue();
int dayOfMonth = now.getDayOfMonth();
System.out.printf("year = %d, month = %d, day = %d", year, monthValue, ayOfMonth);
//結(jié)果
year = 2018, month = 6, day = 20
3. 在 Java 8 中處理特定日期
LocalDate date = LocalDate.of(2018, 06, 20);
System.out.println(date);
4. 在 Java 8 中判斷兩個(gè)日期是否相等
LocalDate now = LocalDate.now();
LocalDate date = LocalDate.of(2018, 06, 20);
if (date.equals(now)) {
System.out.println("同一天");
}
5. 在 Java 8 中檢查像生日這種周期性事件
LocalDate now = LocalDate.now();
LocalDate dateOfBirth = LocalDate.of(2018, 06, 20);
MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(now);
if (currentMonthDay.equals(birthday)) {
System.out.println("Happy Birthday");
} else {
System.out.println("Sorry, today is not your birthday");
}
//結(jié)果
Happy Birthday
6. 在 Java 8 中獲取當(dāng)前時(shí)間
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
//結(jié)果可以看到當(dāng)前時(shí)間就只包含時(shí)間信息,沒有日期。
13:35:56.155
7. 如何在現(xiàn)有的時(shí)間上增加小時(shí)
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
LocalTime localTime1 = localTime.plusHours(2);//增加2小時(shí)
System.out.println(localTime1);
//結(jié)果
13:41:20.721
15:41:20.721
8. 如何計(jì)算一周后的日期
LocalDate now = LocalDate.now();
LocalDate plusDate = now.plus(1, ChronoUnit.WEEKS);
System.out.println(now);
System.out.println(plusDate);
//結(jié)果
2018-06-20
2018-06-27
9. 計(jì)算一年前或一年后的日期
LocalDate now = LocalDate.now();
LocalDate minusDate = now.minus(1, ChronoUnit.YEARS);
LocalDate plusDate1 = now.plus(1, ChronoUnit.YEARS);
System.out.println(minusDate);
System.out.println(plusDate1);
//結(jié)果
2017-06-20
2019-06-20
10. 使用 Java 8 的 Clock 時(shí)鐘類
Clock clock = Clock.systemUTC();
Clock clock1 = Clock.systemDefaultZone();
System.out.println(clock);
System.out.println(clock1);
//結(jié)果
SystemClock[Z]
SystemClock[Asia/Shanghai]
11. 如何用 Java 判斷日期是早于還是晚于另一個(gè)日期
LocalDate tomorrow = LocalDate.of(2018,6,20);
if(tomorrow.isAfter(now)){
System.out.println("Tomorrow comes after today");
}
LocalDate yesterday = now.minus(1, ChronoUnit.DAYS);
if(yesterday.isBefore(now)){
System.out.println("Yesterday is day before today");
}
12. 在 Java 8 中處理時(shí)區(qū)
ZoneId america = ZoneId.of("America/New_York");
LocalDateTime localtDateAndTime = LocalDateTime.now();
ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america );
System.out.println(dateAndTimeInNewYork);
13. 如何表示信用卡到期這類固定日期,答案就在 YearMonth
YearMonth currentYearMonth = YearMonth.now();
System.out.printf("Days in month year %s: %d%n", currentYearMonth, currentYearMonth.lengthOfMonth());
YearMonth creditCardExpiry = YearMonth.of(2018, Month.FEBRUARY);
System.out.printf("Your credit card expires on %s %n", creditCardExpiry);
//結(jié)果
Days in month year 2018-06: 30
Your credit card expires on 2018-02
14. 如何在 Java 8 中檢查閏年
15. 計(jì)算兩個(gè)日期之間的天數(shù)和月數(shù)
LocalDate date = LocalDate.of(2019, Month.MARCH, 20);
Period period = Period.between(now, date);
System.out.println("離下個(gè)時(shí)間還有" + period.getMonths() + " 個(gè)月");
16. 包含時(shí)差信息的日期和時(shí)間
在 Java 8 中,ZoneOffset 類用來表示時(shí)區(qū),舉例來說印度與 GMT 或 UTC 標(biāo)準(zhǔn)時(shí)區(qū)相差 +05:30,可以通過ZoneOffset.of() 靜態(tài)方法來獲取對應(yīng)的時(shí)區(qū)。一旦得到了時(shí)差就可以通過傳入 LocalDateTime 和 ZoneOffset 來創(chuàng)建一個(gè) OffSetDateTime 對象。
LocalDateTime datetime = LocalDateTime.of(2014, Month.JANUARY, 14,19,30);
ZoneOffset offset = ZoneOffset.of("+05:30");
OffsetDateTime date = OffsetDateTime.of(datetime, offset);
System.out.println("Date and Time with timezone offset in Java : " + date);
17. 在 Java 8 中獲取當(dāng)前的時(shí)間戳
Instant timestamp = Instant.now();
System.out.println(timestamp);
// 結(jié)果
2018-06-20T06:35:24.881Z
18. 在 Java 8 中如何使用預(yù)定義的格式化工具去解析或格式化日期
19. 如何在 Java 中使用自定義格式化工具解析日期
//DateTimeFormatter 如何使用:
//解析日期
String dateStr= "2018年06月20日";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
LocalDate date= LocalDate.parse(dateStr, formatter);
//日期轉(zhuǎn)換為字符串
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm a");
String nowStr = now .format(format);
20. 在 Java 8 中如何把日期轉(zhuǎn)換成字符串
LocalDateTime arrivalDate = LocalDateTime.now();
try {
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMMdd yyyy hh:mm a");
String landing = arrivalDate.format(format);
System.out.printf("Arriving at : %s %n", landing);
}catch (DateTimeException ex) {
System.out.printf("%s can't be formatted!%n", arrivalDate);
ex.printStackTrace();
}