您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java中常用時間的相關方法有哪些”,在日常操作中,相信很多人在Java中常用時間的相關方法有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java中常用時間的相關方法有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
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); }
獲取當前時間可以使用Date LocalDatetime Calendar Datetime
public static void main(String[] args) { //建議使用Calendar 可以設置年月日時分秒 Calendar calendar = Calendar.getInstance(); ////當月16 calendar.set(Calendar.DAY_OF_MONTH, 16); System.out.println(calendar.getTime()); //當月16 DateTime now = DateTime.now(); DateTime dateTime = now.withDayOfMonth(16); System.out.println(dateTime); //當月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(); //結束時間 Date endTime = now.plusHours(1).toDate(); //1小時 System.out.println("開始時間與結束時間的時間間隔:" + DateUtil.between(startTime, endTime, DateUnit.SECOND)); long time = (endTime.getTime() - startTime.getTime()) / 1000; System.out.println(time); }
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
到此,關于“Java中常用時間的相關方法有哪些”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。