App下載

Java8新特性之日期時(shí)間API的介紹和具體使用

猿友 2021-08-05 15:25:52 瀏覽數(shù) (1684)
反饋

jdk8之前

 一、java.lang.System

2021041610182737

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

在這里插入圖片描述

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

2021041610182839

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

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

三、java.text.SimpleDateFormat

SimpleDateFormat是對(duì)日期Date類的格式化和解析。

2021041610182840

兩個(gè)操作:

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);

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

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

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

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

2021041610182944

常用實(shí)例化方法:

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

常用方法:

1.get():獲取常用的屬性和信息。
2.set():設(shè)置:相當(dāng)于把本身的日期給改變了
3.add():添加(增加時(shí)間、天數(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)前日期是這個(gè)月的第幾天
		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

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

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

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

2021041610182946
2021041610183047

一、常用類

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

2021041610183048

二、一些方法

在這里插入圖片描述

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

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è)置指定時(shí)間的年、月、日、時(shí)、分。沒有偏移量。(實(shí)例化方法二)

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

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

三、Instant

2021041610183151

2021041610183252

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

//now():獲取本初子午線對(duì)應(yīng)的標(biāo)準(zhǔn)時(shí)間
		Instant instant = Instant.now();
		System.out.println(instant);
		//添加時(shí)間的偏移量
		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í)例 (實(shí)例化方法二)

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

四、DateTimeFormatter類

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

2021041610183253

(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);

在這里插入圖片描述

2021041610183356

到此這篇關(guān)于Java8版本提供的日期時(shí)間API的新特性的文章就介紹到這了,想要了解更多相關(guān)Java日期時(shí)間API內(nèi)容或者Java8其他新特性的內(nèi)容,請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持!


0 人點(diǎn)贊