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

JDK8時(shí)間相關(guān)類超詳細(xì)總結(jié)(含多個(gè)實(shí)例)

 更新時(shí)間:2023年01月19日 10:59:49   作者:Alita11101_  
jdk1.8的一些新特性簡(jiǎn)化了代碼的寫法,減少了部分開發(fā)量,下面這篇文章主要給大家介紹了關(guān)于JDK8時(shí)間相關(guān)類超詳細(xì)總結(jié),文中包含了多個(gè)實(shí)例代碼,需要的朋友可以參考下

一、帶時(shí)區(qū)的時(shí)間

1.獲取當(dāng)前時(shí)間對(duì)象(帶時(shí)區(qū))

import java.time.ZonedDateTime;
public class demo1 {
    public static void main(String[] args) {
        
	    ZonedDateTime now = ZonedDateTime.now();
        System.out.println(now);
      
    }
}

2023-01-13T19:24:18.389+08:00[Asia/Shanghai]

2.獲取指定的時(shí)間對(duì)象(帶時(shí)區(qū))1/年月日時(shí)分秒納秒方式指定

import java.time.Instant;
public class demo1 {
    public static void main(String[] args) {
        
		ZonedDateTime time1 = ZonedDateTime.of(2023, 1, 1, 8, 30, 0, 0, ZoneId.of("Asia/Shanghai"));
        System.out.println(time1);
      
    }
}

2023-01-01T08:30+08:00[Asia/Shanghai]

3.通過Instant + 時(shí)區(qū)的方式指定獲取時(shí)間對(duì)象

import java.time.Instant;
public class demo1 {
    public static void main(String[] args) {
        Instant instant = Instant.ofEpochMilli(0L);
		ZoneId zoneId = ZoneId.of("Asia/Shanghai");
		ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);
		System.out.println(time2);
      
    }
}

1970-01-01T08:00+08:00[Asia/Shanghai]

4.修改時(shí)間

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Demo8 {
    public static void main(String[] args) {
        Instant instant = Instant.ofEpochMilli(0L);
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);
        ZonedDateTime time3 = time2.withYear(2000);
        System.out.println(time3);

        ZonedDateTime time4 = time3.minusYears(1);
        System.out.println(time4);

        ZonedDateTime time5 = time4.plusYears(1);
        System.out.println(time5);
    }
}

2000-01-01T08:00+08:00[Asia/Shanghai]
1999-01-01T08:00+08:00[Asia/Shanghai]
2000-01-01T08:00+08:00[Asia/Shanghai]

二、DateTimeFormatter

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
//獲取時(shí)間對(duì)象
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));

// 解析/格式化器
DateTimeFormatter dtf1=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm;ss EE a");
// 格式化
System.out.println(dtf1.format(time));

2023-01-14 23:55;55 星期六 下午

三、LocalDate

1. 獲取當(dāng)前時(shí)間的日歷對(duì)象(包含年月日)

//1.獲取當(dāng)前時(shí)間的日歷對(duì)象(包含 年月日)
LocalDate nowDate = LocalDate.now();
//System.out.println("今天的日期:" + nowDate);

2.獲取指定的時(shí)間的日歷對(duì)象

LocalDate ldDate = LocalDate.of(2023, 1, 1);
System.out.println("指定日期:" + ldDate);

System.out.println("=============================");

//3.get系列方法獲取日歷中的每一個(gè)屬性值//獲取年
int year = ldDate.getYear();
System.out.println("year: " + year);
//獲取月//方式一:
Month m = ldDate.getMonth();
System.out.println(m);
System.out.println(m.getValue());

//方式二:
int month = ldDate.getMonthValue();
System.out.println("month: " + month);


//獲取日
int day = ldDate.getDayOfMonth();
System.out.println("day:" + day);

//獲取一年的第幾天
int dayofYear = ldDate.getDayOfYear();
System.out.println("dayOfYear:" + dayofYear);

//獲取星期
DayOfWeek dayOfWeek = ldDate.getDayOfWeek();
System.out.println(dayOfWeek);
System.out.println(dayOfWeek.getValue());

//is開頭的方法表示判斷
System.out.println(ldDate.isBefore(ldDate));
System.out.println(ldDate.isAfter(ldDate));

//with開頭的方法表示修改,只能修改年月日
LocalDate withLocalDate = ldDate.withYear(2000);
System.out.println(withLocalDate);

//minus開頭的方法表示減少,只能減少年月日
LocalDate minusLocalDate = ldDate.minusYears(1);
System.out.println(minusLocalDate);

//plus開頭的方法表示增加,只能增加年月日
LocalDate plusLocalDate = ldDate.plusDays(1);
System.out.println(plusLocalDate);

四、LocalTime

1.獲取本地時(shí)間的日歷對(duì)象(包含時(shí)分秒)

LocalTime nowTime = LocalTime.now();
System.out.println("今天的時(shí)間:" + nowTime);

int hour = nowTime.getHour();//時(shí)
System.out.println("hour: " + hour);

int minute = nowTime.getMinute();//分
System.out.println("minute: " + minute);

int second = nowTime.getSecond();//秒
System.out.println("second:" + second);

int nano = nowTime.getNano();//納秒
System.out.println("nano:" + nano);
System.out.println("------------------------------------");
System.out.println(LocalTime.of(8, 20));//時(shí)分
System.out.println(LocalTime.of(8, 20, 30));//時(shí)分秒
System.out.println(LocalTime.of(8, 20, 30, 150));//時(shí)分秒納秒
LocalTime mTime = LocalTime.of(8, 20, 30, 150);

2.is系列的方法

System.out.println(nowTime.isBefore(mTime));
System.out.println(nowTime.isAfter(mTime));

3.with系列的方法

