日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java常用类(2)--日期时间相关类Date、Calendar、LocalDateTime、Instant全面

發(fā)布時(shí)間:2023/12/2 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java常用类(2)--日期时间相关类Date、Calendar、LocalDateTime、Instant全面 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • java.lang.System類
    • java.util.Date類
    • java.sql.Date類
    • java.text.SimpleDateFormat類
    • java.util.Calendar(日歷)類
    • java.time類
    • java.time.Instant類
    • java.time.format.DateTimeFormatter 類
    • 其它API



java.lang.System類

System類提供的public static long currentTimeMillis()用來返回當(dāng)前時(shí)間與1970年1月1日0時(shí)0分0秒之間以毫秒為單位的時(shí)間差(時(shí)間戳)。(此方法適于計(jì)算時(shí)間差)

long time = System.currentTimeMillis(); System.out.println(time);

java.util.Date類

構(gòu)造器:
①Date():創(chuàng)建的對象可以獲取本地當(dāng)前時(shí)間
②Date(long date):創(chuàng)建指定毫秒數(shù)的Date對象,相當(dāng)于又將時(shí)間戳轉(zhuǎn)換成String形式

常用方法
①getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 對象表示的毫秒數(shù)。
②toString():把此 Date 對象轉(zhuǎn)換為以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中: dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat),zzz是時(shí)間標(biāo)準(zhǔn)。

