JodaTime Miscellaneous

Overview

This article covers miscellaneous How-Tos for Joda-Time.

  1. Convert 2 Digit Year To 4 Digit Year Using Pivot Year
  2. Get List Of Default Time Zones
  3. Verify Whether A Partial DateTime Is Contiguous

Convert 2 Digit Year To 4 Digit Year Using Pivot Year

In Joda-Time, it is possible to convert 2 digit year to 4 digit year using the concept of ‘Pivot Year’.

The Pivot year defines the range of supported years using the following formula:

range = (pivot year – 50) to (pivot year + 49)

Hence if the pivot year is 1975, the supported range is 1925 to 2024.

To convert from 2 digit year to 4 digit year, the appropriate digits are prefixed to the 2 digit year from the supported range based on the pivot year.

e.g. If the pivot year is 1975, then

2 digit year Corresponding 4 digit year
00 2000
20 2020
40 1940
60 1960
80 1980


Get List Of Default Time Zones

Use
DateTimeUtils.getDefaultTimeZoneNames()
for fetching a list of default time zones.


Verify Whether A Partial DateTime Is Contiguous

A date can be defined as a partial. See
here
for further details.

The fields defined in a Partial DateTime can be contiguous (i.e. adjacent to each other) or non-contiguous (i.e. not adjacent to each other).

e.g. The fields year and month are contiguous. However the fields year and hour are not contiguous.


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 *