詳解Java關(guān)于JDK中時(shí)間日期的API
JDK 8 之前日期和時(shí)間的API測試

//1.System類中的currentTimeMillis()
public void test1(){
long time = System.currentTimeMillis();
//返回當(dāng)前時(shí)間與1970年1月1日0時(shí)0分0秒之間以毫秒為時(shí)間為單位的時(shí)間差。
//稱為時(shí)間戳
System.out.println(time);//1628416744054
}

/*
java.util.Date類
|---java.sql.Date類(兩個(gè)是子父類的關(guān)系)
1.兩個(gè)構(gòu)造器的使用
>構(gòu)造器一:Date():創(chuàng)建一個(gè)對應(yīng)當(dāng)前時(shí)間的Date對象
>構(gòu)造器二:創(chuàng)建指定毫秒數(shù)的Date對象
2.兩個(gè)方法的使用
>toString():顯示當(dāng)前的年、月、日、時(shí)、分、秒
>getTime():獲取當(dāng)前Date對象對應(yīng)的毫秒數(shù)。(時(shí)間戳)
3.java.sql.Date對應(yīng)著數(shù)據(jù)庫中的日期類型的變量
>如何實(shí)例化
>如何將java.util.Date對象轉(zhuǎn)換為java.sql.Date對象
*/
@Test
public void test2(){
//構(gòu)造器一:Date():創(chuàng)建一個(gè)對應(yīng)當(dāng)前時(shí)間的Date對象
Date date1 = new Date();
System.out.println(date1.toString());//Sun Aug 08 18:07:27 CST 2021
//構(gòu)造器二:創(chuàng)建指定毫秒數(shù)的Date對象
Date date2 = new Date(1534798293927L);
System.out.println(date2.toString());//Tue Aug 21 04:51:33 CST 2018
//創(chuàng)建java.sql.Date對象
java.sql.Date date3 = new java.sql.Date(237493269533L);
System.out.println(date3);//1977-07-12
//如何將java.util.Date對象轉(zhuǎn)換為java.sql.Date對象
//情況一:(面向?qū)ο?
// Date date4 = new java.sql.Date(23682368236343L);
// java.sql.Date date5 = (java.sql.Date)date4;
//情況二:
Date date6 = new Date();
java.sql.Date date7 = new java.sql.Date(date6.getTime());
}

/*
SimpleDateFormat的使用:SimpleDateFormat對日期Date類的格式化和解析
1.兩個(gè)操作:
1.1 格式化:日期--->字符串
1.2 解析:格式化的逆過程:字符串--->日期
2.SimpleDateFormat的實(shí)例化
*/
@Test
public void testSimpleDateFormat() throws ParseException {
//實(shí)例化SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat();
//格式化:日期--->字符串
Date date = new Date();
System.out.println(date);
String format = sdf.format(date);
System.out.println(format);
//解析:格式化的逆過程,字符串--->日期
String str = "21-8-9 下午3:17";
Date date1 = sdf.parse(str);
System.out.println(date1);
//******************按照指定的方式格式化和解析:調(diào)用帶參的構(gòu)造器*****************************
// SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm ss");
//格式化
String format1 = sdf1.format(date);
System.out.println(format1);//2021-08-09 04:02 13
//解析:要求字符串必須是符合SimpleDateFormat識別的格式(通過構(gòu)造器參數(shù)體現(xiàn)),否則,報(bào)異常
Date date2 = sdf1.parse("2019-08-09 04:02 13");
System.out.println(date2);
}
/*
練習(xí)一:字符串"2020-09-08"轉(zhuǎn)換為java.sql.Date
*/
@Test
public void testExer() throws ParseException {
String brith = "2020-09-08";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyy-MM-dd");
Date date = sdf1.parse(brith);
java.sql.Date brithDate = new java.sql.Date(date.getTime());
System.out.println(brithDate);
}

/*
Calendar日歷類(抽象類)的使用
*/
@Test
public void testCalendar(){
//1.實(shí)例化
//方式一:創(chuàng)建其子類(GregorianCalendar)的對象
//方式二:調(diào)用其靜態(tài)方法getInstance()
Calendar calendar = Calendar.getInstance();
//2.常用方法
//get()
int days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
//set()
calendar.set(Calendar.DAY_OF_MONTH,22);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//add()
calendar.add(Calendar.DAY_OF_MONTH,-3);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//getTime():日歷類 -->Date
Date date1 = calendar.getTime();
calendar.setTime(date1);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
}
JDK 8中關(guān)于日期和時(shí)間的API測試
LocalDate 、 LocalTime 、 LocalDateTime 類是其中較重要的幾個(gè)類,它們的實(shí)例是不可變的對象,分別表示使用 ISO —8601日歷系統(tǒng)的日期、時(shí)間、日期和時(shí)間。它們提供了簡單的本地日期或時(shí)間,并不包含當(dāng)前的時(shí)間信息,也不包含與時(shí)區(qū)相關(guān)的信息。
> LocalDate 代表 IOS 格式( yyyy - MM - dd )的日期,可以存儲生日、紀(jì)念日等日期。> LocalTime 表示一個(gè)時(shí)間,而不是日期。
> LocalDateTime 是用來表示日期和時(shí)間的,這是一個(gè)最常用的類之一。
注: ISO —8601日歷系統(tǒng)是圍際標(biāo)準(zhǔn)化組織制定的現(xiàn)代公民的門期和時(shí)間的表示法,也就是公歷。
/*
LocalDate,LocalTime,LocalDateTime的使用
*/
public void test1(){
//now():獲取當(dāng)前的日期,時(shí)間,日期+時(shí)間
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);//2021-08-09
System.out.println(localTime);//21:19:25.264
System.out.println(localDateTime);//2021-08-09T21:19:25.264
//of():設(shè)置指定的年,月,日,時(shí),分,秒.沒有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2020,10,6,13,23,43);
System.out.println(localDateTime1);
//getXxx():獲取相關(guān)的屬性
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getMonth());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getMinute());
//體現(xiàn)不可變性
//withXxx():設(shè)置相關(guān)的屬性
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);
System.out.println(localDate1);
LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.println(localDate);
System.out.println(localDateTime2);
}

