本文實例講述了java8 localdate localdatetime等時間類用法。分享給大家供大家參考,具體如下:
這篇文章主要是java8中新的date和time api的實戰。新的date和time類是java開發者社區千呼萬喚始出來的。java8 之前存在的date類一直都受人詬病,很多人都會選擇使用第三方的date庫joda-time。java8中的date和time api是jodatime的作者參與開發的,實現了jsr310的全部內容。這些新的api都在包java.time下。
既然第三方的joda-time,date4j都已經足夠強大了,為什么java8還要重新實現他呢,一部分的原因是這些第三方的庫是存在兼容問題的,比如標準的jsf日期轉化器與joda-time api,就不兼容,每次使用都需要編寫自己的轉換器,所以標準化api是必須的,就有了jsr310,java8中就實現了他全部的規定內容。
新date類和time類背后的設計原則:
不可變類
java8之前,date類都是可變類。當我們在多線程環境下使用它,編程人員應該確認date對象的線程安全。java8的date和time api提供了線程安全的不可變類。編程人員不用考慮并發的問題。
領域模型驅動設計方法
新的日期和時間的類別遵循“域驅動設計”。對于開發者來說,理解方法和類的功能是很容易的。
java.time.localdate:
localdate只提供日期不提供時間信息。它是不可變類且線程安全的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package org.smarttechie; import java.time.localdate; import java.time.temporal.chronounit; /** * this class demonstrates java 8 data and time api * @author siva prasad rao janapati * */ public class datetimedemonstration { /** * @param args */ public static void main(string[] args) { //create date localdate localdate = localdate.now(); system.out.println( "the local date is :: " + localdate); //find the length of the month. that is, how many days are there for this month. system.out.println( "the number of days available for this month:: " + localdate.lengthofmonth()); //know the month name system.out.println( "what is the month name? :: " + localdate.getmonth().name()); //add 2 days to the today's date. system.out.println(localdate.plus( 2 , chronounit.days)); //substract 2 days from today system.out.println(localdate.minus( 2 , chronounit.days)); //convert the string to date system.out.println(localdate.parse( "2017-04-07" )); } } |
運行結果:
java.time.localtime:
localtime只提供時間而不提供日期信息,它是不可變類且線程安全的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import java.time.localtime; import java.time.temporal.chronounit; /** * this class demonstrates java 8 data and time api * @author siva prasad rao janapati * */ public class datetimedemonstration { /** * @param args */ public static void main(string[] args) { //get local time localtime localtime = localtime.now(); system.out.println(localtime); //get the hour of the day system.out.println( "the hour of the day:: " + localtime.gethour()); //add 2 hours to the time. system.out.println(localtime.plus( 2 , chronounit.hours)); //add 6 minutes to the time. system.out.println(localtime.plusminutes( 6 )); //substract 2 hours from current time system.out.println(localtime.minus( 2 , chronounit.hours)); } } |
運行結果:
java.time.localdatetime:
localdatetime提供時間和日期的信息,它是不可變類且線程安全的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import java.time.localdatetime; import java.time.temporal.chronounit; /** * this class demonstrates java 8 data and time api * @author siva prasad rao janapati * */ public class datetimedemonstration { /** * @param args */ public static void main(string[] args) { //get localdatetime object localdatetime localdatetime = localdatetime.now(); system.out.println(localdatetime); //find the length of month. that is, how many days are there for this month. system.out.println( "the number of days available for this month:: " + localdatetime.getmonth().length( true )); //know the month name system.out.println( "what is the month name? :: " + localdatetime.getmonth().name()); //add 2 days to today's date. system.out.println(localdatetime.plus( 2 , chronounit.days)); //substract 2 days from today system.out.println(localdatetime.minus( 2 , chronounit.days)); } } |
運行結果:
java.time.year:
year提供年的信息,它是不可變類且線程安全的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.time.year; import java.time.temporal.chronounit; /** * this class demonstrates java 8 data and time api * @author siva prasad rao janapati * */ public class datetimedemonstration { /** * @param args */ public static void main(string[] args) { //get year year year = year.now(); system.out.println( "year ::" + year); //know the year is leap year or not system.out.println( "is year[" +year+ "] leap year?" + year.isleap()); } } |
運行結果:
java.time.duration:
duration是用來計算兩個給定的日期之間包含多少秒,多少毫秒,它是不可變類且線程安全的
java.time.period:
period是用來計算兩個給定的日期之間包含多少天,多少月或者多少年,它是不可變類且線程安全的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import java.time.localdate; import java.time.period; import java.time.temporal.chronounit; /** * this class demonstrates java 8 data and time api * @author siva prasad rao janapati * */ public class datetimedemonstration { /** * @param args */ public static void main(string[] args) { localdate localdate = localdate.now(); period period = period.between(localdate, localdate.plus( 2 , chronounit.days)); system.out.println(period.getdays()); } } |
運行結果:
希望本文所述對大家java程序設計有所幫助。