????????????????????????????????????????Java 8 中處理日期和時(shí)間示例
package com.demo.test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
/**
* @Author:Dai Zhipeng
* @Description: Java 8 中處理日期和時(shí)間示例
* @Date: Created in 9:44 2019/2/26
* @Modified by:
*/
public class TestJava8Time {
public static void main(String args[]) {
TestJava8Time test =new TestJava8Time();
? ? ? ? test.dealSpecificTime();
? ? ? ? System.out.println("**********");
? ? ? ? test.checkBirthday();
? ? ? ? System.out.println("**********");
? ? ? ? test.getCurrentTime();
? ? ? ? System.out.println("**********");
? ? ? ? test.compareDate();
? ? ? ? System.out.println("**********");
? ? ? ? test.dealDifferentZoneDateTime();
? ? ? ? System.out.println("**********");
? ? ? ? test.compareTwoDate();
? ? ? ? System.out.println("**********");
? ? ? ? test.analyzeDate();
? ? ? ? System.out.println("**********");
? ? ? ? test.timeConvertDate();
? ? }
/**
? ? * 獲取今天的日期(指年月日)和按照指定日期,進(jìn)行相應(yīng)操作
? ? */
? ? private void dealSpecificTime() {
LocalDate todayDate = LocalDate.now();
? ? ? ? System.out.println("今天的日期:" + todayDate);
? ? ? ? //獲取今年當(dāng)前這個(gè)月的第1天
? ? ? ? LocalDate firstDay = todayDate.with(TemporalAdjusters.firstDayOfMonth());
? ? ? ? System.out.println("今年的當(dāng)前月份的第一天:" + firstDay);
? ? ? ? //獲取今年當(dāng)前這個(gè)月的第1天,另外一種寫法
? ? ? ? LocalDate firstDay2 = todayDate.withDayOfMonth(1);
? ? ? ? System.out.println("今年的當(dāng)前月份的第一天(另一種寫法):" + firstDay2);
? ? ? ? //獲取今年當(dāng)前這個(gè)月的最后1天,不用考慮大月,小月,平年,閏年
? ? ? ? LocalDate lastDay = todayDate.with(TemporalAdjusters.lastDayOfMonth());
? ? ? ? System.out.println("今年當(dāng)前這個(gè)月的最后1天:" + lastDay);
? ? ? ? //當(dāng)前日期+1天
? ? ? ? LocalDate tomorrow = todayDate.plusDays(1);
? ? ? ? System.out.println("N天后的日期為:" + tomorrow);
? ? ? ? //判斷是否為閏年
? ? ? ? boolean isLeapYear = todayDate.isLeapYear();
? ? ? ? System.out.println("是否為閏年:" + isLeapYear);
? ? }
/**
? ? * 判斷當(dāng)天是否為某人的生日,當(dāng)天是否為賬單日
? ? */
? ? private void checkBirthday() {
boolean flag =false;
? ? ? ? LocalDate todayDate = LocalDate.now();
? ? ? ? LocalDate birthday = LocalDate.of(1990, 10, 20);
? ? ? ? MonthDay birthdayMd = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
? ? ? ? MonthDay today = MonthDay.from(LocalDate.of(todayDate.getYear(), todayDate.getMonth(), todayDate.getDayOfMonth()));
? ? ? ? if (today.equals(birthdayMd)) {
flag =true;
? ? ? ? }
System.out.println("是否今天過生日:" + flag);
? ? }
/**
? ? * 獲取當(dāng)前時(shí)間用LocalTime
*/
? ? private void getCurrentTime() {
//獲取當(dāng)前時(shí)間(包含毫秒)
? ? ? ? LocalTime currentTime = LocalTime.now();
? ? ? ? System.out.println("獲取當(dāng)前時(shí)間(包含毫秒)為:" + currentTime);
? ? ? ? //獲取當(dāng)前時(shí)間(不含毫秒)
? ? ? ? LocalTime currentTime2 = LocalTime.now().withNano(0);
? ? ? ? System.out.println("獲取當(dāng)前時(shí)間(不包含毫秒)為:" + currentTime2);
? ? ? ? //指定時(shí)間
? ? ? ? LocalTime time = LocalTime.of(14, 10, 21);
? ? ? ? System.out.println("指定時(shí)間1為:" + time);
? ? ? ? LocalTime time2 = LocalTime.parse("12:00:01");
? ? ? ? System.out.println("指定時(shí)間2為:" + time2);
? ? ? ? //當(dāng)前時(shí)間增加2小時(shí)(方式一)
? ? ? ? LocalTime nowTimePlus2Hour = currentTime.plusHours(2);
? ? ? ? System.out.println("當(dāng)前時(shí)間增加2小時(shí)(方式一)為:" + nowTimePlus2Hour.withNano(0));
? ? ? ? //當(dāng)前時(shí)間增加2小時(shí)(方式二)
? ? ? ? LocalTime nowTimePlus2Hour2 = currentTime.plus(2, ChronoUnit.HOURS);
? ? ? ? System.out.println("當(dāng)前時(shí)間增加2小時(shí)(方式二)為:" + nowTimePlus2Hour2.withNano(0));
? ? }
/**
? ? * 比較2個(gè)日期哪個(gè)在前,哪個(gè)在后;isAfter(),isBefore()
*/
? ? private void compareDate() {
LocalDate today = LocalDate.now();
? ? ? ? LocalDate specifyDate1 = LocalDate.of(2015, 10, 20);
? ? ? ? System.out.println("當(dāng)前日期是否在指定日期1后面:" + today.isAfter(specifyDate1));
? ? ? ? LocalDate specifyDate2 = LocalDate.of(2019, 10, 20);
? ? ? ? System.out.println("當(dāng)前日期是否在指定日期2前面:" + today.isBefore(specifyDate2));
? ? }
/**
? ? * 處理不同時(shí)區(qū)的時(shí)間
? ? */
? ? private void dealDifferentZoneDateTime() {
//查看當(dāng)前的時(shí)區(qū)
? ? ? ? ZoneId defaultZone = ZoneId.systemDefault();
? ? ? ? //Asia/Shanghai
? ? ? ? System.out.println("查看當(dāng)前的時(shí)區(qū):" + defaultZone);
? ? ? ? //查看美國紐約當(dāng)前的時(shí)間
? ? ? ? ZoneId america = ZoneId.of("America/New_York");
? ? ? ? LocalDateTime shanghaiTime = LocalDateTime.now();
? ? ? ? LocalDateTime americaDateTime = LocalDateTime.now(america);
? ? ? ? System.out.println("上海時(shí)間為:" + shanghaiTime);
? ? ? ? System.out.println("美國時(shí)間為:" + americaDateTime);
? ? ? ? //帶有時(shí)區(qū)的時(shí)間
? ? ? ? ZonedDateTime americaZoneDateTime = ZonedDateTime.now(america);
? ? ? ? //2019-02-26T00:49:14.910-05:00[America/New_York]
? ? ? ? System.out.println("帶有時(shí)區(qū)的美國時(shí)間為:" + americaZoneDateTime);
? ? }
/**
? ? * 比較兩個(gè)日期之前時(shí)間差:在項(xiàng)目中,經(jīng)常需要比較兩個(gè)日期之間相差幾天,
? ? * 或者相隔幾個(gè)月,我們可以使用java8的Period來進(jìn)行處理;
? ? * 我們使用Period類比較天數(shù),比較奇怪,他返回的值,并不是2個(gè)日期之間總共的天數(shù)差,
? ? * 而是一個(gè)相對(duì)天數(shù)差,比如,5月1日,和10月2日,他比較的是僅僅2個(gè)天之間的差,那1號(hào)和2號(hào),相差1天,
? ? * 而實(shí)際上,因?yàn)橹虚g相差了好幾個(gè)月,所以真正的天數(shù)差肯定不是1天,
? ? * 所以我們可以使用until,并指明精度單位是days,就可以計(jì)算真正的天數(shù)差了
? ? */
? ? private void compareTwoDate() {
LocalDate today = LocalDate.now();
? ? ? ? LocalDate specifyDate = LocalDate.of(2015, 10, 2);
? ? ? ? Period period = Period.between(specifyDate, today);
? ? ? ? //天數(shù)間隔
? ? ? ? System.out.println(period.getDays());
? ? ? ? //月份間隔
? ? ? ? System.out.println(period.getMonths());
? ? ? ? //天數(shù)差
? ? ? ? System.out.println(specifyDate.until(today, ChronoUnit.DAYS));
? ? }
/**
? ? * 日期時(shí)間格式解析、格式化
? ? */
? ? private void analyzeDate() {
String specifyDate ="20151011";
? ? ? ? DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
? ? ? ? LocalDate formatted = LocalDate.parse(specifyDate, formatter);
? ? ? ? System.out.println("yyyy-MM-dd的轉(zhuǎn)換格式為:" + formatted);
? ? ? ? //自定義時(shí)間轉(zhuǎn)換格式
? ? ? ? DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("YYYY MM dd");
? ? ? ? System.out.println("yyyy MM dd的轉(zhuǎn)換格式為:" + formatter2.format(LocalDate.now()));
? ? }
/**
? ? * java8 時(shí)間類與Date類的相互轉(zhuǎn)化:因?yàn)閖ava8之前Date是包含日期和時(shí)間的,
? ? * 而LocalDate只包含日期,LocalTime只包含時(shí)間,
? ? * 所以與Date在互轉(zhuǎn)中,勢(shì)必會(huì)丟失日期或者時(shí)間,或者會(huì)使用起始時(shí)間
? ? */
? ? private void timeConvertDate() {
//Date與Instant的相互轉(zhuǎn)化
? ? ? ? Instant instant = Instant.now();
? ? ? ? Date date = Date.from(instant);
? ? ? ? Instant instant2 = date.toInstant();
? ? ? ? //Date轉(zhuǎn)為LocalDateTime
? ? ? ? Date date2 =new Date();
? ? ? ? LocalDateTime localDateTime2 = LocalDateTime.ofInstant(date2.toInstant(), ZoneId.systemDefault());
? ? ? ? //LocalDateTime轉(zhuǎn)Date
? ? ? ? LocalDateTime localDateTime3 = LocalDateTime.now();
? ? ? ? Instant instant3 = localDateTime3.atZone(ZoneId.systemDefault()).toInstant();
? ? ? ? Date date3 = Date.from(instant);
? ? ? ? //LocalDate轉(zhuǎn)Date
? ? ? ? //因?yàn)長ocalDate不包含時(shí)間,所以轉(zhuǎn)Date時(shí),會(huì)默認(rèn)轉(zhuǎn)為當(dāng)天的起始時(shí)間,00:00:00
? ? ? ? LocalDate localDate4 = LocalDate.now();
? ? ? ? Instant instant4 = localDate4.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
? ? ? ? Date date4 = Date.from(instant);
? ? }
}