前兩節討論了那么多,這節就是兩個議題,討論了新增的日期的api,再說一說我的Java8的一些心得體會了。
首先,我們必須要搞清楚Java 8 為什么要增加新的日期的api,這是由於老的日期api非常的繁瑣,使用起來非常不方便,Java作者奉行這變者通不變者死的原則,於是增加了這些api。下面,我們總點介紹這幾個類——LocalDate類、LocalTime類、LocalDateTime類、DateTimeFormatter類,zoneDate類。一個個來看:
Ⅰ、LocalDate類——返回日期類。
LocalDate表示不帶時區的日期,比如2000-1-1.此類的常見方法是:
getYear——返回相應的年份,
getMonth——返回相應的月份,
getDayOfMonth——返回相應的月份的某一天,
of——傳遞整型格式化日期。
例如我們用此類將當前日期格式化xxxx年xx月xx日,並且與相應的tostring方法就行對比。例如,請看代碼:
LocalDate localDate = LocalDate.now(); int year = localDate.getYear(); int month = localDate.getMonthValue(); int day=localDate.getDayOfMonth(); DecimalFormat decimalFormat=new DecimalFormat("00"); System.out.println(year+"年"+decimalFormat.format(month)+"月"+decimalFormat.format(day)+"日"); System.out.println(localDate.toString());
運行結果如下:
我們看到Localdate不想calendar類中的月份的值自動少一了。
ⅡLocalTime類——返回時間類。
LocalTime表示不帶時區的時間,比如04:44:50.12。常見的方法有:
getHour——返回當前所對應的小時數,
getMinute——返回當前所對應的分鍾數,
getSecond——返回當前所對應的秒數,
ofxxxx——返回所對應某(小時,分鍾,秒)數.
例如我們用此類將當前時間格式化xx時xx分xx秒,並且與相應的tostring方法就行對比。例如,請看代碼:
LocalTime time=LocalTime.now(); DecimalFormat decimalFormat=new DecimalFormat("00"); int hour= time.getHour(); int minute=time.getMinute(); int second=time.getSecond(); System.out.println(decimalFormat.format(hour)+"時"+decimalFormat.format(minute)+"分"+decimalFormat.format(second)); System.out.println(time.toString());
運行結果如下:
從而看出LocalTime能夠拋棄時區的影響。
ⅢLocalDateTime類——返回時間日期類。
他是日期日期時間中一個最重要的類,它是LocalDate和LocalTime的組合體,表示的是不帶時區的日期及時間。看上去,LocalDateTime和Instant很象,但記得的是“Instant中是不帶時區的即時時間點。可能有人說,即時的時間點不就是日期+時間么?看上去是這樣的,但還是有所區別,比如LocalDateTime對於用戶來說,可能就只是一個簡單的日期和時間的概念,考慮如下的例子:兩個人都在2013年7月2日11點出生,第一個人是在英國出生,而第二個是在中國北京,如果我們問他們是在什么時候出生的話,則他們看上去都是在同樣的時間出生(就是LocalDateTime所表達的),但如果我們根據時間線(如格林威治時間線)去仔細考察,則會發現在出生的人會比在英國出生的人稍微早8個小時(這就是Instant所表達的概念,並且要將其轉換為UTC格式的時間)。常見的方法有:
getYear——返回相應的年份,
getMonth——返回相應的月份,
getDayOfMonth——返回相應的月份的某一天,
getHour——返回當前所對應的小時數,
getMinute——返回當前所對應的分鍾數,
getSecond——返回當前所對應的秒數。
例如我們用此類將當前時間格式化xxxx年xx月xx日 xx時xx分xx秒,並且與相應的tostring方法就行對比。例如,請看代碼:
LocalDateTime localDateTime = LocalDateTime.now(); int year = localDateTime.getYear(); int month = localDateTime.getMonthValue(); int day = localDateTime.getDayOfMonth(); DecimalFormat decimalFormat = new DecimalFormat("00"); int hour = localDateTime.getHour(); int minute = localDateTime.getMinute(); int second = localDateTime.getSecond(); System.out.println(year + "年" + decimalFormat.format(month) + "月" + decimalFormat.format(day) + "日" + " " + decimalFormat.format(hour) + "時" + decimalFormat.format(minute) + "分" + decimalFormat.format(second)); System.out.println(localDateTime.toString());
運行結果如下:
ⅣDateTimeFormatter類——返回日期時間格式化類。
java.text.NumberFormat不一樣的是DateTimeFormatter是不可變的並且是類型安全的。常見的方法有:
format方法——將其格式化成相應的字符串。請看源代碼:
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("MM dd, yyyy - HH:mm"); LocalDateTime parsed = LocalDateTime.parse("11 03, 2014 - 07:13", dateTimeFormatter); String string = dateTimeFormatter.format(parsed); System.out.println(string);
運行結果如下:
這樣格式化字符串更加的方便
ⅤZonedDateTime類——獲取相應的時區時間,媽媽再也不用為我的時區問題發愁了。
ZonedDateTime, ZoneId -時區很重要的時候使用.
我們看個實例,獲取相應時區時間:
ZonedDateTime zonedDateTime= ZonedDateTime.now();
System.out.println(zonedDateTime.toString());
運行結果如下:
看到沒有了,能夠獲取相應時區。十分的方便。
總之,Java8 日期的api十分方便。
java8最終總結,一些邊邊角角的知識拾人牙慧
反射和注解的變化
通過類型注解,我們能夠在更多的地方使用注解,例如像List<@Nullable String>這樣的泛型參數中。這增強了通過靜態分析工具發現錯誤的能力,它將增強並重定義Java內置的類型系統。
Nashorn JavaScript引擎
Nashorn是一個集成到JDK中的新的、輕量級、高性能的JavaScript實現。Nashorn是Rhino的繼任者,它提升了性能和內存使用情況。它將會支持javax.script API,但是它並不會支持DOM/CSS,也不會包含瀏覽器插件API。
java.lang、java.util等其他地方的新增功能
Java 8還向很多其他的包中添加了大量其他的功能,在本文中我們並沒有提及。下面是一些值得注意的內容。可以使用ThreadLocal.withInitial(Supplier)更加簡潔的聲明本地線程變量。長期未兌現的StringJoiner和String.join(...)現在已經是Java 8的一部分了。比較器提供了一些新的方法能夠用於鏈接和基於域的比較。默認的字符串池映射大小更大了,大約在25—50K。