最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java8新特性之日期時間API

 更新時間:2021年04月16日 11:39:33   作者:我思想出了問題  
這篇文章主要介紹了java8新特性之日期時間API,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下

jdk8之前

 一、java.lang.System

在這里插入圖片描述

long times = System.currentTimeMillis();
		//返回的是當(dāng)前時間與1970年1月1月1日0分0秒之間以毫秒為單位的時間差
		//稱為時間戳
		System.out.println(times);

在這里插入圖片描述

二、java.util.Date And java.sql.Date

在這里插入圖片描述

將java.util.Date 對象轉(zhuǎn)換為java.sql.Date對象:

	//將java.util.Date 對象轉(zhuǎn)換為java.sql.Date對象
		Date date1 = new Date();
		java.sql.Date date2 = new java.sql.Date(date1.getTime());

三、java.text.SimpleDateFormat

SimpleDateFormat是對日期Date類的格式化和解析。

在這里插入圖片描述

兩個操作:

1.格式化:

將日期轉(zhuǎn)換為字符串:

SimpleDateFormat sfd = new SimpleDateFormat();
		Date date = new Date();
		System.out.println(date);
		String formateDate = sfd.format(date);
		System.out.println(formateDate);

在這里插入圖片描述

也可以指定具體的格式化格式,查看具體的API格式。
例:指定格式的格式化輸出(調(diào)用帶參數(shù)的構(gòu)造器)

Date date = new Date();
		System.out.println(date);
		SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String formateDate = sfd.format(date);
		System.out.println(formateDate);

在這里插入圖片描述

2.解析:

將 字符串 轉(zhuǎn)換為 日期。即格式化的逆過程。

String str = "2021/4/12 下午10:16";
	    SimpleDateFormat sfd = new SimpleDateFormat();
		Date date2 = sfd.parse(str);
		System.out.println(date2);

這個注意要拋異常(傳入的str格式要與Date的原格式一致,或者說要與SimpleDateFormate當(dāng)前識別的格式相同)。

練習(xí):將字符串“2021-04-13” 轉(zhuǎn)換為java.sql.Date類型對象。

分析:首先將字符串解析為Date類型的對象,然后在轉(zhuǎn)為java.sql.Date類型對象。

public static void testExper() throws ParseException {
		String s = "2021-04-13";
		SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sfd.parse(s);
		java.sql.Date date2 = new java.sql.Date(date.getTime());
		System.out.println(date2);
	}

在這里插入圖片描述

四、java.util.Calendar

在這里插入圖片描述

常用實例化方法:

	Calendar calendar = Calendar.getInstance();
		System.out.println(calendar.getClass()); //java.util.GregorianCalendar,其實還是子類類型的對象

常用方法:

1.get():獲取常用的屬性和信息。
2.set():設(shè)置:相當(dāng)于把本身的日期給改變了
3.add():添加(增加時間、天數(shù))
4.getTime():日歷類----> Date類
5.setTime():Date類----> 日歷類

Calendar calendar = Calendar.getInstance();
//		System.out.println(calendar.getClass()); //java.util.GregorianCalendar
		//get()
		int days = calendar.get(calendar.DAY_OF_MONTH);//獲取當(dāng)前日期是這個月的第幾天
		System.out.println(days); //13
		//set()
		calendar.set(calendar.DAY_OF_MONTH, 22);//重新設(shè)置
	    days = calendar.get(calendar.DAY_OF_MONTH);//在重新獲取
		System.out.println(days); //22
		//add()
		calendar.add(calendar.DAY_OF_MONTH, 3);
		days = calendar.get(calendar.DAY_OF_MONTH);
		System.out.println(days); //25

		//getTime():日歷類----> Date類
		Date date = calendar.getTime();
		System.out.println(date); //Sun Apr 25 13:14:59 CST 2021

		//setTime():Date類----> 日歷類
		Date date2 = new Date();
		calendar.setTime(date2);
		days = calendar.get(calendar.DAY_OF_MONTH);
		System.out.println(days); //13

在這里插入圖片描述

jdk8中:java.time

新日期時間API出現(xiàn)的背景:

可變性:像日期和時間這樣的類應(yīng)該是不可變的。
偏移性:Date中的年份是從1900開始的,而月份都是從0開始。
格式化:格式化只對Date有用,Calendar則不行。

此外,他們也不是線程安全的;不能處理閏秒。

在這里插入圖片描述
在這里插入圖片描述

一、常用類

說明:LocalDateTime類相較于其他兩個類使用頻率較高。

在這里插入圖片描述

二、一些方法

在這里插入圖片描述

(1)now():獲取當(dāng)前的日期、時間、日期+時間。(實例化方法一)

LocalDate localDate = LocalDate.now();
		LocalTime localTime = LocalTime.now();
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println(localDate);
		System.out.println(localTime);
		System.out.println(localDateTime);

在這里插入圖片描述

(2)of():設(shè)置指定時間的年、月、日、時、分。沒有偏移量。(實例化方法二)

//of():設(shè)置指定時間的年、月、日、時、分。沒有偏移量。
		LocalDateTime localDateTime2 = LocalDateTime.of(2021, 4, 13, 15, 20);
		System.out.println(localDateTime2);

(3)getXxx():獲取…
(4)withXxx():修改(設(shè)置)…,這個方法不會改動原本的值。
(5)plusXxx():添加
(6)minusXxx():減

三、Instant

在這里插入圖片描述

在這里插入圖片描述

(1)now():獲取本初子午線對應(yīng)的標(biāo)準(zhǔn)時間。(實例化方法一)

