Java8中新的Date和Time类入门详解
这篇文章主要是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提供了线程安全的不可变类。编程人员不用考虑并发的问题。
新的日期和时间的类别遵循“域驱动设计”。对于开发者来说,理解方法和类的功能是很容易的。
接下来让我们来看看新Date和Time API:
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")); } }
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)); } }
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)); } }
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()); } }
java.time.Duration:
Duration是用来计算两个给定的日期之间包含多少秒,多少毫秒,它是不可变类且线程安全的
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()); } }
以上是Java8中新的Date和Time类入门详解的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Java8计算一年前或一年后的日期利用minus()方法计算一年前的日期packagecom.shxt.demo02;importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;publicclassDemo09{publicstaticvoidmain(String[]args){LocalDatetoday=LocalDate.now();LocalDatepreviousYear=today.minus(1,ChronoUni

在使用PHP程序开发时,经常会碰到一些警告或者错误的提示信息。其中,可能出现的一个错误提示就是:PHPWarning:date()expectsparameter2tobelong,stringgiven。这个错误的提示信息意思是:函数date()的第二个参数期望是长整型(long),但是实际传递给它的是字符串(string)。那么,我们

如果您正在寻找根据系统时间戳自动创建文件和文件夹并为其命名的方法,那么您来对地方了。有一种超级简单的方法可以用来完成这项任务。然后,创建的文件夹或文件可用于各种目的,例如存储文件备份、根据日期对文件进行排序等。在本文中,我们将通过一些非常简单的步骤解释如何在Windows11/10中自动创建文件和文件夹,并根据系统的时间戳对其进行命名。使用的方法是批处理脚本,非常简单。希望你喜欢阅读这篇文章。第1节:如何根据系统当前时间戳自动创建文件夹并命名第1步:首先,导航到要在其中创建文件夹的父文件夹,

我们今天主要是来看一看golang time 包的时间应用方式。两者的一般规则是「wall time」用于告知时间,而「monotonic clock」用于测量时间;除外还有其他的时钟处理方式。

如何使用Date类的getTime()方法获取日期的毫秒表示形式在Java中,Date类是用于表示日期和时间的类。它提供了许多有用的方法来操作和获取日期对象的信息。其中,getTime()方法是Date类中的一个重要方法,它可以返回日期对象的毫秒表示形式。接下来,我们将详细介绍如何使用这个方法来获取日期的毫秒表示形式,并提供相应的代码示例。使用Date类的g

一.介绍java.util包中的Date类表示特定的时间,精确到毫秒。如果要想使用我们的Date类,那么我们必须得引入我们的Date类。Date类直接写入年份是得不到正确的结果的。因为java中Date是从1900年开始算的,所以前面的第一个参数只要填入从1900年后过了多少年就是你想要得到的年份。月需要减1,日可以直接插入。这种方法用的比较少,常用的是第二种方法。这种方法是将一个符合特定格式,比如yyyy-MM-dd,的字符串转化成为Date类型的数据。首先,定义一个Date类型的对象Date

Java8如何计算一周后的日期这个例子会计算一周后的日期。LocalDate日期不包含时间信息,它的plus()方法用来增加天、周、月,ChronoUnit类声明了这些时间单位。由于LocalDate也是不变类型,返回后一定要用变量赋值。packagecom.shxt.demo02;importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;publicclassDemo08{publicstaticvoidmain(String[

Stringbuild类由于String类的对象内容不可改变,每次拼接都会构建一个新的String对象,既耗时,又浪费内存空间这时需要通过java提供的StringBuild类解决这个问题StringBuilder又称为可变字符序列,它是一个类似于String的字符串缓冲区,可以看作是一个容器,容器中可以装很多字符串可变指的是StringBuilder对象中的内容是可变的构造方法publicStringBuilder():创建一个空的缓冲区publicStringBuilder(Stringsr