這個(gè)系列的方法有局限性,只能修改時(shí)、分、秒

System.out.println(nowTime.withHour(10));

4.plus系列的方法

這個(gè)系列的方法有局限性,只能修改時(shí)、分、秒

System.out.println(nowTime.plusHours(10));

五、LocalDateTime

1.當(dāng)前時(shí)間的的日歷對(duì)象(包含年月日時(shí)分秒)

LocalDateTime nowDateTime = LocalDateTime.now();

System.out.println("今天是:" + nowDateTime);//今天是:
System.out.println(nowDateTime.getYear());//年
System.out.println(nowDateTime.getMonthValue());//月
System.out.println(nowDateTime.getDayOfMonth());//日
System.out.println(nowDateTime.getHour());//時(shí)
System.out.println(nowDateTime.getMinute());//分
System.out.println(nowDateTime.getSecond());//秒
System.out.println(nowDateTime.getNano());//納秒

2.獲取日:當(dāng)年的第幾天

System.out.println("dayofYear:" + nowDateTime.getDayOfYear());

3.獲取星期

System.out.println(nowDateTime.getDayOfWeek());
System.out.println(nowDateTime.getDayOfWeek().getValue());

4.獲取月份

System.out.println(nowDateTime.getMonth());
System.out.println(nowDateTime.getMonth().getValue());

LocalDate ld = nowDateTime.toLocalDate();
System.out.println(ld);

LocalTime lt = nowDateTime.toLocalTime();
System.out.println(lt.getHour());
System.out.println(lt.getMinute());
System.out.println(lt.getSecond());

六、結(jié)語

到此這篇關(guān)于JDK8時(shí)間相關(guān)類超詳細(xì)總結(jié)的文章就介紹到這了,更多相關(guān)JDK8時(shí)間相關(guān)類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java數(shù)據(jù)類型轉(zhuǎn)換的示例詳解

    Java數(shù)據(jù)類型轉(zhuǎn)換的示例詳解

    Java程序中要求參與的計(jì)算的數(shù)據(jù),必須要保證數(shù)據(jù)類型的一致性,如果數(shù)據(jù)類型不一致將發(fā)生類型的轉(zhuǎn)換。本文將通過示例詳細(xì)說說Java中數(shù)據(jù)類型的轉(zhuǎn)換,感興趣的可以了解一下
    2022-10-10
  • 深入解析Spring?Boot?的SPI機(jī)制詳情

    深入解析Spring?Boot?的SPI機(jī)制詳情

    這篇文章主要介紹了深入解析Spring?Boot的SPI機(jī)制詳情,SPI是JDK內(nèi)置的一種服務(wù)提供發(fā)現(xiàn)機(jī)制,可以用來啟用框架擴(kuò)展和替換組件,主要用于框架中開發(fā),更多相關(guān)介紹,感興趣的小伙伴可以參考一下下面文章內(nèi)容
    2022-08-08
  • java正則表達(dá)式表單驗(yàn)證類工具類(驗(yàn)證郵箱、手機(jī)號(hào)碼、qq號(hào)碼等)

    java正則表達(dá)式表單驗(yàn)證類工具類(驗(yàn)證郵箱、手機(jī)號(hào)碼、qq號(hào)碼等)

    這篇文章主要介紹了java使用正則表達(dá)式進(jìn)行表單驗(yàn)證工具類,可以驗(yàn)證郵箱、手機(jī)號(hào)碼、qq號(hào)碼等方法,需要的朋友可以參考下
    2014-04-04
  • 基于Spring框架由ConditionalOnMissingBean注解引發(fā)的問題

    基于Spring框架由ConditionalOnMissingBean注解引發(fā)的問題

    這篇文章主要介紹了基于Spring框架由ConditionalOnMissingBean注解引發(fā)的問題,具有很好
    2023-11-11
  • JAVA?Springboot配置i18n國(guó)際化語言詳細(xì)步驟

    JAVA?Springboot配置i18n國(guó)際化語言詳細(xì)步驟

    國(guó)際化(Internationalization,縮寫為i18n)是指根據(jù)來展示不同的內(nèi)容,使應(yīng)用程序能夠適應(yīng)不同的語言和文化習(xí)慣,下面這篇文章主要給大家介紹了關(guān)于JAVA?Springboot配置i18n國(guó)際化語言的詳細(xì)步驟,需要的朋友可以參考下
    2024-08-08
  • Spring+Quartz配置定時(shí)任務(wù)實(shí)現(xiàn)代碼

    Spring+Quartz配置定時(shí)任務(wù)實(shí)現(xiàn)代碼

    這篇文章主要介紹了Spring+Quartz配置定時(shí)任務(wù)實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Spring Security OAuth2實(shí)現(xiàn)使用JWT的示例代碼

    Spring Security OAuth2實(shí)現(xiàn)使用JWT的示例代碼

    這篇文章主要介紹了Spring Security OAuth2實(shí)現(xiàn)使用JWT的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • 基于Maven的pom.xml文件詳解

    基于Maven的pom.xml文件詳解

    下面小編就為大家?guī)硪黄贛aven的pom.xml文件詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • 最新評(píng)論

    青川县| 广饶县| 峨眉山市| 台安县| 成安县| 容城县| 浮山县| 观塘区| 芜湖市| 日照市| 扎兰屯市| 高阳县| 攀枝花市| 开封市| 肥乡县| 云浮市| 临邑县| 台东县| 攀枝花市| 钟祥市| 荃湾区| 廉江市| 保亭| 兴业县| 苍南县| 米林县| 舒兰市| 阳新县| 巧家县| 襄汾县| 洮南市| 宣城市| 仲巴县| 屯门区| 科尔| 北京市| 阿尔山市| 盱眙县| 永安市| 德格县| 上林县|