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缂栬瘧   鏂瑰紡   before   

1.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]

绉嶈崏ECMAScript2021鏂扮壒鎬?/a>

鍏ㄧ綉棣栧彂锛侊紒C++20鏂扮壒鎬у叏鍦ㄨ繖涓€寮犲浘閲屼簡

webpack 3 姝e紡鍙戝竷锛岃浣犳洿鈥滃揩鈥?/h1>

銆屼竴鍔虫案閫搞€嶇敱娴呭叆娣遍厤缃畐ebpack4

KaliLinux鍒囨崲python鐗堟湰