Package org.springframework.format.datetime.standard

# Class DurationFormatter

java.lang.Object
org.springframework.format.datetime.standard.DurationFormatter

All Implemented Interfaces:
  `Formatter<Duration>`, `Parser<Duration>`, `Printer<Duration>`

public class DurationFormatter extends Object implements Formatter<Duration>
`Formatter` implementation for a JSR-310 `Duration`, following JSR-310's parsing rules for a Duration by default and supporting additional `DurationFormat.Style` styles.

Since:
  6.2
Author:
  Juergen Hoeller
See Also:
  - `DurationFormatterUtils`
  - `DurationFormat.Style`

## Constructor Summary

Constructors
Constructor
Description
`DurationFormatter(DurationFormat.Style style)`
Create a `DurationFormatter` in a specific `DurationFormat.Style`.
`DurationFormatter(DurationFormat.Style style, DurationFormat.Unit defaultUnit)`
Create a `DurationFormatter` in a specific `DurationFormat.Style` with an optional `DurationFormat.Unit`.

## Method Summary

Modifier and Type
Method
Description
`Duration`
`parse(String text, Locale locale)`
Parse a text String to produce a T.
`String`
`print(Duration object, Locale locale)`
Print the object of type T for display.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### DurationFormatter

public DurationFormatter(DurationFormat.Style style)
Create a `DurationFormatter` in a specific `DurationFormat.Style`.

When a unit is needed but cannot be determined (for example, printing a Duration in the `SIMPLE` style), `DurationFormat.Unit#MILLIS` is used.

### DurationFormatter

public DurationFormatter(DurationFormat.Style style, @Nullable DurationFormat.Unit defaultUnit)
Create a `DurationFormatter` in a specific `DurationFormat.Style` with an optional `DurationFormat.Unit`.

If a `defaultUnit` is specified, it may be used in parsing cases when no unit is present in the string (provided the style allows for such a case). It will also be used as the representation's resolution when printing in the `DurationFormat.Style.SIMPLE` style. Otherwise, the style defines its default unit.

Parameters:
  `style` - the `DurationStyle` to use
  `defaultUnit` - the `DurationFormat.Unit` to fall back to when parsing and printing

## Method Details

### parse

public Duration parse(String text, Locale locale) throws ParseException
Description copied from interface: `Parser`
Parse a text String to produce a T.

Specified by:
  `parse` in interface `Parser<Duration>`
Parameters:
  `text` - the text string
  `locale` - the current user locale
Returns:
  an instance of T
Throws:
  `ParseException` - when a parse exception occurs in a java.text parsing library

### print

public String print(Duration object, Locale locale)
Description copied from interface: `Printer`
Print the object of type T for display.

Specified by:
  `print` in interface `Printer<Duration>`
Parameters:
  `object` - the instance to print
  `locale` - the current user locale
Returns:
  the printed text string