您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java中常用時(shí)間的相關(guān)方法有哪些”,在日常操作中,相信很多人在Java中常用時(shí)間的相關(guān)方法有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java中常用時(shí)間的相關(guān)方法有哪些”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
public static void main(String[] args) { //Date Date now = new Date(); System.out.println(now); //java8的時(shí)間 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)前時(shí)間可以使用Date LocalDatetime Calendar Datetime
public static void main(String[] args) { //建議使用Calendar 可以設(shè)置年月日時(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時(shí)%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(); //加一小時(shí) 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 加一小時(shí) System.out.println(DateUtil.offset(now, DateField.HOUR, 1)); //減一小時(shí) System.out.println(DateUtil.offset(now, DateField.HOUR, -1)); LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("加一小時(shí)" + localDateTime.plusHours(1)); System.out.println("減一小時(shí)" + localDateTime.minusHours(1)); DateTime dateTime = DateTime.now(); System.out.println(dateTime.plusHours(1)); System.out.println(dateTime.minusHours(1)); }
LocalDateTime和DateTime都自帶增加和減少時(shí)間的方法
public static void main(String[] args) { //時(shí)間1990-12-05 DateTime birthDay = DateTime.now().withYear(1990).withMonthOfYear(10).withDayOfMonth(23); System.out.println(birthDay); //獲取相差得年 會(huì)進(jìn)行月份和日期比較 如 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(); //開始時(shí)間 Date startTime = now.toDate(); //結(jié)束時(shí)間 Date endTime = now.plusHours(1).toDate(); //1小時(shí) System.out.println("開始時(shí)間與結(jié)束時(shí)間的時(shí)間間隔:" + 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時(shí)間 System.out.println(utcDate); //北京時(shí)間 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()); }
北京時(shí)間=UTC+8
到此,關(guān)于“Java中常用時(shí)間的相關(guān)方法有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。