java8中新的Date和Time详解
程序员文章站
2024-03-12 20:48:56
新date类和time类背后的设计原则:
不可变类
java8之前,date类都是可变类。当我们在多线程环境下使用它,编程人员应该确认date对象的线程安全。java8...
新date类和time类背后的设计原则:
不可变类
java8之前,date类都是可变类。当我们在多线程环境下使用它,编程人员应该确认date对象的线程安全。java8的date和time api提供了线程安全的不可变类。编程人员不用考虑并发的问题。
领域模型驱动设计方法
新的日期和时间的类别遵循“域驱动设计”。对于开发者来说,理解方法和类的功能是很容易的。
接下来让我们来看看新date和time api:
1.java.time.localdate:
localdate只提供日期不提供时间信息。它是不可变类且线程安全的。
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")); } }
2.java.time.localtime:
localtime只提供时间而不提供日期信息,它是不可变类且线程安全的。
package org.smarttechie; 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)); } }
3.java.time.localdatetime:
localdatetime提供时间和日期的信息,它是不可变类且线程安全的
package orr.smarttechie; 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)); } }
4.java.time.year:
year提供年的信息,它是不可变类且线程安全的。
package orr.smarttechie; 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()); } }
5.java.time.duration:
duration是用来计算两个给定的日期之间包含多少秒,多少毫秒,它是不可变类且线程安全的
6.java.time.period:
period是用来计算两个给定的日期之间包含多少天,多少月或者多少年,它是不可变类且线程安全的
package orr.smarttechie; 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()); } }
推荐阅读
-
php 时间time与日期date之间的使用详解及区别
-
mybatis如何使用Java8的日期LocalDate和LocalDateTime详解
-
mybatis如何使用Java8的日期LocalDate和LocalDateTime详解
-
详解Java8新特性之interface中的static方法和default方法
-
详解Java8新特性之interface中的static方法和default方法
-
Java时间类Date类和Calendar类的使用详解
-
Java8中stream和functional interface的配合使用详解
-
php中time()和date()函数获取时间与本地不一致的问题
-
Java8与传统的日期和时间类详解
-
Java8中 LocalDate和java.sql.Date的相互转换操作