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

Using the Period Class to Represent a Period of Time

Header Image

Ahoy mateys! Are you ready to set sail on a voyage through time? Well, you’re in luck because today we’re going to be discussing how to use the Period class in JodaTime to represent a period of time.

The Period Class

First things first, let’s talk about the Period class. The Period class is part of the JodaTime library, which is a powerful tool for working with dates and times in Java. The Period class allows us to represent a period of time, such as “3 days” or “1 hour and 30 minutes”.

To use the Period class, we need to import it along with the other necessary JodaTime classes. Here’s an example of how to do that:

import org.joda.time.Period;
import org.joda.time.PeriodType;

Once we’ve imported the Period class, we can create a new Period object like this:

Period threeDays = Period.days(3);

In this example, we’ve created a new Period object called threeDays that represents a period of 3 days. We can also create Period objects that represent other periods of time, such as hours, minutes, or seconds. Here are some examples:

Period twoHours = Period.hours(2);
Period fifteenMinutes = Period.minutes(15);
Period thirtySeconds = Period.seconds(30);

Adding a Period of Time

Now that we know how to create Period objects, let’s talk about how to add a period of time to a DateTime object. To do this, we simply use the plus() method on the DateTime object and pass in the Period object that represents the amount of time we want to add. Here’s an example:

DateTime now = new DateTime();
DateTime twoHoursLater = now.plus(Period.hours(2));

In this example, we’ve created a new DateTime object called now that represents the current date and time. We’ve then created a new DateTime object called twoHoursLater that represents the date and time that is 2 hours later than the current date and time.

We can also add more than one period of time to a DateTime object. Here’s an example:

DateTime now = new DateTime();
Period twoHours = Period.hours(2);
Period fifteenMinutes = Period.minutes(15);
DateTime twoHoursAndFifteenMinutesLater = now.plus(twoHours).plus(fifteenMinutes);

In this example, we’ve created a new DateTime object called twoHoursAndFifteenMinutesLater that represents the date and time that is 2 hours and 15 minutes later than the current date and time.

Subtracting a Period of Time

We can also subtract a period of time from a DateTime object using the minus() method. Here’s an example:

DateTime now = new DateTime();
DateTime twoHoursAgo = now.minus(Period.hours(2));

In this example, we’ve created a new DateTime object called twoHoursAgo that represents the date and time that is 2 hours earlier than the current date and time.

We can also subtract more than one period of time from a DateTime object. Here’s an example:

DateTime now = new DateTime();
Period twoHours = Period.hours(2);
Period fifteenMinutes = Period.minutes(15);
DateTime twoHoursAndFifteenMinutesAgo = now.minus(twoHours).minus(fifteenMinutes);

In this example, we’ve created a new DateTime object called twoHoursAndFifteenMinutesAgo that represents the date and time that is 2 hoursand 15 minutes earlier than the current date and time.

Conclusion

And there you have it, mateys! Using the Period class in JodaTime is a simple and powerful way to represent a period of time and add or subtract it from a DateTime object. With just a few lines of code, you can travel through time and arrive at any date and time you desire.

Keep in mind that JodaTime is a great alternative to the standard Java Date and Time classes, providing additional features and benefits. We hope you’ve found this article informative and entertaining. Until next time, keep sailing and coding!

Additional Resources