Java Period
A java period is a class used to measure time in years, months, and days. The package of the Period class in java is java.time.Period. The Period class object specifies the period of time or is used to determine the difference between two times in years, months, and days. A Period object is immutable and thread-safe also as the Period object is immutable, so we cannot change its values once it is created. But, we can create new Period objects based on another Period object. The Period class inherits an object class ( as the object is the superclass of all classes in java) and implements the ChronoPeriod interface.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
The syntax of the Period class declaration in java is as follows:
public final class Period extends Object implements ChronoPeriod, Serializable { // variables and method of the class Period }
Methods to Explain Java Period
The lists of Period class methods are explained below with example code; an example code can be used further for similar methods (as for each method example code not given):
- long get(TemporalUnit unit): Return the value of the requested unit.
- The static period between(LocalDate startInclusive, LocalDate endExclusive): Return Period object is the period between two dates of the number of years, months, and days.
Code:
import java.time.Period; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class PeriodClassDemo { public static void main(String[] args) { Period p = Period.between(LocalDate.ofYearDay(2017, 20), LocalDate.ofYearDay(2017, 30) ); System.out.println(p); System.out.println(p.get(ChronoUnit.DAYS)); System.out.println(p.get(ChronoUnit.MONTHS)); System.out.println(p.get(ChronoUnit.YEARS)); } }
Output:
- Temporal addTo(Temporal temporal): Return Period object, which is the addition of temporal and this Period object.
Code:
import java.time.Period; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.time.LocalDateTime; public class PeriodClassDemo { public static void main(String[] args) { Period period = Period.of(1,1,1); LocalDateTime d = LocalDateTime.now(); System.out.println(d); d = (LocalDateTime)period.addTo(d); System.out.println(d); } }
Output:
- boolean equals(Object otherPeriod): Checks this Period with specified period and return Boolean.
- boolean isNegative(): Return True if this period is negative.
- boolean isZero(): Return True if this period is negative.
Code:
import java.time.Period; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.time.LocalDateTime; public class PeriodClassDemo { public static void main(String[] args) { Period p1 = Period.of(1,1,1); Period p2 = Period.of(10,5,2); Period p3 = Period.of(10,5,2); System.out.println(p1.equals(p2)); System.out.println(p2.equals(p3)); System.out.println(p2.isNegative()); System.out.println(p2.isZero()); } }
Output:
- static Duration from(TemporalAmount amount): Obtains an instance of the period from a temporal amount.
Code:
import java.time.Period; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.time.LocalDateTime; public class PeriodClassDemo { public static void main(String[] args) { Period p = Period.from(Period.of(10, 5, 2) ); System.out.println(p); } }
Output:
- IsoChronology getChronology(): Return the chronology of this period, which is of the ISO calendar system.
- int getDays(): Return Period in days.
- int getMonths(): Return Period in months.
-
List
getUnits(): Return set of units supported by this period. - int hashCode(): Return hash code for this period.
Code:
import java.time.Period; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.time.LocalDateTime; public class PeriodClassDemo { public static void main(String[] args) { Period p = Period.from(Period.of(10, 5, 2) ); System.out.println(p.getChronology()); System.out.println(p.get(ChronoUnit.MONTHS)); System.out.println(p.getDays()); System.out.println(p.getMonths()); System.out.println(p.getYears()); System.out.println(p.getClass()); System.out.println(p.getUnits()); } }
Output:
- Period minus(TemporalAmount amountToSubtract): Returns object resulting from this period subtracted with the specified period.
- Period minusDays(long daysToSubtract): Returns object they subtracted with the specified Days.
- Period mindsmonths(long months): Returns object, the result of this period subtracted with the specified months.
- Period minusYears(long years): Returns object, which is the result of this period subtracted from the specified years.
- Period multipliedBy(long multiplicand): Returns a period object multiplied by the scalar.
- Period negated(): Returns a period object that results from this period with the length negated.
- Period normalized(): Returns a period object that results from this period with normalized in the years and months.
- static period of(int years, int months, int days): Returns a Period object representing several years, months, and days.
- static Period ofDays(int days): Returns a Period object of several days.
- static Period ofMonths(int months): Returns a Period object of several months.
- static Period ofWeeks(int weeks): Returns a Period object of several weeks.
- static Period ofYears(int years): Returns a Period object of several weeks.
Code:
import java.time.Period; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.time.LocalDateTime; public class PeriodClassDemo { public static void main(String[] args) { Period p1 = Period.of(10,5,2); Period p2 = Period.of(20,5,2); System.out.println(p1.getDays()); System.out.println(p2.getDays()); System.out.println(p1.minus(p2).getDays()); System.out.println(p1.minusDays(2).getDays()); System.out.println(p1.minusMonths(1).getDays()); System.out.println(p1.minusYears(1).getDays()); System.out.println(p1.multipliedBy(2).getDays()); System.out.println(p1.negated().getDays()); System.out.println(p1.normalized().getDays()); Period p3 = Period.ofDays(1); System.out.println(p3.getDays( )); Period p4 = Period.ofMonths(2); System.out.println(p4.getMonths()); } }
Output:
- static Period parse(CharSequence text): Return Period object from a text, for example, PnYnMnD.
- Period plus(TemporalAmount amountToAdd): Return the period object of this period with added the specified period.
- Period plusDays(long daysToAdd ): Return a period object of this period with added the specified days.
- Period plusMonths(long monthsToAdd): Return a period object of this period with added the specified months.
- Period plusYears(long yearsToAdd): Return a period object of this period with added the specified years.
Code:
import java.time.Period; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.time.LocalDateTime; public class PeriodClassDemo { public static void main(String[] args) { Period p1 = Period.of(10,5,2); Period p2 = Period.parse("P1Y2M3D"); System.out.println(p2.getMonths()); Period p3 = p1.plus(Period.ofDays(5)); System.out.println(p3); } }
Output:
- Temporal subtractFrom(Temporal temporal): Return Subtraction of this period from a temporal object.
- String toString(): Return this period in string representation, such as PT8H6M12.345S.
- long toTotalMonths(): Return the total number of months in this period.
- Period withDays(int days): Return a period object of this period with the specified amount of days.
- Period withMonths(int months): Return a period object of this period with the specified amount of months.
- Period withYears(int years): Return a period object of this period with the specified amount of Years.
Code:
import java.time.Period; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.time.LocalDateTime; public class PeriodClassDemo { public static void main(String[] args) { Period p1 = Period.of(10,5,2); System.out.println(p1); LocalDateTime d = LocalDateTime.now(); System.out.println(d); d = (LocalDateTime)p1.subtractFrom(d); System.out.println(d); System.out.println(p1.toString()); System.out.println(p1.toTotalMonths()); System.out.println(p1.withDays(2)); System.out.println(p1.toString()); System.out.println(p1.withMonths(1)); System.out.println(p1.toString()); } }
Output:
Conclusion
The Period class is one of the built-in class in java, which is used to measure time in years, months, and days and add, subtract, and convert the period, or in simple words, the period class allows one to operate on day or month, or year period. The period class is available in java.time.Period package of java.
The above is the detailed content of Java Period. 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











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
