一.背景
作爲一個寫業務的Java程序員,往往需要處理和日期相關的代碼,比如獲取當天最早和最晚時間,比如獲取前三天時間,等等
- 之前都是用Date類,往往需要寫N多行代碼來進行轉換,還需要使用SimpleDateFormat等來進行格式化,可讀性非常差,Java 8提供了一組新的類方便處理時間。
java.time.LocalDate ->只對年月日做出處理
java.time.LocalTime ->只對時分秒納秒做出處理
java.time.LocalDateTime ->同時可以處理年月日和時分秒
二.使用
基本使用方法:
// 獲取今天的日期
LocalDate today = LocalDate.now();
// 今天是幾號
int dayofMonth = today.getDayOfMonth();
// 今天是周幾(返回的是個枚舉類型,需要再getValue())
int dayofWeek = today.getDayOfWeek().getValue();
// 今年是哪一年
int dayofYear = today.getDayOfYear();
來一些複雜的:
// 取本月第1天:
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
// 取本月第2天:
LocalDate secondDayOfThisMonth = today.withDayOfMonth(2);
// 取本月最後一天,不需要人爲計算本月有幾天
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());
// 取下一天:
LocalDate firstDayOfNextMonth = lastDayOfThisMonth.plusDays(1);
三.類似Stream的函數式編程形式
Java 8新增的Stream借鑑了函數式編程的思路,讓代碼可讀性更好,下面舉例說明:
需求是:獲得前三天凌晨0點0分0秒的時間戳
long ts = LocalDate.now().minusDays(3)
.atStartOfDay(ZoneId.systemDefault())
.toInstant()
.toEpochMilli();
解釋:
- Instant——代表的是時間戳,注意這裏默認的Instant是0時區,比北京少8個時區,例子:2018-10-08T09:50:21.852Z,相當於當天北京時間的17:50:21.852
- ZoneId.systemDefault(); //這是爲了獲取本地的時區
- toEpochMilli()//轉化爲時間戳,單位爲ms
四.使用總結
//獲取當前日期
LocalDate now = LocalDate.now();
System.out.println("系統當前日期爲:" + now);
//獲取當前日期時間
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("系統當前日期時間爲:" + localDateTime);
//默認時區
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("默認時區:" + zoneId);
//使用默認時區獲取當前時間
LocalDate now1 = LocalDate.now(ZoneId.systemDefault());
System.out.println("系統當前時間(使用默認時區)爲:" + now1);
//獲取指定日期
LocalDate specifiedDate = LocalDate.of(2017, 9, 22);
System.out.println("指定日期爲:" + specifiedDate);
//獲取指定時間
LocalDateTime specifiedDateTime = LocalDateTime.of(2017, 9, 22, 15, 40, 01);
System.out.println("指定時間爲:" + specifiedDateTime);
//獲取相差的天數 單位:ChronoUnit.DAYS 天數
LocalDate yesterday = LocalDate.of(2018, 1, 1);
LocalDate today = LocalDate.now(ZoneId.systemDefault());
long betweenDays = yesterday.until(today, ChronoUnit.DAYS);
System.out.println("相差的天數:" + betweenDays);
//獲取相差的星期數
long betweenWeeks = yesterday.until(today, ChronoUnit.WEEKS);
System.out.println("相差的星期數:" + betweenWeeks);
//獲取相差的世紀
LocalDate pass = LocalDate.of(1911, 1, 1);
long betweenCenturies = pass.until(today, ChronoUnit.CENTURIES);
System.out.println("相差的世紀數:" + betweenCenturies);
//獲取相差的幾十年 10年爲1個單位
LocalDate passDecades = LocalDate.of(2000, 1, 1);
long betweenDecades = passDecades.until(today, ChronoUnit.DECADES);
System.out.println("相差幾十年:" + betweenDecades);
//獲取相差的紀元
LocalDate passEras = LocalDate.of(2000, 1, 1);
long betweenEras = passEras.until(today, ChronoUnit.ERAS);
System.out.println("相差的紀元:" + betweenEras);
//日期字符串解析爲LocalDate對象
LocalDate parseDate = LocalDate.parse("2016-12-31");
System.out.println("字符串解析爲LocalDate對象:" + parseDate);
//調整月份
LocalDate currentLocalDate = LocalDate.now();
LocalDate newMonth = currentLocalDate.withMonth(10);
System.out.print("當前的日期爲:" + LocalDate.now());
System.out.println("調整當前的日期爲:" + newMonth);
//調整當前的時間 不會更改本地計算機的時間
LocalDateTime newCurrentDate = currentLocalDate.atTime(18,7,9);//18:07:09 LocalDateTime沒有atTime方法
System.out.print("當前的時間爲:" + LocalDateTime.now());
System.out.println("調整當前的時間爲:" + newCurrentDate);
//獲取當天的起始時間
LocalDate localDate = LocalDate.now();
LocalDateTime startOfDay = localDate.atStartOfDay();
System.out.println("當天的起始時間爲:" + startOfDay);
//獲取年、月、日
LocalDate nowLocalDate = LocalDate.now();
System.out.println("年份:" + nowLocalDate.getYear());
System.out.println("英文 月份:" + nowLocalDate.getMonth());
System.out.println("數字 月份:" + nowLocalDate.getMonthValue());
System.out.println("日份:" + nowLocalDate.getDayOfMonth());
System.out.println("英文 星期:" + nowLocalDate.getDayOfWeek());
System.out.println("本年已過多少天:" + nowLocalDate.getDayOfYear());
//調整時間
LocalDateTime nowDateTime = LocalDateTime.now();
//明天
LocalDateTime plusDays = nowDateTime.plusDays(1);
System.out.println("明天此時:" + plusDays);
//昨天
LocalDateTime plusDays2 = nowDateTime.plusDays(-1);
System.out.println("昨天此時:" + plusDays2);
//獲取日期 時間詳細信息
System.out.println(nowDateTime.atZone(ZoneId.systemDefault()));