JodaTime SingleFieldPeriods

Overview

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:

  • Years
  • Months
  • Days
  • Weeks
  • Hours
  • Minutes
  • Seconds

In this article we shall see how to use these Single Field Period classes. This article shall elaborate on using the
Days
class. The same usage idioms can be applied for all other Single Field Periods.

  1. Calculate Number Of Days
  2. Perform Arithmetic Operations On Single Field Periods
  3. Parsing ISO 8601 Format String For Period
  4. 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:

  1. Using Days.daysBetween(): When two DateTime instances are given
  2. 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:

  1. plus() : Addition
  2. minus() : Subtraction
  3. multiply() : Multiplication
  4. 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:

P[date_fields]T[time_fields]

P
nYnMnD
T
nHnMnS
where n represents the value of the date or time field and the meaning of other letters is as per the table below:

Letter Meaning
P Period
Y Year
M Month
D Day
T Time Field
H Hour
M Minute
S Second


Convert To Other Standard Representations Like Duration, Hour, Minute And Second

The
Days
class can be converted to other standard representations as shown below.

The Output is shown in ISO 8601 format discussed in
this article


To start, click on the next arrow. If you find anything missing in the tutorials then please drop in your comments.

Leave a Reply

Your email address will not be published. Required fields are marked *