Class DateTimeFormatter
- java.lang.Object
-
- org.joda.time.format.DateTimeFormatter
-
public class DateTimeFormatter extends java.lang.Object
Controls the printing and parsing of a datetime 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:
DateTimeFormat
- formats by pattern and styleISODateTimeFormat
- ISO8601 formatsDateTimeFormatterBuilder
- complex formats created via method calls
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()
andisParser()
methods.The underlying printer/parser can be altered to behave exactly as required by using one of the decorator modifiers:
withLocale(Locale)
- returns a new formatter that uses the specified localewithZone(DateTimeZone)
- returns a new formatter that uses the specified time zonewithChronology(Chronology)
- returns a new formatter that uses the specified chronologywithOffsetParsed()
- returns a new formatter that returns the parsed time zone offsetwithPivotYear(int)
- returns a new formatter with the specified pivot yearwithDefaultYear(int)
- returns a new formatter with the specified default year
The main methods of the class are the
printXxx
andparseXxx
methods. These are used as follows:// print using the defaults (default locale, chronology/zone of the datetime) String dateStr = formatter.print(dt); // print using the French locale String dateStr = formatter.withLocale(Locale.FRENCH).print(dt); // print using the UTC zone String dateStr = formatter.withZone(DateTimeZone.UTC).print(dt); // parse using the Paris zone DateTime date = formatter.withZone(DateTimeZone.forID("Europe/Paris")).parseDateTime(str);
Parsing builds up the resultant instant by 'setting' the value of each parsed field from largest to smallest onto an initial instant, typically 1970-01-01T00:00Z. This design means that day-of-month is set before day-of-week. As such, if both the day-of-month and day-of-week are parsed, and the day-of-week is incorrect, then the day-of-week overrides the day-of-month. This has a side effect if the input is not consistent.
- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description DateTimeFormatter(DateTimePrinter printer, DateTimeParser parser)
Creates a new formatter, however you will normally use the factory or the builder.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Chronology
getChronolgy()
Deprecated.Use the method with the correct spellingChronology
getChronology()
Gets the chronology to use as an override.int
getDefaultYear()
Gets the default year for parsing months and days.java.util.Locale
getLocale()
Gets the locale that will be used for printing and parsing.DateTimeParser
getParser()
Gets the internal parser object that performs the real parsing work.java.lang.Integer
getPivotYear()
Gets the pivot year to use as an override.DateTimePrinter
getPrinter()
Gets the internal printer object that performs the real printing work.DateTimeZone
getZone()
Gets the zone to use as an override.boolean
isOffsetParsed()
Checks whether the offset from the string is used as the zone of the parsed datetime.boolean
isParser()
Is this formatter capable of parsing.boolean
isPrinter()
Is this formatter capable of printing.DateTime
parseDateTime(java.lang.String text)
Parses a date-time from the given text, returning a new DateTime.int
parseInto(ReadWritableInstant instant, java.lang.String text, int position)
Parses a datetime from the given text, at the given position, saving the result into the fields of the given ReadWritableInstant.LocalDate
parseLocalDate(java.lang.String text)
Parses only the local date from the given text, returning a new LocalDate.LocalDateTime
parseLocalDateTime(java.lang.String text)
Parses only the local date-time from the given text, returning a new LocalDateTime.LocalTime
parseLocalTime(java.lang.String text)
Parses only the local time from the given text, returning a new LocalTime.long
parseMillis(java.lang.String text)
Parses a datetime from the given text, returning the number of milliseconds since the epoch, 1970-01-01T00:00:00Z.MutableDateTime
parseMutableDateTime(java.lang.String text)
Parses a date-time from the given text, returning a new MutableDateTime.java.lang.String
print(long instant)
Prints a millisecond instant to a String.java.lang.String
print(ReadableInstant instant)
Prints a ReadableInstant to a String.java.lang.String
print(ReadablePartial partial)
Prints a ReadablePartial to a new String.void
printTo(java.io.Writer out, long instant)
Prints an instant from milliseconds since 1970-01-01T00:00:00Z, using ISO chronology in the default DateTimeZone.void
printTo(java.io.Writer out, ReadableInstant instant)
Prints a ReadableInstant, using the chronology supplied by the instant.void
printTo(java.io.Writer out, ReadablePartial partial)
Prints a ReadablePartial.void
printTo(java.lang.Appendable appendable, long instant)
Prints an instant from milliseconds since 1970-01-01T00:00:00Z, using ISO chronology in the default DateTimeZone.void
printTo(java.lang.Appendable appendable, ReadableInstant instant)
Prints a ReadableInstant, using the chronology supplied by the instant.void
printTo(java.lang.Appendable appendable, ReadablePartial partial)
Prints a ReadablePartial.void
printTo(java.lang.StringBuffer buf, long instant)
Prints an instant from milliseconds since 1970-01-01T00:00:00Z, using ISO chronology in the default DateTimeZone.void
printTo(java.lang.StringBuffer buf, ReadableInstant instant)
Prints a ReadableInstant, using the chronology supplied by the instant.void
printTo(java.lang.StringBuffer buf, ReadablePartial partial)
Prints a ReadablePartial.void
printTo(java.lang.StringBuilder buf, long instant)
Prints an instant from milliseconds since 1970-01-01T00:00:00Z, using ISO chronology in the default DateTimeZone.void
printTo(java.lang.StringBuilder buf, ReadableInstant instant)
Prints a ReadableInstant, using the chronology supplied by the instant.void
printTo(java.lang.StringBuilder buf, ReadablePartial partial)
Prints a ReadablePartial.DateTimeFormatter
withChronology(Chronology chrono)
Returns a new formatter that will use the specified chronology in preference to that of the printed object, or ISO on a parse.DateTimeFormatter
withDefaultYear(int defaultYear)
Returns a new formatter that will use the specified default year.DateTimeFormatter
withLocale(java.util.Locale locale)
Returns a new formatter with a different locale that will be used for printing and parsing.DateTimeFormatter
withOffsetParsed()
Returns a new formatter that will create a datetime with a time zone equal to that of the offset of the parsed string.DateTimeFormatter
withPivotYear(int pivotYear)
Returns a new formatter that will use the specified pivot year for two digit year parsing in preference to that stored in the parser.DateTimeFormatter
withPivotYear(java.lang.Integer pivotYear)
Returns a new formatter that will use the specified pivot year for two digit year parsing in preference to that stored in the parser.DateTimeFormatter
withZone(DateTimeZone zone)
Returns a new formatter that will use the specified zone in preference to the zone of the printed object, or default zone on a parse.DateTimeFormatter
withZoneUTC()
Returns a new formatter that will use the UTC zone in preference to the zone of the printed object, or default zone on a parse.
-
-
-
Constructor Detail
-
DateTimeFormatter
public DateTimeFormatter(DateTimePrinter printer, DateTimeParser parser)
Creates a new formatter, however you will normally use the factory or the builder.- Parameters:
printer
- the internal printer, null if cannot printparser
- the internal parser, null if cannot parse
-
-
Method Detail
-
isPrinter
public boolean isPrinter()
Is this formatter capable of printing.- Returns:
- true if this is a printer
-
getPrinter
public DateTimePrinter getPrinter()
Gets the internal printer object that performs the real printing work.- Returns:
- the internal printer; is null if printing not supported
-
isParser
public boolean isParser()
Is this formatter capable of parsing.- Returns:
- true if this is a parser
-
getParser
public DateTimeParser getParser()
Gets the internal parser object that performs the real parsing work.- Returns:
- the internal parser; is null if parsing not supported
-
withLocale
public DateTimeFormatter withLocale(java.util.Locale locale)
Returns a new formatter with a different locale that will be used for printing and parsing.A DateTimeFormatter is immutable, so a new instance is returned, and the original is unaltered and still usable.
- Parameters:
locale
- the locale to use; if null, formatter uses default locale at invocation time- Returns:
- the new formatter
-
getLocale
public java.util.Locale getLocale()
Gets the locale that will be used for printing and parsing.- Returns:
- the locale to use; if null, formatter uses default locale at invocation time
-
withOffsetParsed
public DateTimeFormatter withOffsetParsed()
Returns a new formatter that will create a datetime with a time zone equal to that of the offset of the parsed string.After calling this method, a string '2004-06-09T10:20:30-08:00' will create a datetime with a zone of -08:00 (a fixed zone, with no daylight savings rules). If the parsed string represents a local time (no zone offset) the parsed datetime will be in the default zone.
Calling this method sets the override zone to null. Calling the override zone method sets this flag off.
- Returns:
- the new formatter
-
isOffsetParsed
public boolean isOffsetParsed()
Checks whether the offset from the string is used as the zone of the parsed datetime.- Returns:
- true if the offset from the string is used as the zone
-
withChronology
public DateTimeFormatter withChronology(Chronology chrono)
Returns a new formatter that will use the specified chronology in preference to that of the printed object, or ISO on a parse.When printing, this chronology will be used in preference to the chronology from the datetime that would otherwise be used.
When parsing, this chronology will be set on the parsed datetime.
A null chronology means no-override. If both an override chronology and an override zone are set, the override zone will take precedence over the zone in the chronology.
- Parameters:
chrono
- the chronology to use as an override- Returns:
- the new formatter
-
getChronology
public Chronology getChronology()
Gets the chronology to use as an override.- Returns:
- the chronology to use as an override
-
getChronolgy
@Deprecated public Chronology getChronolgy()
Deprecated.Use the method with the correct spellingGets the chronology to use as an override.- Returns:
- the chronology to use as an override
-
withZoneUTC
public DateTimeFormatter withZoneUTC()
Returns a new formatter that will use the UTC zone in preference to the zone of the printed object, or default zone on a parse.When printing, UTC will be used in preference to the zone from the datetime that would otherwise be used.
When parsing, UTC will be set on the parsed datetime.
If both an override chronology and an override zone are set, the override zone will take precedence over the zone in the chronology.
- Returns:
- the new formatter, never null
- Since:
- 2.0
-
withZone
public DateTimeFormatter withZone(DateTimeZone zone)
Returns a new formatter that will use the specified zone in preference to the zone of the printed object, or default zone on a parse.When printing, this zone will be used in preference to the zone from the datetime that would otherwise be used.
When parsing, this zone will be set on the parsed datetime.
A null zone means of no-override. If both an override chronology and an override zone are set, the override zone will take precedence over the zone in the chronology.
- Parameters:
zone
- the zone to use as an override- Returns:
- the new formatter
-
getZone
public DateTimeZone getZone()
Gets the zone to use as an override.- Returns:
- the zone to use as an override
-
withPivotYear
public DateTimeFormatter withPivotYear(java.lang.Integer pivotYear)
Returns a new formatter that will use the specified pivot year for two digit year parsing in preference to that stored in the parser.This setting is useful for changing the pivot year of formats built using a pattern -
DateTimeFormat.forPattern(String)
.When parsing, this pivot year is used. Null means no-override. There is no effect when printing.
The pivot year enables a two digit year to be converted to a four digit year. The pivot represents the year in the middle of the supported range of years. Thus the full range of years that will be built is
(pivot - 50) .. (pivot + 49)
.pivot supported range 00 is 20 is 40 is 60 is 80 is --------------------------------------------------------------- 1950 1900..1999 1900 1920 1940 1960 1980 1975 1925..2024 2000 2020 1940 1960 1980 2000 1950..2049 2000 2020 2040 1960 1980 2025 1975..2074 2000 2020 2040 2060 1980 2050 2000..2099 2000 2020 2040 2060 2080
- Parameters:
pivotYear
- the pivot year to use as an override when parsing- Returns:
- the new formatter
- Since:
- 1.1
-
withPivotYear
public DateTimeFormatter withPivotYear(int pivotYear)
Returns a new formatter that will use the specified pivot year for two digit year parsing in preference to that stored in the parser.This setting is useful for changing the pivot year of formats built using a pattern -
DateTimeFormat.forPattern(String)
.When parsing, this pivot year is used. There is no effect when printing.
The pivot year enables a two digit year to be converted to a four digit year. The pivot represents the year in the middle of the supported range of years. Thus the full range of years that will be built is
(pivot - 50) .. (pivot + 49)
.pivot supported range 00 is 20 is 40 is 60 is 80 is --------------------------------------------------------------- 1950 1900..1999 1900 1920 1940 1960 1980 1975 1925..2024 2000 2020 1940 1960 1980 2000 1950..2049 2000 2020 2040 1960 1980 2025 1975..2074 2000 2020 2040 2060 1980 2050 2000..2099 2000 2020 2040 2060 2080
- Parameters:
pivotYear
- the pivot year to use as an override when parsing- Returns:
- the new formatter
- Since:
- 1.1
-
getPivotYear
public java.lang.Integer getPivotYear()
Gets the pivot year to use as an override.- Returns:
- the pivot year to use as an override
- Since:
- 1.1
-
withDefaultYear
public DateTimeFormatter withDefaultYear(int defaultYear)
Returns a new formatter that will use the specified default year.The default year is used when parsing in the case where there is a month or a day but not a year. Specifically, it is used if there is a field parsed with a duration between the length of a month and the length of a day inclusive.
This value is typically used to move the year from 1970 to a leap year to enable February 29th to be parsed. Unless customised, the year 2000 is used.
This setting has no effect when printing.
- Parameters:
defaultYear
- the default year to use- Returns:
- the new formatter, not null
- Since:
- 2.0
-
getDefaultYear
public int getDefaultYear()
Gets the default year for parsing months and days.- Returns:
- the default year for parsing months and days
- Since:
- 2.0
-
printTo
public void printTo(java.lang.StringBuffer buf, ReadableInstant instant)
Prints a ReadableInstant, using the chronology supplied by the instant.- Parameters:
buf
- the destination to format to, not nullinstant
- instant to format, null means now
-
printTo
public void printTo(java.lang.StringBuilder buf, ReadableInstant instant)
Prints a ReadableInstant, using the chronology supplied by the instant.- Parameters:
buf
- the destination to format to, not nullinstant
- instant to format, null means now
-
printTo
public void printTo(java.io.Writer out, ReadableInstant instant) throws java.io.IOException
Prints a ReadableInstant, using the chronology supplied by the instant.- Parameters:
out
- the destination to format to, not nullinstant
- instant to format, null means now- Throws:
java.io.IOException
-
printTo
public void printTo(java.lang.Appendable appendable, ReadableInstant instant) throws java.io.IOException
Prints a ReadableInstant, using the chronology supplied by the instant.- Parameters:
appendable
- the destination to format to, not nullinstant
- instant to format, null means now- Throws:
java.io.IOException
- Since:
- 2.0
-
printTo
public void printTo(java.lang.StringBuffer buf, long instant)
Prints an instant from milliseconds since 1970-01-01T00:00:00Z, using ISO chronology in the default DateTimeZone.- Parameters:
buf
- the destination to format to, not nullinstant
- millis since 1970-01-01T00:00:00Z
-
printTo
public void printTo(java.lang.StringBuilder buf, long instant)
Prints an instant from milliseconds since 1970-01-01T00:00:00Z, using ISO chronology in the default DateTimeZone.- Parameters:
buf
- the destination to format to, not nullinstant
- millis since 1970-01-01T00:00:00Z
-
printTo
public void printTo(java.io.Writer out, long instant) throws java.io.IOException
Prints an instant from milliseconds since 1970-01-01T00:00:00Z, using ISO chronology in the default DateTimeZone.- Parameters:
out
- the destination to format to, not nullinstant
- millis since 1970-01-01T00:00:00Z- Throws:
java.io.IOException
-
printTo
public void printTo(java.lang.Appendable appendable, long instant) throws java.io.IOException
Prints an instant from milliseconds since 1970-01-01T00:00:00Z, using ISO chronology in the default DateTimeZone.- Parameters:
appendable
- the destination to format to, not nullinstant
- millis since 1970-01-01T00:00:00Z- Throws:
java.io.IOException
- Since:
- 2.0
-
printTo
public void printTo(java.lang.StringBuffer buf, ReadablePartial partial)
Prints a ReadablePartial.Neither the override chronology nor the override zone are used by this method.
- Parameters:
buf
- the destination to format to, not nullpartial
- partial to format
-
printTo
public void printTo(java.lang.StringBuilder buf, ReadablePartial partial)
Prints a ReadablePartial.Neither the override chronology nor the override zone are used by this method.
- Parameters:
buf
- the destination to format to, not nullpartial
- partial to format
-
printTo
public void printTo(java.io.Writer out, ReadablePartial partial) throws java.io.IOException
Prints a ReadablePartial.Neither the override chronology nor the override zone are used by this method.
- Parameters:
out
- the destination to format to, not nullpartial
- partial to format- Throws:
java.io.IOException
-
printTo
public void printTo(java.lang.Appendable appendable, ReadablePartial partial) throws java.io.IOException
Prints a ReadablePartial.Neither the override chronology nor the override zone are used by this method.
- Parameters:
appendable
- the destination to format to, not nullpartial
- partial to format- Throws:
java.io.IOException
- Since:
- 2.0
-
print
public java.lang.String print(ReadableInstant instant)
Prints a ReadableInstant to a String.This method will use the override zone and the override chronology if they are set. Otherwise it will use the chronology and zone of the instant.
- Parameters:
instant
- instant to format, null means now- Returns:
- the printed result
-
print
public java.lang.String print(long instant)
Prints a millisecond instant to a String.This method will use the override zone and the override chronology if they are set. Otherwise it will use the ISO chronology and default zone.
- Parameters:
instant
- millis since 1970-01-01T00:00:00Z- Returns:
- the printed result
-
print
public java.lang.String print(ReadablePartial partial)
Prints a ReadablePartial to a new String.Neither the override chronology nor the override zone are used by this method.
- Parameters:
partial
- partial to format- Returns:
- the printed result
-
parseInto
public int parseInto(ReadWritableInstant instant, java.lang.String text, int position)
Parses a datetime from the given text, at the given position, saving the result into the fields of the given ReadWritableInstant. If the parse succeeds, the return value is the new text position. Note that the parse may succeed without fully reading the text and in this case those fields that were read will be set.Only those fields present in the string will be changed in the specified instant. All other fields will remain unaltered. Thus if the string only contains a year and a month, then the day and time will be retained from the input instant. If this is not the behaviour you want, then reset the fields before calling this method, or use
parseDateTime(String)
orparseMutableDateTime(String)
.If it fails, the return value is negative, but the instant may still be modified. To determine the position where the parse failed, apply the one's complement operator (~) on the return value.
This parse method ignores the
default year
and parses using the year from the supplied instant based on the chronology and time-zone of the supplied instant.The parse will use the chronology of the instant.
- Parameters:
instant
- an instant that will be modified, not nulltext
- the text to parseposition
- position to start parsing from- Returns:
- new position, negative value means parse failed - apply complement operator (~) to get position of failure
- Throws:
java.lang.UnsupportedOperationException
- if parsing is not supportedjava.lang.IllegalArgumentException
- if the instant is nulljava.lang.IllegalArgumentException
- if any field is out of range
-
parseMillis
public long parseMillis(java.lang.String text)
Parses a datetime from the given text, returning the number of milliseconds since the epoch, 1970-01-01T00:00:00Z.The parse will use the ISO chronology, and the default time zone. If the text contains a time zone string then that will be taken into account.
- Parameters:
text
- the text to parse, not null- Returns:
- parsed value expressed in milliseconds since the epoch
- Throws:
java.lang.UnsupportedOperationException
- if parsing is not supportedjava.lang.IllegalArgumentException
- if the text to parse is invalid
-
parseLocalDate
public LocalDate parseLocalDate(java.lang.String text)
Parses only the local date from the given text, returning a new LocalDate.This will parse the text fully according to the formatter, using the UTC zone. Once parsed, only the local date will be used. This means that any parsed time, time-zone or offset field is completely ignored. It also means that the zone and offset-parsed settings are ignored.
- Parameters:
text
- the text to parse, not null- Returns:
- the parsed date, never null
- Throws:
java.lang.UnsupportedOperationException
- if parsing is not supportedjava.lang.IllegalArgumentException
- if the text to parse is invalid- Since:
- 2.0
-
parseLocalTime
public LocalTime parseLocalTime(java.lang.String text)
Parses only the local time from the given text, returning a new LocalTime.This will parse the text fully according to the formatter, using the UTC zone. Once parsed, only the local time will be used. This means that any parsed date, time-zone or offset field is completely ignored. It also means that the zone and offset-parsed settings are ignored.
- Parameters:
text
- the text to parse, not null- Returns:
- the parsed time, never null
- Throws:
java.lang.UnsupportedOperationException
- if parsing is not supportedjava.lang.IllegalArgumentException
- if the text to parse is invalid- Since:
- 2.0
-
parseLocalDateTime
public LocalDateTime parseLocalDateTime(java.lang.String text)
Parses only the local date-time from the given text, returning a new LocalDateTime.This will parse the text fully according to the formatter, using the UTC zone. Once parsed, only the local date-time will be used. This means that any parsed time-zone or offset field is completely ignored. It also means that the zone and offset-parsed settings are ignored.
- Parameters:
text
- the text to parse, not null- Returns:
- the parsed date-time, never null
- Throws:
java.lang.UnsupportedOperationException
- if parsing is not supportedjava.lang.IllegalArgumentException
- if the text to parse is invalid- Since:
- 2.0
-
parseDateTime
public DateTime parseDateTime(java.lang.String text)
Parses a date-time from the given text, returning a new DateTime.The parse will use the zone and chronology specified on this formatter.
If the text contains a time zone string then that will be taken into account in adjusting the time of day as follows. If the
withOffsetParsed()
has been called, then the resulting DateTime will have a fixed offset based on the parsed time zone. Otherwise the resulting DateTime will have the zone of this formatter, but the parsed zone may have caused the time to be adjusted.- Parameters:
text
- the text to parse, not null- Returns:
- the parsed date-time, never null
- Throws:
java.lang.UnsupportedOperationException
- if parsing is not supportedjava.lang.IllegalArgumentException
- if the text to parse is invalid
-
parseMutableDateTime
public MutableDateTime parseMutableDateTime(java.lang.String text)
Parses a date-time from the given text, returning a new MutableDateTime.The parse will use the zone and chronology specified on this formatter.
If the text contains a time zone string then that will be taken into account in adjusting the time of day as follows. If the
withOffsetParsed()
has been called, then the resulting DateTime will have a fixed offset based on the parsed time zone. Otherwise the resulting DateTime will have the zone of this formatter, but the parsed zone may have caused the time to be adjusted.- Parameters:
text
- the text to parse, not null- Returns:
- the parsed date-time, never null
- Throws:
java.lang.UnsupportedOperationException
- if parsing is not supportedjava.lang.IllegalArgumentException
- if the text to parse is invalid
-
-