🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
This article is the series of Java Date Time API Tutorial. In this article, we will discuss important methods or APIs of the Java Period class from the java.time package.
The Period class represents a quantity of time in terms of years, months and days. A Period uses date-based values (years, months, days). This class is immutable and thread-safe.
Java Period Class Diagram
The below class diagram shows a list of methods/APIs that the Period class provides.
Java Period Class Methods/APIs with Examples
Let's demonstrates the usage of a few important and commonly used Period class methods with examples. In below PeriodMethodsExample class, each method name describes the Period class method and its usage. Refer https://docs.oracle.com/javase/8/docs/api/java/time/Period.html Java doc for more details.
package net.javaguides.date;
import java.time.LocalDateTime;
import java.time.Period;
/**
* Class demonstrates the usage of Period class methods with examples
*
* @author Ramesh Fadatare
*
*/
public class PeriodMethodsExample {
public static void main(String[] args) {
PeriodMethodsExample example = new PeriodMethodsExample();
example.withYearsMethodExample();
example.withMonthsMethodExample();
example.withDaysMethodExample();
example.toTotalMonthsMethodExample();
example.subtractFromMethodExample();
example.plusYearsMethodExample();
example.plusMonthsMethodExample();
example.plusDaysMethodExample();
example.plusMethodExample();
example.parseMethodExample();
example.ofYearsMethodExample();
example.ofWeeksMethodExample();
example.ofMonthsMethodExample();
example.ofDaysMethodExample();
example.ofMethodExample();
example.minusYearsMethodExample();
example.minusMonthsMethodExample();
example.minusDaysMethodExample();
}
public void withYearsMethodExample() {
Period period = Period.ofYears(2);
System.out.println(period.toString());
period = period.withYears(5);
System.out.println(period.toString());
}
public void withMonthsMethodExample() {
Period period = Period.ofMonths(2);
System.out.println(period.toString());
period = period.withMonths(5);
System.out.println(period.toString());
}
public void withDaysMethodExample() {
Period period = Period.ofDays(2);
System.out.println(period.toString());
period = period.withDays(5);
System.out.println(period.toString());
}
public void toTotalMonthsMethodExample() {
Period period = Period.ofYears(2);
System.out.println(period.toTotalMonths());
}
public void subtractFromMethodExample() {
Period period = Period.ofYears(2);
LocalDateTime date = LocalDateTime.now();
System.out.println(date);
date = (LocalDateTime) period.subtractFrom(date);
System.out.println(date);
}
public void plusYearsMethodExample() {
Period period = Period.ofYears(2);
Period period1 = period.plusYears(1);
System.out.println(period1.getYears());
}
public void plusMonthsMethodExample() {
Period period = Period.ofMonths(2);
Period period1 = period.plusMonths(1);
System.out.println(period1.getMonths());
}
public void plusDaysMethodExample() {
Period period = Period.ofDays(2);
Period period1 = period.plusDays(1);
System.out.println(period1.getDays());
}
public void plusMethodExample() {
Period period = Period.of(1, 5, 2);
System.out.println(
"Years: " + period.getYears() + ", Months: " + period.getMonths() + ", Days: " + period.getDays());
Period period1 = period.plus(Period.ofDays(5));
System.out.println(
"Years: " + period1.getYears() + ", Months: " + period1.getMonths() + ", Days: " + period1.getDays());
}
public void parseMethodExample() {
Period period = Period.parse("P1Y2M3D");
System.out.println(
"Years: " + period.getYears() + ", Months: " + period.getMonths() + ", Days: " + period.getDays());
}
public void ofYearsMethodExample() {
Period period = Period.ofYears(2);
System.out.println(period.getYears());
}
public void ofWeeksMethodExample() {
Period period = Period.ofWeeks(2);
System.out.println(period.getDays());
}
public void ofMonthsMethodExample() {
Period period = Period.ofMonths(2);
System.out.println(period.getMonths());
}
public void ofDaysMethodExample() {
Period period = Period.ofDays(2);
System.out.println(period.getDays());
}
public void ofMethodExample() {
Period period = Period.of(1, 5, 2);
System.out.println(
"Years: " + period.getYears() + ", Months: " + period.getMonths() + ", Days: " + period.getDays());
}
public void minusYearsMethodExample() {
Period period = Period.ofYears(5);
System.out.println(period.getYears());
Period period1 = period.minusYears(3);
System.out.println(period1.getYears());
}
public void minusMonthsMethodExample() {
Period period = Period.ofMonths(5);
System.out.println(period.getMonths());
Period period1 = period.minusMonths(3);
System.out.println(period1.getMonths());
}
public void minusDaysMethodExample() {
Period period = Period.ofDays(5);
System.out.println(period.getDays());
Period period1 = period.minusDays(3);
System.out.println(period1.getDays());
}
}
Output:
P2Y
P5Y
P2M
P5M
P2D
P5D
24
2018-12-22T13:27:23.928
2016-12-22T13:27:23.928
3
3
3
Years: 1, Months: 5, Days: 2
Years: 1, Months: 5, Days: 7
Years: 1, Months: 2, Days: 3
2
14
2
2
Years: 1, Months: 5, Days: 2
5
2
5
2
5
2
Related Java 8 Date & Time API Guide
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business

Comments
Post a Comment
Leave Comment