//now():獲取本初子午線對應(yīng)的標(biāo)準(zhǔn)時間
		Instant instant = Instant.now();
		System.out.println(instant);
		//添加時間的偏移量
		OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
		System.out.println(offsetDateTime);

(2)toEpochMilli():獲取毫秒數(shù)

long milli = instant.toEpochMilli();
		System.out.println(milli);

(3)ofEpochMilli():通過給定的毫秒數(shù),獲取Instant實例 (實例化方法二)

//通過給定的毫秒數(shù),獲取Instant實例
		Instant instant2 = Instant.ofEpochMilli(1618300028028l);
		System.out.println(instant2);

四、DateTimeFormatter類

DateTimeFormatter類:格式化或解析日期、時間,類似于SimpleDateFormat。

在這里插入圖片描述

(1)預(yù)定義的標(biāo)準(zhǔn)格式進(jìn)行格式化:DateTimeFormatter.ISO_LOCAL_DATE_TIME

注意: 日期-----> 字符串

DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
		LocalDateTime localDateTime = LocalDateTime.now();
		String str = formatter.format(localDateTime);//注意類型變化
		System.out.println(localDateTime);
		System.out.println(str);

(2)本地化相關(guān)的格式。如:ofLocalizedDateTime()。

//FormatStyle.SHORT / FormatStyle.LONG / FormatStyle.MEDIUM :適用于LocalDateTime

	DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
		LocalDateTime localDateTime = LocalDateTime.now();
		String str = formatter.format(localDateTime);
		System.out.println(localDateTime);
		System.out.println(str);

在這里插入圖片描述

(3)自定義格式:ofPattern()

//自定義格式。如:
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
		String string = formatter.format(LocalDateTime.now());
		System.out.println(string);

在這里插入圖片描述

在這里插入圖片描述

到此這篇關(guān)于java8新特性之日期時間API的文章就介紹到這了,更多相關(guān)Java日期時間API內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring MVC+mybatis實現(xiàn)注冊登錄功能

    Spring MVC+mybatis實現(xiàn)注冊登錄功能

    這篇文章主要為大家詳細(xì)介紹了Spring MVC+mybatis實現(xiàn)注冊登錄功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • JUC之CountdownLatch使用詳解

    JUC之CountdownLatch使用詳解

    這篇文章主要介紹了JUC之CountdownLatch使用詳解,CountdownLatch 用來進(jìn)行線程同步協(xié)作,等待所有線程完成倒計時,
    其中構(gòu)造參數(shù)用來初始化等待計數(shù)值,await() 用來等待計數(shù)歸零,countDown() 用來讓計數(shù)減一,需要的朋友可以參考下
    2023-12-12
  • java中Lamda表達(dá)式講解

    java中Lamda表達(dá)式講解

    本文詳細(xì)講解了java中的Lamda表達(dá)式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • Java MD5加密(實例講解)

    Java MD5加密(實例講解)

    下面小編就為大家?guī)硪黄狫ava MD5加密(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • SpringSecurity實現(xiàn)自定義用戶認(rèn)證方案

    SpringSecurity實現(xiàn)自定義用戶認(rèn)證方案

    Spring?Security?實現(xiàn)自定義用戶認(rèn)證方案可以根據(jù)具體需求和業(yè)務(wù)場景進(jìn)行設(shè)計和實施,滿足不同的安全需求和業(yè)務(wù)需求,這種靈活性使得認(rèn)證機(jī)制能夠更好地適應(yīng)各種復(fù)雜的環(huán)境和變化?,本文給大家介紹了SpringSecurity實現(xiàn)自定義用戶認(rèn)證方案,需要的朋友可以參考下
    2025-01-01
  • java刪除文件時總是返回false,刪不掉的解決方案

    java刪除文件時總是返回false,刪不掉的解決方案

    這篇文章主要介紹了java刪除文件時總是返回false,刪不掉的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring Boot 中使用 JSON Schema 校驗復(fù)雜JSON數(shù)據(jù)的過程

    Spring Boot 中使用 JSON Schema 校驗復(fù)雜JSO

    在數(shù)據(jù)交換領(lǐng)域,JSON Schema 以其強(qiáng)大的標(biāo)準(zhǔn)化能力,為定義和規(guī)范 JSON 數(shù)據(jù)的結(jié)構(gòu)與規(guī)則提供了有力支持,下面給大家介紹Spring Boot 中使用 JSON Schema 校驗復(fù)雜JSON數(shù)據(jù)的過程,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • 簡單了解Spring循環(huán)依賴解決過程

    簡單了解Spring循環(huán)依賴解決過程

    這篇文章主要介紹了簡單了解Spring循環(huán)依賴解決過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • Mybatis-Plus 多表聯(lián)查分頁的實現(xiàn)代碼

    Mybatis-Plus 多表聯(lián)查分頁的實現(xiàn)代碼

    本篇文章主要介紹了Mybatis-Plus 多表聯(lián)查分頁的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • Spring Boot項目@RestController使用重定向redirect方式

    Spring Boot項目@RestController使用重定向redirect方式

    這篇文章主要介紹了Spring Boot項目@RestController使用重定向redirect方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評論

稻城县| 体育| 丹江口市| 西藏| 民勤县| 舟曲县| 乳山市| 崇仁县| 海门市| 特克斯县| 句容市| 会宁县| 左权县| 长治市| 广东省| 大化| 双桥区| 瑞金市| 中牟县| 汉沽区| 两当县| 临海市| 呼玛县| 从化市| 阳山县| 抚远县| 凤城市| 盐池县| 社旗县| 谷城县| 青浦区| 大同县| 长葛市| 应用必备| 边坝县| 韩城市| 磴口县| 溧水县| 靖江市| 杭锦后旗| 周宁县|