011-jdk1.8鐗堟湰鏂扮壒鎬т笁-Date API
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了011-jdk1.8鐗堟湰鏂扮壒鎬т笁-Date API相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%8f%af%e5%8f%98' title='鍙彉'>鍙彉
ant 澶氭 emd 鍙樺寲 yesterday java缂栬瘧 鏂瑰紡 before1.7銆丏ate API
銆€銆€Java 8 鍦ㄥ寘java.time涓嬪寘鍚簡涓€缁勫叏鏂扮殑鏃堕棿鏃ユ湡API銆傛柊鐨勬棩鏈烝PI鍜屽紑婧愮殑Joda-Time搴撳樊涓嶅锛屼絾鍙堜笉瀹屽叏涓€鏍凤紝涓嬮潰鐨勪緥瀛愬睍绀轰簡杩欑粍鏂癆PI閲屾渶閲嶈鐨勪竴浜涢儴鍒嗭細
1銆丆lock 鏃堕挓
銆€銆€Clock绫绘彁渚涗簡璁块棶褰撳墠鏃ユ湡鍜屾椂闂寸殑鏂规硶锛孋lock鏄椂鍖烘晱鎰熺殑锛屽彲浠ョ敤鏉ュ彇浠?System.currentTimeMillis() 鏉ヨ幏鍙栧綋鍓嶇殑寰鏁般€傛煇涓€涓壒瀹氱殑鏃堕棿鐐逛篃鍙互浣跨敤Instant绫绘潵琛ㄧず锛孖nstant绫讳篃鍙互鐢ㄦ潵鍒涘缓鑰佺殑java.util.Date瀵硅薄銆?/p>
Clock clock = Clock.systemDefaultZone(); long millis = clock.millis(); Instant instant = clock.instant(); Date legacyDate = Date.from(instant); // legacy java.util.Date
2銆?strong>Timezones 鏃跺尯
鍦ㄦ柊API涓椂鍖轰娇鐢╖oneId鏉ヨ〃绀恒€傛椂鍖哄彲浠ュ緢鏂逛究鐨勪娇鐢ㄩ潤鎬佹柟娉昽f鏉ヨ幏鍙栧埌銆?鏃跺尯瀹氫箟浜嗗埌UTS鏃堕棿鐨勬椂闂村樊锛屽湪Instant鏃堕棿鐐瑰璞″埌鏈湴鏃ユ湡瀵硅薄涔嬮棿杞崲鐨勬椂鍊欐槸鏋佸叾閲嶈鐨勩€?/p>
System.out.println(ZoneId.getAvailableZoneIds()); // prints all available timezone ids ZoneId zone1 = ZoneId.of("Europe/Berlin"); ZoneId zone2 = ZoneId.of("Brazil/East"); System.out.println(zone1.getRules()); System.out.println(zone2.getRules()); // ZoneRules[currentStandardOffset=+01:00] // ZoneRules[currentStandardOffset=-03:00]
3銆丩ocalTime 鏈湴鏃堕棿
LocalTime 瀹氫箟浜嗕竴涓病鏈夋椂鍖轰俊鎭殑鏃堕棿锛屼緥濡?鏅氫笂10鐐癸紝鎴栬€?17:30:15銆備笅闈㈢殑渚嬪瓙浣跨敤鍓嶉潰浠g爜鍒涘缓鐨勬椂鍖哄垱寤轰簡涓や釜鏈湴鏃堕棿銆備箣鍚庢瘮杈冩椂闂村苟浠ュ皬鏃跺拰鍒嗛挓涓哄崟浣嶈绠椾袱涓椂闂寸殑鏃堕棿宸細
LocalTime now1 = LocalTime.now(zone1); LocalTime now2 = LocalTime.now(zone2); System.out.println(now1.isBefore(now2)); // false long hoursBetween = ChronoUnit.HOURS.between(now1, now2); long minutesBetween = ChronoUnit.MINUTES.between(now1, now2); System.out.println(hoursBetween); // -4 System.out.println(minutesBetween); // -299
LocalTime 鎻愪緵浜嗗绉嶅伐鍘傛柟娉曟潵绠€鍖栧璞$殑鍒涘缓锛屽寘鎷В鏋愭椂闂村瓧绗︿覆銆?/p>
LocalTime late = LocalTime.of(23, 59, 59); System.out.println(late); // 23:59:59 DateTimeFormatter germanFormatter = DateTimeFormatter .ofLocalizedTime(FormatStyle.SHORT) .withLocale(Locale.GERMAN); LocalTime leetTime = LocalTime.parse("13:37", germanFormatter); System.out.println(leetTime); // 13:37
4銆?strong>LocalDate 鏈湴鏃ユ湡
LocalDate 琛ㄧず浜嗕竴涓‘鍒囩殑鏃ユ湡锛屾瘮濡?2014-03-11銆傝瀵硅薄鍊兼槸涓嶅彲鍙樼殑锛岀敤璧锋潵鍜孡ocalTime鍩烘湰涓€鑷淬€備笅闈㈢殑渚嬪瓙灞曠ず浜嗗浣曠粰Date瀵硅薄鍔犲噺澶?鏈?骞淬€傚彟澶栬娉ㄦ剰鐨勬槸杩欎簺瀵硅薄鏄笉鍙彉鐨勶紝鎿嶄綔杩斿洖鐨勬€绘槸涓€涓柊瀹炰緥銆?/p>
LocalDate today = LocalDate.now(); LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS); LocalDate yesterday = tomorrow.minusDays(2); LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4); DayOfWeek dayOfWeek = independenceDay.getDayOfWeek(); System.out.println(dayOfWeek); // FRIDAY
浠庡瓧绗︿覆瑙f瀽涓€涓狶ocalDate绫诲瀷鍜岃В鏋怢ocalTime涓€鏍风畝鍗?/p>
DateTimeFormatter germanFormatter = DateTimeFormatter .ofLocalizedDate(FormatStyle.MEDIUM) .withLocale(Locale.GERMAN); LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter); System.out.println(xmas); // 2014-12-24
5銆?strong>LocalDateTime 鏈湴鏃ユ湡鏃堕棿
LocalDateTime 鍚屾椂琛ㄧず浜嗘椂闂村拰鏃ユ湡锛岀浉褰撲簬鍓嶄袱鑺傚唴瀹瑰悎骞跺埌涓€涓璞′笂浜嗐€侺ocalDateTime鍜孡ocalTime杩樻湁LocalDate涓€鏍凤紝閮芥槸涓嶅彲鍙樼殑銆侺ocalDateTime鎻愪緵浜嗕竴浜涜兘璁块棶鍏蜂綋瀛楁鐨勬柟娉曘€?/strong>
LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59); DayOfWeek dayOfWeek = sylvester.getDayOfWeek(); System.out.println(dayOfWeek); // WEDNESDAY Month month = sylvester.getMonth(); System.out.println(month); // DECEMBER long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY); System.out.println(minuteOfDay); // 1439
鍙闄勫姞涓婃椂鍖轰俊鎭紝灏卞彲浠ュ皢鍏惰浆鎹负涓€涓椂闂寸偣Instant瀵硅薄锛孖nstant鏃堕棿鐐瑰璞″彲浠ュ緢瀹规槗鐨勮浆鎹负鑰佸紡鐨刯ava.util.Date銆?/p>
Instant instant = sylvester .atZone(ZoneId.systemDefault()) .toInstant(); Date legacyDate = Date.from(instant); System.out.println(legacyDate); // Wed Dec 31 23:59:59 CET 2014
鏍煎紡鍖朙ocalDateTime鍜屾牸寮忓寲鏃堕棿鍜屾棩鏈熶竴鏍风殑锛岄櫎浜嗕娇鐢ㄩ瀹氫箟濂界殑鏍煎紡澶栵紝鎴戜滑涔熷彲浠ヨ嚜宸卞畾涔夋牸寮忥細
DateTimeFormatter formatter = DateTimeFormatter .ofPattern("MMM dd, yyyy - HH:mm"); LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter); String string = formatter.format(parsed); System.out.println(string); // Nov 03, 2014 - 07:13
鍜宩ava.text.NumberFormat涓嶄竴鏍风殑鏄柊鐗堢殑DateTimeFormatter鏄笉鍙彉鐨勶紝鎵€浠ュ畠鏄嚎绋嬪畨鍏ㄧ殑銆?br />鍏充簬鏃堕棿鏃ユ湡鏍煎紡鐨勮缁嗕俊鎭細http://download.java.net/jdk8/docs/api/java/time/format/DateTimeFormatter.html
1.8銆佸閲嶆敞瑙?/p>
Java 8鍦ㄧ紪璇戝櫒灞傚仛浜嗕紭鍖栵紝鐩稿悓娉ㄨВ浼氫互闆嗗悎鐨勬柟寮忎繚瀛橈紝鍥犳搴曞眰鐨勫師鐞嗗苟娌℃湁鍙樺寲銆?/p>
棣栧厛瀹氫箟涓€涓寘瑁呯被Hints娉ㄨВ鐢ㄦ潵鏀剧疆涓€缁勫叿浣撶殑Hint娉ㄨВ锛?/p>
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface Hints { Hint[] value(); } @Repeatable(Hints.class) @interface Hint { String value(); }
Java 8鍏佽鎴戜滑鎶婂悓涓€涓被鍨嬬殑娉ㄨВ浣跨敤澶氭锛屽彧闇€瑕佺粰璇ユ敞瑙f爣娉ㄤ竴涓婡Repeatable鍗冲彲銆?/p>
渚?1: 浣跨敤鍖呰绫诲綋瀹瑰櫒鏉ュ瓨澶氫釜娉ㄨВ锛堣€佹柟娉曪級
@Hints({@Hint("hint1"), @Hint("hint2")}) class Person {}
渚?2锛氫娇鐢ㄥ閲嶆敞瑙o紙鏂版柟娉曪級
@Hint("hint1") @Hint("hint2") class Person {}
绗簩涓緥瀛愰噷java缂栬瘧鍣ㄤ細闅愭€х殑甯綘瀹氫箟濂紷Hints娉ㄨВ锛屼簡瑙h繖涓€鐐规湁鍔╀簬浣犵敤鍙嶅皠鏉ヨ幏鍙栬繖浜涗俊鎭細
Hint hint = Person.class.getAnnotation(Hint.class); System.out.println(hint);// null // 鑰佺殑鏂瑰紡 Hints hints1 = Person.class.getAnnotation(Hints.class); System.out.println(hints1.value().length); // 2 // 鎺ㄨ崘鐨勬柟寮?鍙嶅皠鐩稿叧鐨凙PI鎻愪緵浜嗘柊鐨勫嚱鏁癵etAnnotationsByType()鏉ヨ繑鍥為噸澶嶆敞瑙g殑绫诲瀷銆?/span> Hint[] hints2 = Person.class.getAnnotationsByType(Hint.class); System.out.println(hints2.length); // 2
鍗充究鎴戜滑娌℃湁鍦≒erson绫讳笂瀹氫箟@Hints娉ㄨВ锛屾垜浠繕鏄彲浠ラ€氳繃 getAnnotation(Hints.class) 鏉ヨ幏鍙?@Hints娉ㄨВ锛屾洿鍔犳柟渚跨殑鏂规硶鏄娇鐢?getAnnotationsByType 鍙互鐩存帴鑾峰彇鍒版墍鏈夌殑@Hint娉ㄨВ銆?br />鍙﹀Java 8鐨勬敞瑙h繕澧炲姞鍒颁袱绉嶆柊鐨則arget涓婁簡锛?/p>
@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) @interface MyAnnotation {}
鐨?/p>
以上是关于011-jdk1.8鐗堟湰鏂扮壒鎬т笁-Date API的主要内容,如果未能解决你的问题,请参考以下文章
Webpack5.0 鏂扮壒鎬у皾椴滃疄鎴?馃馃 [姣忔棩鍓嶇澶滆瘽0x37]