Detailed introduction to the new Date and Time classes in Java 8
This article is mainly about the actual combat of the new Date and Time API in java8. The new Date and Time classes are the result of long-awaited requests from the Java developer community. The Date class that existed before Java8 has always been criticized, and many people choose to use the third-party date library joda-time. The date and time API in Java8 was developed by the author of jodatime and implements all the contents of JSR310. These new APIs are under the package java.time.
Since the third-party joda-time and date4j are already powerful enough, why does Java8 need to re-implement them? Part of the reason is that these third-party libraries have compatibility issues, such as the standard JSF date The converter is incompatible with the joda-time API. Every time you use it, you need to write your own converter. Therefore, a standardized API is necessary. With JSR310, all its provisions are implemented in Java8.
Design principles behind the new Date class and Time class:
Immutable class
Before java8, the Date class was It is a mutable class. When we use it in a multi-threaded environment, programmers should confirm that the Date object is thread-safe. Java 8's Date and Time APIs provide thread-safe immutable classes. Programmers do not need to consider concurrency issues.
DomainModelDrivenDesign Approach
The new date and time categories follow "Domain Driven Design". It is easy for developers to understand the functionality of methods and classes.
Next let's take a look at the new Date and Time API:
java.time.LocalDate:
LocalDate only provides date but not time information. It is immutable and thread-safe.
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 only provides time but not date information. It is an immutable class and thread-safe.
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 provides time and date information, it is an immutable class and thread-safe
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 provides year information, it is an immutable class and thread-safe.
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 is used to calculate how many seconds and milliseconds are included between two given dates. It is an immutable class And thread-safe
java.time.Period:
Period is used to calculate how many days, months or years there are between two given dates. , it is an immutable class and thread-safe
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()); } }
The above is the detailed content of Detailed introduction to the new Date and Time classes in Java 8. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Java8 calculates the date one year ago or one year later using the minus() method to calculate the date one year ago packagecom.shxt.demo02;importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;publicclassDemo09{publicstaticvoidmain(String[]args ){LocalDatetoday=LocalDate.now();LocalDatepreviousYear=today.minus(1,ChronoUni

When developing using PHP programs, you often encounter some warning or error messages. Among them, one error message that may appear is: PHPWarning:date()expectsparameter2tobelong,stringgiven. The error message means: the second parameter of the function date() is expected to be a long integer (long), but what is actually passed to it is a string (string). So, we

If you're looking for a way to automatically create and name files and folders based on system timestamps, you've come to the right place. There is a super simple way to accomplish this task. The created folders or files can then be used for various purposes such as storing file backups, sorting files based on date, etc. In this article, we will explain in some very simple steps how to automatically create files and folders in Windows 11/10 and name them according to the system’s timestamp. The method used is a batch script, which is very simple. Hope you enjoyed reading this article. Section 1: How to automatically create and name a folder based on the current timestamp of the system Step 1: First, navigate to the parent folder where you want to create the folder,

Today we are mainly going to take a look at the time application method of golang time package. The general rule between the two is that "wall time" is used to tell time, and "monotonic clock" is used to measure time; there are other clock processing methods.

How to get millisecond representation of date using getTime() method of Date class In Java, Date class is a class used to represent date and time. It provides many useful methods to manipulate and obtain information about date objects. Among them, the getTime() method is an important method in the Date class, which can return the millisecond representation of the date object. Next, we will detail how to use this method to obtain the millisecond representation of a date, and provide corresponding code examples. Using the Date class

1. Introduction The Date class in the java.util package represents a specific time, accurate to milliseconds. If we want to use our Date class, then we must introduce our Date class. Writing the year directly into the Date class will not produce the correct result. Because Date in Java is calculated from 1900, so as long as you fill in the first parameter with the number of years since 1900, you will get the year you want. The month needs to be subtracted by 1, and the day can be inserted directly. This method is rarely used, and the second method is commonly used. This method is to convert a string that conforms to a specific format, such as yyyy-MM-dd, into Date type data. First, define an object of Date type Date

How to calculate the date one week later in Java8 This example will calculate the date one week later. The LocalDate date does not contain time information. Its plus() method is used to add days, weeks, and months. The ChronoUnit class declares these time units. Since LocalDate is also an immutable type, you must use variables to assign values after returning. packagecom.shxt.demo02;importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;publicclassDemo08{publicstaticvoidmain(String[

Stringbuild class Since the object content of the String class cannot be changed, a new String object will be constructed every time it is spliced, which is time-consuming and wastes memory space. At this time, you need to solve this problem through the StringBuild class provided by Java. StringBuilder is also called a variable character sequence. , it is a string buffer similar to String, which can be regarded as a container. Many strings can be held in the container. Variable means that the content in the StringBuilder object is variable. The construction method publicStringBuilder(): creates an empty buffer publicStringBuilder(Stringsr
