精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
java新時(shí)間類

時(shí)間

java8以前使用的時(shí)間很多方法都已經(jīng)廢棄了,而且不是線程安全的,java8提供了一系列的時(shí)間類,這些時(shí)間類都是線程安全的

LocalDate、LocalTime、LocalDateTime

這三個(gè)關(guān)于時(shí)間的類在使用上都類似

/**	 * LocalDate	 */@Testpublic void test1() {  LocalDate date1 = LocalDate.now();  System.out.println(date1);//2020-03-30  LocalDate plusYears = date1.plusYears(1);  System.out.println(plusYears);//2021-03-30  LocalDate minusDays = date1.minusDays(2);  System.out.println(minusDays);//2020-03-28  LocalDate date2 = LocalDate.of(2019, 3, 30);  System.out.println(date2.getYear());//2019}/**	 * LocalTime	 */@Testpublic void test2() {  LocalTime now = LocalTime.now();  System.out.println(now);//21:15:23.418  int minute = now.getMinute();  System.out.println(minute);//15  int second = now.getSecond();  System.out.println(second);//23  LocalTime of = LocalTime.of(10, 10, 10);  System.out.println(of);//10:10:10  LocalTime minusMinutes = of.minusMinutes(2);  System.out.println(minusMinutes);//10:08:10  LocalTime plusHours = of.plusHours(2);  System.out.println(plusHours);//12:10:10}/**	 * LocalDateTime	 */@Testpublic void test3() {  LocalDateTime now = LocalDateTime.now();  System.out.println(now);//2020-03-30T21:20:37.961  int minute = now.getMinute();  System.out.println(minute);//20  LocalDateTime plusMinutes = now.plusMinutes(20);  System.out.println(plusMinutes);//2020-03-30T21:40:37.961  LocalDateTime minusYears = now.minusYears(2);  System.out.println(minusYears);//2018-03-30T21:20:37.961  LocalDateTime of = LocalDateTime.of(2021, 3, 30, 21, 19, 50);  System.out.println(of);//2021-03-30T21:19:50}

時(shí)間戳

/**	 * Instant	 */@Testpublic void test4() {  Instant now = Instant.now();  System.out.println(now);//2020-03-30T13:26:10.640Z  Instant plusSeconds = now.plusSeconds(10);  System.out.println(plusSeconds);//2020-03-30T13:26:20.640Z  //獲取時(shí)間戳相對于1970年0時(shí)0分0秒的毫秒數(shù)  long epochMilli = plusSeconds.toEpochMilli();  System.out.println(epochMilli);//1585574780640}

Duration獲取時(shí)間間隔

/**	 * Duration	 */@Testpublic void test5() {  Instant start = Instant.now();  Instant end = start.plusSeconds(10);  Duration duration = Duration.between(start, end);  long seconds = duration.getSeconds();  //獲取時(shí)間間隔的秒數(shù)  System.out.println(seconds);//10  //獲取時(shí)間間隔的毫秒數(shù)  long millis = duration.toMillis();  System.out.println(millis);//10000}

Peroid獲取日期間隔

@Testpublic void test6() {  LocalDate date1 = LocalDate.now();  LocalDate date2 = date1.plusYears(2);  Period period = Period.between(date1, date2);  //獲取兩時(shí)間間隔的月數(shù),指兩個(gè)月份的間隔數(shù),并不是時(shí)間間隔的總月數(shù)  int months = period.getMonths();  System.out.println(months);//0}

TemporalAdjuster矯正日期

@Testpublic void test7() {  LocalDate date1 = LocalDate.now();  System.out.println(date1);//2020-03-31  //TemporalAdjusters類中封裝了一些常用地時(shí)間矯正方法  TemporalAdjuster next = TemporalAdjusters.next(DayOfWeek.FRIDAY);  LocalDate date2 = date1.with(next);  System.out.println(date2);//2020-04-03  //自定義時(shí)間矯正器  LocalDate date3 = date1.with(x->{    LocalDate ld=(LocalDate)x;    DayOfWeek dayOfWeek = ld.getDayOfWeek();    if(dayOfWeek.equals(DayOfWeek.FRIDAY)) {      return ld.plusDays(3);    }else if(dayOfWeek.equals(DayOfWeek.SATURDAY)) {      return ld.plusDays(2);    }else {      return ld.plusDays(1);    }  });  System.out.println(date3);//2020-04-01}

由于TemporalAdjuster是一個(gè)函數(shù)式接口,所以我們可以使用lambda表達(dá)式自定義矯正規(guī)則

@FunctionalInterfacepublic interface TemporalAdjuster {  Temporal adjustInto(Temporal temporal);}

DateTimeFormatter格式化日期時(shí)間

@Testpublic void test8() {  LocalDateTime dateTime1 = LocalDateTime.now();  System.out.println(dateTime1);//2020-03-31T18:28:04.256  DateTimeFormatter formatter1 = DateTimeFormatter.ISO_DATE;  String format1 = dateTime1.format(formatter1);  System.out.println(format1);//2020-03-31  DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日HH時(shí)mm分ss秒");  String format2 = dateTime1.format(formatter2);  System.out.println(format2);//2020年03月31日18時(shí)28分04秒  LocalDateTime dateTime2 = LocalDateTime.parse(format2, formatter2);  System.out.println(dateTime2);//2020-03-31T18:28:04}

ZoneDate

@Testpublic void test9() {  //獲取可用時(shí)區(qū)  ZoneId.getAvailableZoneIds().forEach(System.out::println);  LocalDate date1 = LocalDate.now(ZoneId.of("Asia/Tokyo"));  System.out.println(date1);//2020-03-31}

ZoneTime

@Testpublic void test10() {  LocalTime time1 = LocalTime.now(ZoneId.of("Asia/Tokyo"));  System.out.println(time1);//19:44:15.228  OffsetTime atOffset = time1.atOffset(ZoneOffset.ofHours(2));  System.out.println(atOffset);//19:44:15.228+02:00}

ZoneDateTime

@Testpublic void test11() {  LocalDateTime dateTime1 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));  System.out.println(dateTime1);//2020-03-31T18:51:07.136  ZonedDateTime atZone = dateTime1.atZone(ZoneId.of("Asia/Shanghai"));  System.out.println(atZone);//2020-03-31T18:51:07.136+08:00[Asia/Shanghai]	+09:00表示時(shí)間比格林尼治時(shí)間快9小時(shí)}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Java 8 日期/時(shí)間(Date Time)API指南 – 碼農(nóng)網(wǎng)
Java 8 – 日期和時(shí)間實(shí)用技巧
Java 8 中新的 Date 和 Time 類入門詳解 – 碼農(nóng)網(wǎng)
Date操作
Java基礎(chǔ)之:日期類
Java 8 的時(shí)間和 Date 的對比
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

主站蜘蛛池模板: 沙洋县| 巨野县| 苍山县| 岑巩县| 武汉市| 松原市| 东乡族自治县| 高平市| 盐城市| 吉木萨尔县| 两当县| 固始县| 祁东县| 北辰区| 南召县| 闽清县| 田东县| 邯郸县| 大化| 明光市| 巴彦淖尔市| 日喀则市| 察隅县| 沛县| 平遥县| 依安县| 临泽县| 尼玛县| 鹿邑县| 红原县| 安国市| 方正县| 江城| 安康市| 登封市| 天全县| 苍溪县| 洛浦县| 彩票| 北辰区| 乌兰浩特市|