Home >Java >javaTutorial >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 }
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):
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:
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:
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:
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:
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:
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:
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:
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:
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!