Class FormatBuilder

java.lang.Object
java.text.Format
org.miaixz.bus.core.center.date.format.FormatBuilder
All Implemented Interfaces:
Serializable, Cloneable, DateParser, PositionDateParser, DatePrinter, FormatPrinter

public class FormatBuilder extends Format implements PositionDateParser, FormatPrinter
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Details

    • FULL

      public static final int FULL
      FULL locale dependent date or time style.
      See Also:
    • LONG

      public static final int LONG
      LONG locale dependent date or time style.
      See Also:
    • MEDIUM

      public static final int MEDIUM
      MEDIUM locale dependent date or time style.
      See Also:
    • SHORT

      public static final int SHORT
      SHORT locale dependent date or time style.
      See Also:
  • Constructor Details

    • FormatBuilder

      protected FormatBuilder(String pattern, TimeZone timeZone, Locale locale)
      构造
      Parameters:
      pattern - 使用SimpleDateFormat 相同的日期格式
      timeZone - 非空时区 TimeZone
      locale - Locale 日期地理位置
      Throws:
      NullPointerException - if pattern, timeZone, or locale is null.
    • FormatBuilder

      protected FormatBuilder(String pattern, TimeZone timeZone, Locale locale, Date centuryStart)
      构造
      Parameters:
      pattern - 使用SimpleDateFormat 相同的日期格式
      timeZone - 非空时区 TimeZone
      locale - Locale 日期地理位置
      centuryStart - The start of the 100 year period to use as the "default century" for 2 digit year parsing. If centuryStart is null, defaults to now - 80 years
      Throws:
      NullPointerException - if pattern, timeZone, or locale is null.
  • Method Details

    • getInstance

      public static FormatBuilder getInstance()
      获得 FastDateFormat实例,使用默认格式和地区
      Returns:
      FastFormat
    • getInstance

      public static FormatBuilder getInstance(String pattern)
      获得 FastFormat 实例,使用默认地区 支持缓存
      Parameters:
      pattern - 使用SimpleDateFormat 相同的日期格式
      Returns:
      FastFormat
      Throws:
      IllegalArgumentException - 日期格式问题
    • getInstance

      public static FormatBuilder getInstance(String pattern, TimeZone timeZone)
      获得 FastFormat 实例 支持缓存
      Parameters:
      pattern - 使用SimpleDateFormat 相同的日期格式
      timeZone - 时区TimeZone
      Returns:
      FastFormat
      Throws:
      IllegalArgumentException - 日期格式问题
    • getInstance

      public static FormatBuilder getInstance(String pattern, Locale locale)
      获得 FastFormat 实例 支持缓存
      Parameters:
      pattern - 使用SimpleDateFormat 相同的日期格式
      locale - Locale 日期地理位置
      Returns:
      FastFormat
      Throws:
      IllegalArgumentException - 日期格式问题
    • getInstance

      public static FormatBuilder getInstance(String pattern, TimeZone timeZone, Locale locale)
      获得 FastFormat 实例 支持缓存
      Parameters:
      pattern - 使用SimpleDateFormat 相同的日期格式
      timeZone - 时区TimeZone
      locale - Locale 日期地理位置
      Returns:
      FastFormat
      Throws:
      IllegalArgumentException - 日期格式问题
    • getDateInstance

      public static FormatBuilder getDateInstance(int style)
      获得 FastFormat 实例 支持缓存
      Parameters:
      style - date style: FULL, LONG, MEDIUM, or SHORT
      Returns:
      本地化 FastFormat
    • getDateInstance

      public static FormatBuilder getDateInstance(int style, Locale locale)
      获得 FastFormat 实例 支持缓存
      Parameters:
      style - date style: FULL, LONG, MEDIUM, or SHORT
      locale - Locale 日期地理位置
      Returns:
      本地化 FastFormat
    • getDateInstance

      public static FormatBuilder getDateInstance(int style, TimeZone timeZone)
      获得 FastFormat 实例 支持缓存
      Parameters:
      style - date style: FULL, LONG, MEDIUM, or SHORT
      timeZone - 时区TimeZone
      Returns:
      本地化 FastFormat
    • getDateInstance

      public static FormatBuilder getDateInstance(int style, TimeZone timeZone, Locale locale)
      获得 FastFormat 实例 支持缓存
      Parameters:
      style - date style: FULL, LONG, MEDIUM, or SHORT
      timeZone - 时区TimeZone
      locale - Locale 日期地理位置
      Returns:
      本地化 FastFormat
    • getTimeInstance

      public static FormatBuilder getTimeInstance(int style)
      获得 FastFormat 实例 支持缓存
      Parameters:
      style - time style: FULL, LONG, MEDIUM, or SHORT
      Returns:
      本地化 FastFormat
    • getTimeInstance

      public static FormatBuilder getTimeInstance(int style, Locale locale)
      获得 FastFormat 实例 支持缓存
      Parameters:
      style - time style: FULL, LONG, MEDIUM, or SHORT
      locale - Locale 日期地理位置
      Returns:
      本地化 FastFormat
    • getTimeInstance

      public static FormatBuilder getTimeInstance(int style, TimeZone timeZone)
      获得 FastFormat 实例 支持缓存
      Parameters:
      style - time style: FULL, LONG, MEDIUM, or SHORT
      timeZone - optional time zone, overrides time zone of formatted time
      Returns:
      本地化 FastFormat
    • getTimeInstance

      public static FormatBuilder getTimeInstance(int style, TimeZone timeZone, Locale locale)
      获得 FastFormat 实例 支持缓存
      Parameters:
      style - time style: FULL, LONG, MEDIUM, or SHORT
      timeZone - optional time zone, overrides time zone of formatted time
      locale - Locale 日期地理位置
      Returns:
      本地化 FastFormat
    • getDateTimeInstance

      public static FormatBuilder getDateTimeInstance(int dateStyle, int timeStyle)
      获得 FastFormat 实例 支持缓存
      Parameters:
      dateStyle - date style: FULL, LONG, MEDIUM, or SHORT
      timeStyle - time style: FULL, LONG, MEDIUM, or SHORT
      Returns:
      本地化 FastFormat
    • getDateTimeInstance

      public static FormatBuilder getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)
      获得 FastFormat 实例 支持缓存
      Parameters:
      dateStyle - date style: FULL, LONG, MEDIUM, or SHORT
      timeStyle - time style: FULL, LONG, MEDIUM, or SHORT
      locale - Locale 日期地理位置
      Returns:
      本地化 FastFormat
    • getDateTimeInstance

      public static FormatBuilder getDateTimeInstance(int dateStyle, int timeStyle, TimeZone timeZone)
      获得 FastFormat 实例 支持缓存
      Parameters:
      dateStyle - date style: FULL, LONG, MEDIUM, or SHORT
      timeStyle - time style: FULL, LONG, MEDIUM, or SHORT
      timeZone - 时区TimeZone
      Returns:
      本地化 FastFormat
    • getDateTimeInstance

      public static FormatBuilder getDateTimeInstance(int dateStyle, int timeStyle, TimeZone timeZone, Locale locale)
      获得 FastFormat 实例 支持缓存
      Parameters:
      dateStyle - date style: FULL, LONG, MEDIUM, or SHORT
      timeStyle - time style: FULL, LONG, MEDIUM, or SHORT
      timeZone - 时区TimeZone
      locale - Locale 日期地理位置
      Returns:
      本地化 FastFormat
    • getDateTimeInstance

      public static DateTimeFormatter getDateTimeInstance(String pattern)
      创建并为 DateTimeFormatter 赋予默认时区和位置信息,默认值为系统默认值。
      Parameters:
      pattern - 日期格式
      Returns:
      DateTimeFormatter
    • format

      public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
      Specified by:
      format in class Format
    • format

      public String format(long millis)
      Description copied from interface: FormatPrinter
      格式化日期表示的毫秒数
      Specified by:
      format in interface FormatPrinter
      Parameters:
      millis - 日期毫秒数
      Returns:
      the formatted string
    • format

      public String format(Date date)
      Description copied from interface: FormatPrinter
      使用 GregorianCalendar 格式化 Date
      Specified by:
      format in interface FormatPrinter
      Parameters:
      date - 日期 Date
      Returns:
      格式化后的字符串
    • format

      public String format(Calendar calendar)
      Description copied from interface: FormatPrinter
      Formats a Calendar object.

      格式化 Calendar

      Specified by:
      format in interface FormatPrinter
      Parameters:
      calendar - Calendar
      Returns:
      格式化后的字符串
    • format

      public <B extends Appendable> B format(long millis, B buf)
      Description copied from interface: FormatPrinter
      Formats a millisecond long value into the supplied Appendable.
      Specified by:
      format in interface FormatPrinter
      Type Parameters:
      B - the Appendable class type, usually StringBuilder or StringBuffer.
      Parameters:
      millis - the millisecond value to format
      buf - the buffer to format into
      Returns:
      the specified string buffer
    • format

      public <B extends Appendable> B format(Date date, B buf)
      Description copied from interface: FormatPrinter
      Formats a Date object into the supplied Appendable using a GregorianCalendar.
      Specified by:
      format in interface FormatPrinter
      Type Parameters:
      B - the Appendable class type, usually StringBuilder or StringBuffer.
      Parameters:
      date - the date to format
      buf - the buffer to format into
      Returns:
      the specified string buffer
    • format

      public <B extends Appendable> B format(Calendar calendar, B buf)
      Description copied from interface: FormatPrinter
      Formats a Calendar object into the supplied Appendable. The TimeZone set on the Calendar is only used to adjust the time offset. The TimeZone specified during the construction of the Parser will determine the TimeZone used in the formatted string.
      Specified by:
      format in interface FormatPrinter
      Type Parameters:
      B - the Appendable class type, usually StringBuilder or StringBuffer.
      Parameters:
      calendar - the calendar to format
      buf - the buffer to format into
      Returns:
      the specified string buffer
    • parse

      public Date parse(CharSequence source) throws DateException
      Description copied from interface: DateParser
      将日期字符串解析并转换为 Date 对象
      Specified by:
      parse in interface DateParser
      Parameters:
      source - 被解析的日期字符串
      Returns:
      Date对象
      Throws:
      DateException - 转换异常,被转换的字符串格式错误。
    • parse

      public Date parse(CharSequence source, ParsePosition pos)
      Description copied from interface: PositionDateParser
      将日期字符串解析并转换为 Date 对象 等价于 DateFormat.parse(String, ParsePosition)
      Specified by:
      parse in interface PositionDateParser
      Parameters:
      source - 日期字符串
      pos - ParsePosition
      Returns:
      Date
    • parse

      public boolean parse(CharSequence source, ParsePosition pos, Calendar calendar)
      Description copied from interface: PositionDateParser
      根据给定格式更新Calendar 解析成功后,ParsePosition.getIndex()更新成转换到的位置 失败则ParsePosition.getErrorIndex()更新到解析失败的位置
      Specified by:
      parse in interface PositionDateParser
      Parameters:
      source - 被转换的日期字符串
      pos - 定义开始转换的位置,转换结束后更新转换到的位置
      calendar - 解析并更新的Calendar
      Returns:
      解析成功返回 true,否则返回false
    • parseObject

      public Object parseObject(String source, ParsePosition pos)
      Specified by:
      parseObject in class Format
    • getPattern

      public String getPattern()
      Description copied from interface: DatePrinter
      获得日期格式化或者转换的格式
      Specified by:
      getPattern in interface DatePrinter
      Returns:
      SimpleDateFormat兼容的格式
    • getTimeZone

      public TimeZone getTimeZone()
      Description copied from interface: DatePrinter
      获得时区
      Specified by:
      getTimeZone in interface DatePrinter
      Returns:
      TimeZone
    • getLocale

      public Locale getLocale()
      Description copied from interface: DatePrinter
      获得 日期地理位置
      Specified by:
      getLocale in interface DatePrinter
      Returns:
      Locale
    • getMaxLengthEstimate

      public int getMaxLengthEstimate()
      估算生成的日期字符串长度 实际生成的字符串长度小于或等于此值
      Returns:
      日期字符串长度
    • getDateTimeFormatter

      public DateTimeFormatter getDateTimeFormatter()
      便捷获取 DateTimeFormatter 由于 Fields 很大一部分的格式没有提供 DateTimeFormatter,因此这里提供快捷获取方式
      Returns:
      DateTimeFormatter
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object