org.joda.time.format
Class PeriodFormat

java.lang.Object
  extended by org.joda.time.format.PeriodFormat

public class PeriodFormat
extends Object

Factory that creates instances of PeriodFormatter.

Period formatting is performed by the PeriodFormatter class. Three classes provide factory methods to create formatters, and this is one. The others are ISOPeriodFormat and PeriodFormatterBuilder.

PeriodFormat is thread-safe and immutable, and the formatters it returns are as well.

Since:
1.0
See Also:
ISOPeriodFormat, PeriodFormatterBuilder

Constructor Summary
protected PeriodFormat()
          Constructor.
 
Method Summary
static PeriodFormatter getDefault()
          Gets the default formatter that outputs words in English.
static PeriodFormatter wordBased()
          Returns a word based formatter for the JDK default locale.
static PeriodFormatter wordBased(Locale locale)
          Returns a word based formatter for the specified locale.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PeriodFormat

protected PeriodFormat()
Constructor.

Since:
1.1 (previously private)
Method Detail

getDefault

public static PeriodFormatter getDefault()
Gets the default formatter that outputs words in English.

This calls wordBased(Locale) using a locale of ENGLISH.

Returns:
the formatter, not null

wordBased

public static PeriodFormatter wordBased()
Returns a word based formatter for the JDK default locale.

This calls wordBased(Locale) using the default locale.

Returns:
the formatter, not null
Since:
2.0

wordBased

public static PeriodFormatter wordBased(Locale locale)
Returns a word based formatter for the specified locale.

The words are configured in a resource bundle text file - org.joda.time.format.messages. This can be added to via the normal classpath resource bundle mechanisms.

You can add your own translation by creating messages_.properties file and adding it to the org.joda.time.format.messages path.

Simple example (1 -> singular suffix, not 1 -> plural suffix):

 PeriodFormat.space=\ 
 PeriodFormat.comma=,
 PeriodFormat.commandand=,and 
 PeriodFormat.commaspaceand=, and 
 PeriodFormat.commaspace=, 
 PeriodFormat.spaceandspace=\ and 
 PeriodFormat.year=\ year
 PeriodFormat.years=\ years
 PeriodFormat.month=\ month
 PeriodFormat.months=\ months
 PeriodFormat.week=\ week
 PeriodFormat.weeks=\ weeks
 PeriodFormat.day=\ day
 PeriodFormat.days=\ days
 PeriodFormat.hour=\ hour
 PeriodFormat.hours=\ hours
 PeriodFormat.minute=\ minute
 PeriodFormat.minutes=\ minutes
 PeriodFormat.second=\ second
 PeriodFormat.seconds=\ seconds
 PeriodFormat.millisecond=\ millisecond
 PeriodFormat.milliseconds=\ milliseconds
 

Some languages contain more than two suffixes. You can use regular expressions for them. Here's an example using regular expression for English:

 PeriodFormat.space=\ 
 PeriodFormat.comma=,
 PeriodFormat.commandand=,and 
 PeriodFormat.commaspaceand=, and 
 PeriodFormat.commaspace=, 
 PeriodFormat.spaceandspace=\ and 
 PeriodFormat.regex.separator=%
 PeriodFormat.years.regex=1$%.*
 PeriodFormat.years.list=\ year%\ years
 PeriodFormat.months.regex=1$%.*
 PeriodFormat.months.list=\ month%\ months
 PeriodFormat.weeks.regex=1$%.*
 PeriodFormat.weeks.list=\ week%\ weeks
 PeriodFormat.days.regex=1$%.*
 PeriodFormat.days.list=\ day%\ days
 PeriodFormat.hours.regex=1$%.*
 PeriodFormat.hours.list=\ hour%\ hours
 PeriodFormat.minutes.regex=1$%.*
 PeriodFormat.minutes.list=\ minute%\ minutes
 PeriodFormat.seconds.regex=1$%.*
 PeriodFormat.seconds.list=\ second%\ seconds
 PeriodFormat.milliseconds.regex=1$%.*
 PeriodFormat.milliseconds.list=\ millisecond%\ milliseconds
 

You can mix both approaches. Here's example for Polish ( "1 year, 2 years, 5 years, 12 years, 15 years, 21 years, 22 years, 25 years" translates to "1 rok, 2 lata, 5 lat, 12 lat, 15 lat, 21 lat, 22 lata, 25 lat"). Notice that PeriodFormat.day and PeriodFormat.days is used for day suffixes as there is no need for regular expressions:

 PeriodFormat.space=\ 
 PeriodFormat.comma=,
 PeriodFormat.commandand=,i 
 PeriodFormat.commaspaceand=, i 
 PeriodFormat.commaspace=, 
 PeriodFormat.spaceandspace=\ i 
 PeriodFormat.regex.separator=%
 PeriodFormat.years.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
 PeriodFormat.years.list=\ rok%\ lata%\ lat
 PeriodFormat.months.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
 PeriodFormat.months.list=\ miesiąc%\ miesiące%\ miesięcy
 PeriodFormat.weeks.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
 PeriodFormat.weeks.list=\ tydzień%\ tygodnie%\ tygodni
 PeriodFormat.day=\ dzień
 PeriodFormat.days=\ dni
 PeriodFormat.hours.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
 PeriodFormat.hours.list=\ godzina%\ godziny%\ godzin
 PeriodFormat.minutes.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
 PeriodFormat.minutes.list=\ minuta%\ minuty%\ minut
 PeriodFormat.seconds.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
 PeriodFormat.seconds.list=\ sekunda%\ sekundy%\ sekund
 PeriodFormat.milliseconds.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
 PeriodFormat.milliseconds.list=\ milisekunda%\ milisekundy%\ milisekund
 

Each PeriodFormat.<duration_field_type>.regex property stands for an array of regular expressions and is followed by a property PeriodFormat.<duration_field_type>.list holding an array of suffixes. PeriodFormat.regex.separator is used for splitting. See PeriodFormatterBuilder.appendSuffix(String[], String[]) for details.

Available languages are English, Danish, Dutch, French, German, Japanese, Polish, Portuguese and Spanish.

Returns:
the formatter, not null
Since:
2.0, regex since 2.5


Copyright © 2002–2015 Joda.org. All rights reserved.