java8中新的Date和Time详解
新Date类和Time类背后的设计原则:
不可变类
java8之前,Date类都是可变类。当我们在多线程环境下使用它,编程人员应该确认Date对象的线程安全。Java8的Date和TimeAPI提供了线程安全的不可变类。编程人员不用考虑并发的问题。
领域模型驱动设计方法
新的日期和时间的类别遵循“域驱动设计”。对于开发者来说,理解方法和类的功能是很容易的。
接下来让我们来看看新Date和TimeAPI:
1.java.time.LocalDate:
LocalDate只提供日期不提供时间信息。它是不可变类且线程安全的。
packageorg.smarttechie;
importjava.time.LocalDate;
importjava.time.temporal.ChronoUnit;
/**
*ThisclassdemonstratesJAVA8dataandtimeAPI
*@authorSivaPrasadRaoJanapati
**/
publicclassDateTimeDemonstration{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//CreatedateLocalDatelocalDate=LocalDate.now();
System.out.println("Thelocaldateis::"+localDate);
//Findthelengthofthemonth.Thatis,howmanydaysarethereforthismonth.
System.out.println("Thenumberofdaysavailableforthismonth::"+localDate.lengthOfMonth());
//Knowthemonthname
System.out.println("Whatisthemonthname?::"+localDate.getMonth().name());
//add2daystothetoday'sdate.
System.out.println(localDate.plus(2,ChronoUnit.DAYS));
//substract2daysfromtoday
System.out.println(localDate.minus(2,ChronoUnit.DAYS));
//Convertthestringtodate
System.out.println(localDate.parse("2017-04-07"));
}
}
2.java.time.LocalTime:
LocalTime只提供时间而不提供日期信息,它是不可变类且线程安全的。
packageorg.smarttechie;
importjava.time.LocalTime;
importjava.time.temporal.ChronoUnit;
/**
*ThisclassdemonstratesJAVA8dataandtimeAPI
*@authorSivaPrasadRaoJanapati
**/
publicclassDateTimeDemonstration{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//Getlocaltime
LocalTimelocalTime=LocalTime.now();
System.out.println(localTime);
//Getthehouroftheday
System.out.println("Thehouroftheday::"+localTime.getHour());
//add2hourstothetime.
System.out.println(localTime.plus(2,ChronoUnit.HOURS));
//add6minutestothetime.
System.out.println(localTime.plusMinutes(6));
//substract2hoursfromcurrenttime
System.out.println(localTime.minus(2,ChronoUnit.HOURS));
}
}
3.java.time.LocalDateTime:
LocalDateTime提供时间和日期的信息,它是不可变类且线程安全的
packageorr.smarttechie;
importjava.time.LocalDateTime;
importjava.time.temporal.ChronoUnit;
/**
*ThisclassdemonstratesJAVA8dataandtimeAPI
*@authorSivaPrasadRaoJanapati
*
*/
publicclassDateTimeDemonstration{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//GetLocalDateTimeobject
LocalDateTimelocalDateTime=LocalDateTime.now();
System.out.println(localDateTime);
//Findthelengthofmonth.Thatis,howmanydaysarethereforthismonth.
System.out.println("Thenumberofdaysavailableforthismonth::"+localDateTime.getMonth().length(true));
//Knowthemonthname
System.out.println("Whatisthemonthname?::"+localDateTime.getMonth().name());
//add2daystotoday'sdate.
System.out.println(localDateTime.plus(2,ChronoUnit.DAYS));
//substract2daysfromtoday
System.out.println(localDateTime.minus(2,ChronoUnit.DAYS));
}
}
4.java.time.Year:
Year提供年的信息,它是不可变类且线程安全的。
packageorr.smarttechie;
importjava.time.Year;
importjava.time.temporal.ChronoUnit;
/**
*ThisclassdemonstratesJAVA8dataandtimeAPI
*@authorSivaPrasadRaoJanapati
*
*/
publicclassDateTimeDemonstration{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//Getyear
Yearyear=Year.now();
System.out.println("Year::"+year);
//knowtheyearisleapyearornot
System.out.println("Isyear["+year+"]leapyear?"+year.isLeap());
}
}
5.java.time.Duration:
Duration是用来计算两个给定的日期之间包含多少秒,多少毫秒,它是不可变类且线程安全的
6.java.time.Period:
Period是用来计算两个给定的日期之间包含多少天,多少月或者多少年,它是不可变类且线程安全的
packageorr.smarttechie;
importjava.time.LocalDate;
importjava.time.Period;
importjava.time.temporal.ChronoUnit;
/**
*ThisclassdemonstratesJAVA8dataandtimeAPI
*@authorSivaPrasadRaoJanapati
*
*/
publicclassDateTimeDemonstration{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
LocalDatelocalDate=LocalDate.now();
Periodperiod=Period.between(localDate,localDate.plus(2,ChronoUnit.DAYS));
System.out.println(period.getDays());
}
}