Single Field Period is defined as Period using a single date or time field like , etc. In some use cases, date and time related manipulations need to be performed on a single field. Joda-Time provides the following convenience classes to directly represent the single field periods:
In this article we shall see how to use these Single Field Period classes. This article shall elaborate on using the
class. The same usage idioms can be applied for all other Single Field Periods.
- Calculate Number Of Days
- Perform Arithmetic Operations On Single Field Periods
- Parsing ISO 8601 Format String For Period
- Convert To Other Standard Representations Like Duration, Hour, Minute And Second
Calculate Number Of Days
The number of days can be calculated in the following two ways:
- Using Days.daysBetween(): When two DateTime instances are given
- Using Days.daysIn(): When an Interval is given
Perform Arithmetic Operations On Single Field Periods
The following methods provide facility to perform arithmetic operations on periods:
- plus() : Addition
- minus() : Subtraction
- multiply() : Multiplication
- negated() : Negative Sign
Parsing ISO 8601 Format String For Period
ISO 8601 is a standard for representing date and time related data.
The ISO 8601 format for representing a period is as follows:
where n represents the value of the date or time field and the meaning of other letters is as per the table below:
Convert To Other Standard Representations Like Duration, Hour, Minute And Second
class can be converted to other standard representations as shown below.
The Output is shown in ISO 8601 format discussed in
To start, click on the next arrow. If you find anything missing in the tutorials then please drop in your comments.