Java中常用時間的一些相關(guān)方法
前言
在我們java開發(fā)中,Date日期這個字段會被經(jīng)常使用,比如獲取當(dāng)前系統(tǒng)的時間,獲取上個月,上一年的時間,以及獲取兩個日期相差的時分秒數(shù),或者對日期類型進行格式化,等等,等等,下面將給大家詳細(xì)介紹下Java中常用時間的一些相關(guān)方法
一、獲取當(dāng)前時間的方式
public static void main(String[] args) { //Date Date now = new Date(); System.out.println(now); //java8的時間 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime); Calendar calendar = Calendar.getInstance(); Date time = calendar.getTime(); System.out.println(time); System.out.println("年" + calendar.get(Calendar.YEAR)); System.out.println("月" + (calendar.get(Calendar.MONTH) + 1)); //joda time DateTime dateTime = DateTime.now(); System.out.println(dateTime); }
獲取當(dāng)前時間可以使用Date LocalDatetime Calendar Datetime
二、獲取當(dāng)月第n天
public static void main(String[] args) { //建議使用Calendar 可以設(shè)置年月日時分秒 Calendar calendar = Calendar.getInstance(); ////當(dāng)月16 calendar.set(Calendar.DAY_OF_MONTH, 16); System.out.println(calendar.getTime()); //當(dāng)月16 DateTime now = DateTime.now(); DateTime dateTime = now.withDayOfMonth(16); System.out.println(dateTime); //當(dāng)月14 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime.withDayOfMonth(14)); //1月11 System.out.println(localDateTime.withMonth(1).withDayOfMonth(11)); }
三、格式化為字符串
``` //使用SimpleDateFormat SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(format.format(new Date())); //使用Calendar Calendar calendar = Calendar.getInstance(); System.out.println(String.format("%s年%s月%s日%s時%s分%s秒", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND))); LocalDateTime now = LocalDateTime.now(); String str = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); System.out.println(str); ```
四、加減時間(單位可以是秒,小時等)
public static void main(String[] args) { Date now = new Date(); //加一小時 long time = now.getTime() + (60 * 60 * 1000); System.out.println(new Date(time)); /* <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.14</version> </dependency> */ //引入Hutool 加一小時 System.out.println(DateUtil.offset(now, DateField.HOUR, 1)); //減一小時 System.out.println(DateUtil.offset(now, DateField.HOUR, -1)); LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("加一小時" + localDateTime.plusHours(1)); System.out.println("減一小時" + localDateTime.minusHours(1)); DateTime dateTime = DateTime.now(); System.out.println(dateTime.plusHours(1)); System.out.println(dateTime.minusHours(1)); }
LocalDateTime和DateTime都自帶增加和減少時間的方法
五、通過出生日期獲取年齡
public static void main(String[] args) { //時間1990-12-05 DateTime birthDay = DateTime.now().withYear(1990).withMonthOfYear(10).withDayOfMonth(23); System.out.println(birthDay); //獲取相差得年 會進行月份和日期比較 如 Years years = Years.yearsBetween(birthDay, new DateTime()); System.out.println(years); System.out.println(years.getYears()); }
還可以使用年份相減,再比較月,日的方法得到生日
六、判斷兩個時間段是否覆蓋
public static void main(String[] args) { DateTime now = DateTime.now(); DateTime start1 = now; DateTime end1 = now.plusMinutes(1); DateTime start2 = now.plusSeconds(50); DateTime end2 = now.plusMinutes(2); Interval interval1 = new Interval(start1, end1); Interval interval2 = new Interval(start2, end2); System.out.println(interval1.overlaps(interval2)); System.out.println(start1.getMillis() < end2.getMillis() && start2.getMillis() < end1.getMillis()); }
七、求兩個時間間隔
public static void main(String[] args) { DateTime now = DateTime.now(); //開始時間 Date startTime = now.toDate(); //結(jié)束時間 Date endTime = now.plusHours(1).toDate(); //1小時 System.out.println("開始時間與結(jié)束時間的時間間隔:" + DateUtil.between(startTime, endTime, DateUnit.SECOND)); long time = (endTime.getTime() - startTime.getTime()) / 1000; System.out.println(time); }
八、UTC時間與北京時間轉(zhuǎn)換
public static void main(String[] args) throws ParseException { Date now = new Date(); Date utcDate = bj2UTC(now); //utc時間 System.out.println(utcDate); //北京時間 System.out.println(utc2BJ(utcDate)); DateTime dateTime = DateTime.now().withDayOfMonth(1).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0); System.out.println(dateTime); System.out.println(bj2UTC(dateTime.toDate())); } public static Date bj2UTC(Date date) { if (date == null) { return null; } LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("-8")); return Date.from(localDateTime.atOffset(ZoneOffset.UTC).toInstant()); } public static Date utc2BJ(Date date) { if (date == null) { return null; } LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("+8")); return Date.from(localDateTime.atOffset(ZoneOffset.UTC).toInstant()); }
北京時間=UTC+8
總結(jié)
到此這篇關(guān)于Java中常用時間的一些相關(guān)方法的文章就介紹到這了,更多相關(guān)Java常用時間方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項
這篇文章主要介紹了基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Springboot集成SSE實現(xiàn)單工通信消息推送流程詳解
SSE簡單的來說就是服務(wù)器主動向前端推送數(shù)據(jù)的一種技術(shù),它是單向的,也就是說前端是不能向服務(wù)器發(fā)送數(shù)據(jù)的。SSE適用于消息推送,監(jiān)控等只需要服務(wù)器推送數(shù)據(jù)的場景中,下面是使用Spring Boot來實現(xiàn)一個簡單的模擬向前端推動進度數(shù)據(jù),前端頁面接受后展示進度條2022-11-11SpringMVC @RequestBody Date類型的Json轉(zhuǎn)換方式
這篇文章主要介紹了SpringMVC @RequestBody Date類型的Json轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Java詳細(xì)講解不同版本的接口語法和抽象類與接口的區(qū)別
對于面向?qū)ο缶幊虂碚f,抽象是它的一大特征之一,在?Java?中可以通過兩種形式來體現(xiàn)OOP的抽象:接口和抽象類,下面這篇文章主要給大家介紹了關(guān)于Java入門基礎(chǔ)之抽象類與接口的相關(guān)資料,需要的朋友可以參考下2022-04-04Java生成10個1000以內(nèi)的隨機數(shù)并用消息框顯示數(shù)組內(nèi)容然后求和輸出
這篇文章主要介紹了Java生成10個1000以內(nèi)的隨機數(shù)并用消息框顯示數(shù)組內(nèi)容然后求和輸出,需要的朋友可以參考下2015-10-10