import java.util.Date;public class DataTest {public static void main(String[] args) {Date date = new Date();System.out.println(date.toString());System.out.println(date); //等同于toString()方法System.out.println(date.getTime());} }

java.sql.Date類

是java.util.Date類的子類,對應(yīng)著數(shù)據(jù)庫中的日期類型的變量。

構(gòu)造器:java.sql.Date(時(shí)間戳)

常用方法:同上

java.sql.Date與java.util.Date轉(zhuǎn)換,可以通過多態(tài)和getTime()方法

import java.util.Date;public class DataTest {public static void main(String[] args) {Date date = new Date();java.sql.Date sqlDate = new java.sql.Date(date.getTime());System.out.println(sqlDate);} }

java.text.SimpleDateFormat類

Date類的API不易于國際化,大部分被廢棄了,java.text.SimpleDateFormat類是一個(gè)不與語言環(huán)境有關(guān)的方式來格式化和解析日期的具體類。其允許進(jìn)行①格式化:日期—>字符串、②解析:字符串—>日期。

格式化:
1.實(shí)例化
①SimpleDateFormat() :默認(rèn)的模式和語言環(huán)境創(chuàng)建對象
②SimpleDateFormat(String pattern):該構(gòu)造方法可以用參數(shù)pattern指定的格式創(chuàng)建一個(gè)對象
2.格式化
實(shí)例化對象調(diào)用 String format(Date date)方法格式化時(shí)間對象

import java.text.SimpleDateFormat; import java.util.Date;public class SimpleTest {public static void main(String[] args) {SimpleDateFormat sdf = new SimpleDateFormat(); //無參默認(rèn)模式實(shí)例化Date date = new Date(); String format = sdf.format(date); //格式化System.out.println(format);} }

import java.text.SimpleDateFormat; import java.util.Date;public class SimpleTest {public static void main(String[] args) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //帶參指定格式實(shí)例化Date date = new Date();String format = sdf.format(date); //格式化System.out.println(format);} }


關(guān)于指定格式:


解析(格式化逆過程):
Date parse(String source):依據(jù)格式解析字符串,以生成一個(gè)日期(注意格式的匹配對應(yīng),否則拋異常)

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;public class SimpleTest {public static void main(String[] args) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //帶參指定格式實(shí)例化Date parse = sdf.parse("2020-12-20 22:03:19");System.out.println(parse);} }



java.util.Calendar(日歷)類

這是一個(gè)抽象(abstract)類,主用用于完成日期字段之間相互操作的功能。

實(shí)例化:
①使用Calendar.getInstance()該靜態(tài)方法
②調(diào)用它的子類GregorianCalendar的構(gòu)造器

常用方法:

import java.util.Calendar; import java.util.Date;public class CalendarTest {public static void main(String[] args) {Calendar instance = Calendar.getInstance();//get():int get(int field)int ofMouth = instance.get(Calendar.DAY_OF_MONTH);System.out.println(ofMouth);int ofYear = instance.get(Calendar.DAY_OF_YEAR);System.out.println(ofYear);//set():void set(int field,int value)instance.set(Calendar.DAY_OF_MONTH,9);System.out.println(instance.get(Calendar.DAY_OF_MONTH));//add:void add(int field,int amount)instance.add(Calendar.DAY_OF_MONTH,-2);System.out.println(instance.get(Calendar.DAY_OF_MONTH));//getTime:final Date getTime() 將日歷類型轉(zhuǎn)換為Date類型Date time = instance.getTime();System.out.println(time);//setTime:final void setTime(Date date) 將Date類型轉(zhuǎn)換為日歷類型instance.setTime(new Date());System.out.println("********" + instance.getTime());} }


一個(gè)Calendar的實(shí)例是系統(tǒng)時(shí)間的抽象表示,通過get(int field)方法來取得想要的時(shí)間信息。比如YEAR、MONTH、DAY_OF_WEEK、HOUR_OF_DAY 、MINUTE、SECOND。



java.time類

LocalDate、LocalTime、LocalDateTime 類是其中較重要的幾個(gè)子類,它們的實(shí)例
是不可變的對象,分別表示使用 ISO-8601日歷系統(tǒng)的日期、時(shí)間、日期和時(shí)間。

實(shí)例化:

import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime;public class TimeTest {public static void main(String[] args) {//now() 獲取當(dāng)前日期,時(shí)間,日期+時(shí)間LocalDate now1 = LocalDate.now();LocalTime now2 = LocalTime.now();LocalDateTime now3 = LocalDateTime.now();System.out.println(now1);System.out.println(now2);System.out.println(now3);//of() 設(shè)置指定的年月日時(shí)分秒,沒有偏移量LocalDateTime of = LocalDateTime.of(2021, 9, 9, 15, 36, 40);System.out.println(of);} }

其他常用方法:

import java.time.LocalDateTime;public class TimeTest {public static void main(String[] args) {LocalDateTime now = LocalDateTime.now();//getXxx() 獲取相關(guān)的屬性System.out.println(now.getDayOfWeek());System.out.println(now.getMonth());System.out.println(now.getMonthValue()); //......//withXxx() 設(shè)置相關(guān)的屬性,不可變性LocalDateTime nowWith = now.withDayOfMonth(8);System.out.println(nowWith);System.out.println(now);//plusXxx() 加,不可變性LocalDateTime pl = now.plusMonths(2);System.out.println(pl);//minusXxx() 減,不可變性LocalDateTime mi = now.minusDays(8);System.out.println(mi);} }



java.time.Instant類

Instant:時(shí)間線上的一個(gè)瞬時(shí)點(diǎn)。 這可能被用來記錄應(yīng)用程序中的事件時(shí)間
戳。

import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneOffset;public class InstantTest {public static void main(String[] args) {//獲取本初子午線當(dāng)前的標(biāo)準(zhǔn)時(shí)間Instant now = Instant.now();System.out.println(now);//結(jié)合時(shí)區(qū),添加當(dāng)?shù)貢r(shí)間偏移量OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));System.out.println(offsetDateTime);//返回1970-01-01 00:00:00到當(dāng)前時(shí)間的毫秒數(shù),即為時(shí)間戳long l = now.toEpochMilli();System.out.println(l);//返回在1970-01-01 00:00:00基礎(chǔ)上加上指定毫秒數(shù)之后的Instant類的對象Instant instant = Instant.ofEpochMilli(l);System.out.println(instant);} }



java.time.format.DateTimeFormatter 類

該類提供了三種格式化方法:
①預(yù)定義的標(biāo)準(zhǔn)格式,如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
②本地化相關(guān)的格式,如:ofLocalizedDateTime(FormatStyle.LONG)
③自定義的格式,如:ofPattern(“yyyy-MM-dd hh:mm:ss”)

import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.time.temporal.TemporalAccessor;public class FormatterTest {public static void main(String[] args) {//方式一:預(yù)定義的標(biāo)準(zhǔn)格式DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;//格式化String format1 = isoDateTime.format(LocalDateTime.now());System.out.println(format1);//解析TemporalAccessor parse1 = isoDateTime.parse(format1);System.out.println(parse1);//方式二:本地化相關(guān)的格式DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);//格式化String format2 = dateTimeFormatter.format(LocalDateTime.now());System.out.println(format2);//解析TemporalAccessor parse2 = dateTimeFormatter.parse(format2);System.out.println(parse2);//方式三:自定義格式DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");//格式化String format3 = dateTimeFormatter3.format(LocalDateTime.now());System.out.println(format3);//解析TemporalAccessor parse3 = dateTimeFormatter3.parse(format3);System.out.println(parse3);} }



其它API

ZoneId:該類中包含了所有的時(shí)區(qū)信息,一個(gè)時(shí)區(qū)的ID,如 Europe/Paris
ZonedDateTime:一個(gè)在ISO-8601日歷系統(tǒng)時(shí)區(qū)的日期時(shí)間,
如 2007-12-03T10:15:30+01:00 Europe/Paris
其中每個(gè)時(shí)區(qū)都對應(yīng)著ID,地區(qū)ID都為“{區(qū)域}/{城市}”的格式,例如:
Asia/Shanghai等

Clock:使用時(shí)區(qū)提供對當(dāng)前即時(shí)、日期和時(shí)間的訪問的時(shí)鐘
持續(xù)時(shí)間:Duration,用于計(jì)算兩個(gè)“時(shí)間”間隔
日期間隔:Period,用于計(jì)算兩個(gè)“日期”間隔

TemporalAdjuster : 時(shí)間校正器。有時(shí)我們可能需要獲取例如:將日期調(diào)整到“下一個(gè)工作日”等操作。
TemporalAdjusters : 該類通過靜態(tài)方法(firstDayOfXxx()/lastDayOfXxx()/nextXxx())提供了大量的常用TemporalAdjuster 的實(shí)現(xiàn)


總結(jié)

以上是生活随笔為你收集整理的Java常用类(2)--日期时间相关类Date、Calendar、LocalDateTime、Instant全面的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。