/*
Instant的使用
類似于java.util.Date類
*/
@Test
public void test2(){
//now():獲取本初子午線對應(yīng)的標(biāo)準(zhǔn)時(shí)間
Instant instant = Instant.now();
System.out.println(instant);//2021-08-09T15:08:46.818Z
//添加時(shí)間的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2021-08-09T23:08:46.818+08:00
//toEpochMilli():獲取自1970年1月1日0時(shí)0分0秒(UTC)開始的毫秒數(shù)-->Date類的getTime()
long milli = instant.toEpochMilli();
System.out.println(milli);//1628521726818
//ofEpochMilli():通過給定的毫秒數(shù),獲取Instant實(shí)例-->Date(long millis)
Instant instant1 = Instant.ofEpochMilli(12243455253L);
System.out.println(instant1);//1970-05-22T16:57:35.253Z
}
/*
DateTimeFormatter:格式化或解析日期\時(shí)間
類似于SimpleDateFormat
*/
@Test
public void test3(){
// 方式一:預(yù)定義的標(biāo)準(zhǔn)格式,如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//格式化:日期-->字符串
LocalDateTime localDateTime = LocalDateTime.now();
String str1 = formatter.format(localDateTime);
System.out.println(localDateTime);//2021-08-09T23:43:52.820
System.out.println(str1);//2021-08-09T23:43:52.82
//解析:字符串-->日期
// TemporalAccessor parse = formatter.parse(" 2021-08-09T23:43:52.82");
// System.out.println(parse);
// 方式二:本地化相關(guān)的格式,如:ofLocalizedDateTime
// FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT:適用于LocalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
//格式化:
String str2 = formatter1.format(localDateTime);
System.out.println(str2);//2021年8月10日 上午10時(shí)32分48秒
// 本地化相關(guān)的格式,如:ofLocalizedDate()
// FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT:適用于LocalDate
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
//格式化
String str3 = formatter2.format(LocalDate.now());
System.out.println(str3);//2021-8-10
// 重點(diǎn)?。。。。。。?方式三:自定義的格式.如:ofPattern("yyyy-MM-dd hh:mm:ss")
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str4 = formatter3.format(LocalDateTime.now());
System.out.println(str4);//2021-08-10 10:53:40
//解析
TemporalAccessor accessor = formatter3.parse("2021-08-10 10:53:40");
System.out.println(accessor);//{MinuteOfHour=53, NanoOfSecond=0, MicroOfSecond=0, HourOfAmPm=10, SecondOfMinute=40, MilliOfSecond=0},ISO resolved to 2021-08-10
}
到此這篇關(guān)于詳解Java關(guān)于JDK中時(shí)間日期的API的文章就介紹到這了,更多相關(guān)Java API內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA實(shí)現(xiàn)讀取txt文件內(nèi)容的方法
本篇文章主要介紹了JAVA實(shí)現(xiàn)讀取txt文件內(nèi)容的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
IntelliJ IDEA中使用mybatis-generator的示例
這篇文章主要介紹了IntelliJ IDEA中使用mybatis-generator,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
利用Java寫一個(gè)學(xué)生管理系統(tǒng)
今天這篇文章就給給大家分享利用Java寫一個(gè)學(xué)生管理系統(tǒng)吧,先寫一個(gè)簡單的用List來實(shí)現(xiàn)學(xué)生管理系統(tǒng):2021-09-09
java通過MySQL驅(qū)動攔截器實(shí)現(xiàn)執(zhí)行sql耗時(shí)計(jì)算
本文主要介紹了java通過MySQL驅(qū)動攔截器實(shí)現(xiàn)執(zhí)行sql耗時(shí)計(jì)算,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
JDK 5 提供的注解:Target、Inherited和Documented的區(qū)別
這篇文章主要介紹了JDK 5 提供的注解:Target、Inherited和Documented的區(qū)別,需要的朋友可以參考下2016-02-02
Java 根據(jù)url下載網(wǎng)絡(luò)資源
這篇文章主要介紹了Java 根據(jù)url下載網(wǎng)絡(luò)資源的示例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-11-11

