Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Creating and Initializing Periods

Header Image

Ahoy mateys! In our quest for mastering the Java DateTime library, we’ve covered quite a lot of ground. We’ve navigated through dates, times, calendars, and even time zones. But our journey isn’t over yet! Today, we’re going to be diving into periods - a crucial concept in working with dates and times.

In the world of pirates, a period of time can mean the difference between a successful heist and walking the plank. Similarly, in the world of programming, a period of time can mean the difference between a working application and a broken one. Fortunately, the Java DateTime library provides us with the Period class to help us keep track of these important time spans.

Using the Period class

The Period class is a part of the java.time package and represents a period of time defined in terms of years, months, and days. Creating a new period is as simple as calling the of() method and passing in the desired values:

Period twoYearsThreeMonthsOneDay = Period.of(2, 3, 1);

In this example, we’re creating a period of 2 years, 3 months, and 1 day. The Period class is immutable, which means that once created, we cannot modify its values. This helps us ensure that the period remains consistent and reliable throughout our program.

We can also create a period using a single unit of time, such as days or months:

Period twoMonths = Period.ofMonths(2);
Period tenDays = Period.ofDays(10);

These examples create a period of 2 months and a period of 10 days, respectively.

Now, let’s say we have a LocalDate object that represents a pirate’s booty stash, and we want to add a period of 2 years and 3 months to it. We can do this easily by calling the plus() method and passing in our Period object:

LocalDate bootyStash = LocalDate.of(1720, Month.JUNE, 15);
Period twoYearsThreeMonths = Period.of(2, 3, 0);

LocalDate newDate = bootyStash.plus(twoYearsThreeMonths);

In this example, we’re adding our Period of 2 years and 3 months to the bootyStash date using the plus() method. This creates a new LocalDate object that represents the updated date.

And just like that, we’ve successfully added a period of time to a date object! The Period class allows us to easily define and manipulate time spans, giving us more control over our program’s behavior.

But what if we want to subtract a period of time from a date? Stay tuned, me hearties! We’ll be covering that and more in our next article on working with periods. Until then, keep practicing your Java DateTime skills and keep the plunder coming!

Obtaining information from periods

Now that we know how to create a Period object, let’s take a look at how we can obtain information from it. The Period class provides us with several methods to help us retrieve the values of years, months, and days:

Period twoYearsThreeMonthsOneDay = Period.of(2, 3, 1);

int years = twoYearsThreeMonthsOneDay.getYears();
int months = twoYearsThreeMonthsOneDay.getMonths();
int days = twoYearsThreeMonthsOneDay.getDays();

System.out.println(years + " years, " + months + " months, " + days + " days");

In this example, we’re creating a Period object of 2 years, 3 months, and 1 day. We then use the getYears(), getMonths(), and getDays() methods to retrieve the values of each unit of time. Finally, we print out the values using a println() statement.

This will output:

2 years, 3 months, 1 days

We can also check if a Period object is zero, which means it has no time values:

Period zeroPeriod = Period.ZERO;

boolean isZero = zeroPeriod.isZero();
System.out.println(isZero);

In this example, we’re creating a Period object with zero years, months, and days using the ZERO constant. We then use the isZero() method to check if the period is zero. Finally, we print out the boolean value using a println() statement.

This will output:

true

Now that we know how to create, add, subtract, and retrieve information from a Period object, we have all the tools we need to confidently work with time spans in our Java programs.

Conclusion

Congratulations, me hearties! We’ve successfully navigated through the choppy waters of creating and initializing periods in the Java DateTime library. We’ve learned how to create a Period object using different units of time, add it to a LocalDate object, and retrieve information from it. With these skills under our belts, we’re one step closer to becoming masters of the Java DateTime library.

But our journey is far from over! There’s still much to learn about working with periods, including how to subtract a period from a date and how to work with Duration objects. So hoist the sails, me hearties, and let’s set our sights on our next adventure in the Java DateTime seas!