Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.joda.time.format
Class PeriodFormatter  view PeriodFormatter download PeriodFormatter.java

java.lang.Object
  extended byorg.joda.time.format.PeriodFormatter

public class PeriodFormatter
extends java.lang.Object

Controls the printing and parsing of a time period to and from a string.

This class is the main API for printing and parsing used by most applications. Instances of this class are created via one of three factory classes:

An instance of this class holds a reference internally to one printer and one parser. It is possible that one of these may be null, in which case the formatter cannot print/parse. This can be checked via the isPrinter() 55 and isParser() 55 methods.

The underlying printer/parser can be altered to behave exactly as required by using a decorator modifier:

This returns a new formatter (instances of this class are immutable).

The main methods of the class are the printXxx and parseXxx methods. These are used as follows:

 // print using the default locale
 String periodStr = formatter.print(period);
 // print using the French locale
 String periodStr = formatter.withLocale(Locale.FRENCH).print(period);
 
 // parse using the French locale
 Period date = formatter.withLocale(Locale.FRENCH).parsePeriod(str);
 

Since:
1.0

Field Summary
private  java.util.Locale iLocale
          The locale to use for printing and parsing.
private  PeriodParser iParser
          The internal parser used to output the datetime.
private  org.joda.time.PeriodType iParseType
          The period type used in parsing.
private  PeriodPrinter iPrinter
          The internal printer used to output the datetime.
 
Constructor Summary
  PeriodFormatter(PeriodPrinter printer, PeriodParser parser)
          Creates a new formatter, however you will normally use the factory or the builder.
private PeriodFormatter(PeriodPrinter printer, PeriodParser parser, java.util.Locale locale, org.joda.time.PeriodType type)
          Constructor.
 
Method Summary
private  void checkParser()
          Checks whether parsing is supported.
private  void checkPeriod(org.joda.time.ReadablePeriod period)
          Checks whether the period is non-null.
private  void checkPrinter()
          Checks whether printing is supported.
 java.util.Locale getLocale()
          Gets the locale that will be used for printing and parsing.
 PeriodParser getParser()
          Gets the internal parser object that performs the real parsing work.
 org.joda.time.PeriodType getParseType()
          Gets the PeriodType that will be used for parsing.
 PeriodPrinter getPrinter()
          Gets the internal printer object that performs the real printing work.
 boolean isParser()
          Is this formatter capable of parsing.
 boolean isPrinter()
          Is this formatter capable of printing.
 int parseInto(org.joda.time.ReadWritablePeriod period, java.lang.String text, int position)
          Parses a period from the given text, at the given position, saving the result into the fields of the given ReadWritablePeriod.
 org.joda.time.MutablePeriod parseMutablePeriod(java.lang.String text)
          Parses a period from the given text, returning a new MutablePeriod.
 org.joda.time.Period parsePeriod(java.lang.String text)
          Parses a period from the given text, returning a new Period.
 java.lang.String print(org.joda.time.ReadablePeriod period)
          Prints a ReadablePeriod to a new String.
 void printTo(java.lang.StringBuffer buf, org.joda.time.ReadablePeriod period)
          Prints a ReadablePeriod to a StringBuffer.
 void printTo(java.io.Writer out, org.joda.time.ReadablePeriod period)
          Prints a ReadablePeriod to a Writer.
 PeriodFormatter withLocale(java.util.Locale locale)
          Returns a new formatter with a different locale that will be used for printing and parsing.
 PeriodFormatter withParseType(org.joda.time.PeriodType type)
          Returns a new formatter with a different PeriodType for parsing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

iPrinter

private final PeriodPrinter iPrinter
The internal printer used to output the datetime.


iParser

private final PeriodParser iParser
The internal parser used to output the datetime.


iLocale

private final java.util.Locale iLocale
The locale to use for printing and parsing.


iParseType

private final org.joda.time.PeriodType iParseType
The period type used in parsing.

Constructor Detail

PeriodFormatter

public PeriodFormatter(PeriodPrinter printer,
                       PeriodParser parser)
Creates a new formatter, however you will normally use the factory or the builder.


PeriodFormatter

private PeriodFormatter(PeriodPrinter printer,
                        PeriodParser parser,
                        java.util.Locale locale,
                        org.joda.time.PeriodType type)
Constructor.

Method Detail

isPrinter

public boolean isPrinter()
Is this formatter capable of printing.


getPrinter

public PeriodPrinter getPrinter()
Gets the internal printer object that performs the real printing work.


isParser

public boolean isParser()
Is this formatter capable of parsing.


getParser

public PeriodParser getParser()
Gets the internal parser object that performs the real parsing work.


withLocale

public PeriodFormatter withLocale(java.util.Locale locale)
Returns a new formatter with a different locale that will be used for printing and parsing.

A PeriodFormatter is immutable, so a new instance is returned, and the original is unaltered and still usable.


getLocale

public java.util.Locale getLocale()
Gets the locale that will be used for printing and parsing.


withParseType

public PeriodFormatter withParseType(org.joda.time.PeriodType type)
Returns a new formatter with a different PeriodType for parsing.

A PeriodFormatter is immutable, so a new instance is returned, and the original is unaltered and still usable.


getParseType

public org.joda.time.PeriodType getParseType()
Gets the PeriodType that will be used for parsing.


printTo

public void printTo(java.lang.StringBuffer buf,
                    org.joda.time.ReadablePeriod period)
Prints a ReadablePeriod to a StringBuffer.


printTo

public void printTo(java.io.Writer out,
                    org.joda.time.ReadablePeriod period)
             throws java.io.IOException
Prints a ReadablePeriod to a Writer.


print

public java.lang.String print(org.joda.time.ReadablePeriod period)
Prints a ReadablePeriod to a new String.


checkPrinter

private void checkPrinter()
Checks whether printing is supported.


checkPeriod

private void checkPeriod(org.joda.time.ReadablePeriod period)
Checks whether the period is non-null.


parseInto

public int parseInto(org.joda.time.ReadWritablePeriod period,
                     java.lang.String text,
                     int position)
Parses a period from the given text, at the given position, saving the result into the fields of the given ReadWritablePeriod. If the parse succeeds, the return value is the new text position. Note that the parse may succeed without fully reading the text.

The parse type of the formatter is not used by this method.

If it fails, the return value is negative, but the period may still be modified. To determine the position where the parse failed, apply the one's complement operator (~) on the return value.


parsePeriod

public org.joda.time.Period parsePeriod(java.lang.String text)
Parses a period from the given text, returning a new Period.


parseMutablePeriod

public org.joda.time.MutablePeriod parseMutablePeriod(java.lang.String text)
Parses a period from the given text, returning a new MutablePeriod.


checkParser

private void checkParser()
Checks whether parsing is supported.