Java核心教程之常見時(shí)間日期的處理方法
時(shí)間的基礎(chǔ)知識(shí)
時(shí)區(qū):整個(gè)地球分為二十四時(shí)區(qū),每個(gè)時(shí)區(qū)都有自己的本地時(shí)間。 為了統(tǒng)一起見,使用一個(gè)統(tǒng)一的時(shí)間,稱為全球標(biāo)準(zhǔn)時(shí)間(UTC,Universal Time Coordinated)。 TC與格林尼治平均時(shí)(GMT,Greenwich Mean Time,也翻譯成:格林威治標(biāo)準(zhǔn)時(shí)間)差不多一樣 CST(北京時(shí)間),北京時(shí)間,China standard Time,中國標(biāo)準(zhǔn)時(shí)間。在時(shí)區(qū)劃分上,屬東八區(qū),比協(xié)調(diào)世界時(shí)早8小時(shí),記為UTC+8。 時(shí)間戳:自1970年1月1日(08:00:00GMT)至當(dāng)前時(shí)間的總秒數(shù),它也被稱為Unix時(shí)間戳(unix Timestamp),廣泛的運(yùn)用在知識(shí)產(chǎn)權(quán)保護(hù)、合同簽字、金融帳務(wù)、電子報(bào)價(jià)投標(biāo)、股票交易等方面 格式多種:2050-10-3110:11:11、2050/10/3110/10:10年、月、日、周幾等背景:程序代碼中怎么表示時(shí)間呢?我需要獲取當(dāng)前時(shí)間怎么辦
ava.util包提供了Date類來封裝當(dāng)前的日期和時(shí)間
構(gòu)造函數(shù)
//當(dāng)前時(shí)間Date()//從1970年1月1日起的毫秒數(shù)作為參數(shù)Date(long millisec)
常見方法
//返回自1970年1月1日00:00:00GMT以來此Date對象表示的毫秒數(shù)。long getTime()//調(diào)用此方法的Date對象在指定日期之后返回true,否則返回false。boolean after(Date date)//調(diào)用此方法的Date對象在指定日期之前返回true,否則返回false。boolean before(Date date)新版JDK8之時(shí)間日期處理類
Java8通過發(fā)布新的Date-Time API(JSR310)來進(jìn)一步加強(qiáng)對日期與時(shí)間的處理
新增了很多常見的api,如日期/時(shí)間的比較,加減,格式化等
包所在位置 java.time
核心類
LocalDate:不包含具體時(shí)間的日期。LocalTime:不含日期的時(shí)間。LocalDateTime:包含了日期及時(shí)間。
LocalDate常用API
LocalDate today=LocalDate.now(); system.out.print1n('今天日期:'+today); //獲取年,月,日,周幾system.out.print1n('現(xiàn)在是哪年:'+today.getYear());system.out.print1n('現(xiàn)在是哪月:'+today.getMonth());System.out.print1n('現(xiàn)在是哪月(數(shù)字):'+today.getMonthValue());System.out.print1n('現(xiàn)在是幾號:'+today.getDayofMonth());system.out.print1n('現(xiàn)在是周幾:'+today.getDayofweek());//加減年份,加后返回的對象才是修改后的,舊的依舊是舊的LocalDate changeDate=today.plusYears(1);system.out.print1n('加后是哪年:'+changeDate.getYear());System.out.print1n('舊的是哪年:'+today.getYear());//日期比較system.out.print1n('isafter:'+changeDate.isAfter(today));//getYear()int 獲取當(dāng)前日期的年份//getMonth()Month獲取當(dāng)前日期的月份對象//getMonthValue()int 獲取當(dāng)前日期是第幾月//getDayofweek()Dayofweek 表示該對象表示的日期是星期幾//getDayofMonth()int 表示該對象表示的日期是這個(gè)月第幾天//getDayofyear()int 表示該對象表示的日期是今年第幾天//withyear(int year)LocalDate 修改當(dāng)前對象的年份//withMonth(int month)LocalDate修改當(dāng)前對象的月份//withpayofMonth(int dayofMonth)LocalDate 修改當(dāng)前對象在當(dāng)月的日期//plusYears(long yearsToAdd)Localpate 當(dāng)前對象增加指定的年份數(shù)//plusMonths(1ong monthsToAdd)LocalDate 當(dāng)前對象增加指定的月份數(shù)//plusweeks(1ong weeksToAdd)LocalDate 當(dāng)前對象增加指定的周數(shù)//plusDays(1ong daysToAdd)LocalDate 當(dāng)前對象增加指定的天數(shù)//minusYears(long yearsTosubtract)LocalDate 當(dāng)前對象減去指定的年數(shù)//minusMonths(1ong months ToSubtract)LocalDate當(dāng)前對象減去注定的月數(shù)//minusWeeks(long weeksTosubtract)LocalDate 當(dāng)前對象減去指定的周數(shù)//minusDays(1ong daysTosubtract)LocalDate當(dāng)前對象減去指定的天數(shù)//compareTo(ChronoLocalDate other)int 比較當(dāng)前對象和other對象在時(shí)間上的大小,返回值如果為正,則當(dāng)前對象時(shí)間較晚//isBefore(ChronoLocalDate other)boolean比較當(dāng)前對象日期是否在other對象日期之前//isAfter(ChronoLocalDate other)boolean 比較當(dāng)前對象日期是否在other對象日期之后//isEqual(ChronoLocalDate other)boolean 比較兩個(gè)日期對象是否相等新版JDK8之時(shí)間日期格式化
為什么要時(shí)間日期做格式化
程序打印,或者網(wǎng)頁顯示時(shí)間日期格式,用戶有不同的需求,則需要根據(jù)一定的規(guī)則進(jìn)行格式化常用的占位符
y四位數(shù)年份 M月d日 h時(shí)在 m分 S毫秒JDK8之后:引入線程安全的日期與時(shí)
LocalDateTime ldt = LocalDateTime.now();System.out.println(ldt);DateTimeFormatter dtf = DateTimeFormatter.ofPattern('yyyy-MM-ddHH:mm:ss');String ldtStr = dtf.format(ldt);System.out.println(ldtStr);
獲取指定的日期時(shí)間對象LocalDate Time ldt=LocalDate Time.of(2020,11,11,8,20,30);System.out.println(ldt);
計(jì)算日期時(shí)間差 java.time.Duration
LocalDateTime today = LocalDateTime.now();System.out.println(today);LocalDateTime changeDate = LocalDateTime.of(2020,10,1,10,40,30);System.out.println(changeDate);Duration duration = Duration.between( today,changeDate);//第⼆二個(gè)參數(shù)減第⼀一個(gè)參數(shù)System.out.println(duration.toDays());//兩個(gè)時(shí)間差的天數(shù)System.out.println(duration.toHours());//兩個(gè)時(shí)間差的小時(shí)數(shù)System.out.println(duration.toMinutes());//兩個(gè)時(shí)間差的分鐘數(shù)System.out.println(duration.toMillis());//兩個(gè)時(shí)間差的毫秒數(shù)System.out.println(duration.toNanos());//兩個(gè)時(shí)間差的納秒數(shù)總結(jié)
到此這篇關(guān)于Java核心教程之常見時(shí)間日期的文章就介紹到這了,更多相關(guān)Java常見時(shí)間日期內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐2. Django程序的優(yōu)化技巧3. XML入門的常見問題(一)4. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實(shí)現(xiàn)5. jsp EL表達(dá)式詳解6. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子7. chat.asp聊天程序的編寫方法8. Python多線程操作之互斥鎖、遞歸鎖、信號量、事件實(shí)例詳解9. idea設(shè)置自動(dòng)導(dǎo)入依賴的方法步驟10. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
