Module bus.core

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:
  • Constructor Details

    • FormatBuilder

      protected FormatBuilder(String pattern, TimeZone timeZone, Locale locale)
      构造函数,初始化日期格式化器。
      Parameters:
      pattern - 使用SimpleDateFormat兼容的日期格式
      timeZone - 非空时区
      locale - 日期地域
      Throws:
      NullPointerException - 如果pattern、timeZone或locale为null
    • FormatBuilder

      protected FormatBuilder(String pattern, TimeZone timeZone, Locale locale, Date centuryStart)
      构造函数,初始化日期格式化器,允许指定世纪起始时间。
      Parameters:
      pattern - 使用SimpleDateFormat兼容的日期格式
      timeZone - 非空时区
      locale - 日期地域
      centuryStart - 两位的默认世纪起始时间,若为null则为当前时间前80年
      Throws:
      NullPointerException - 如果pattern、timeZone或locale为null
  • Method Details

    • getInstance

      public static FormatBuilder getInstance()
      获取默认格式和地域的FormatBuilder实例。
      Returns:
      FormatBuilder实例
    • getInstance

      public static FormatBuilder getInstance(String pattern)
      获取指定格式的FormatBuilder实例,使用默认时区和地域。
      Parameters:
      pattern - 日期格式
      Returns:
      FormatBuilder实例
      Throws:
      IllegalArgumentException - 如果日期格式无效
    • getInstance

      public static FormatBuilder getInstance(String pattern, TimeZone timeZone)
      获取指定格式和时区的FormatBuilder实例。
      Parameters:
      pattern - 日期格式
      timeZone - 时区
      Returns:
      FormatBuilder实例
      Throws:
      IllegalArgumentException - 如果日期格式无效
    • getInstance

      public static FormatBuilder getInstance(String pattern, Locale locale)
      获取指定格式和地域的FormatBuilder实例。
      Parameters:
      pattern - 日期格式
      locale - 地域
      Returns:
      FormatBuilder实例
      Throws:
      IllegalArgumentException - 如果日期格式无效
    • getInstance

      public static FormatBuilder getInstance(String pattern, TimeZone timeZone, Locale locale)
      获取指定格式、时区和地域的FormatBuilder实例。
      Parameters:
      pattern - 日期格式
      timeZone - 时区
      locale - 地域
      Returns:
      FormatBuilder实例
      Throws:
      IllegalArgumentException - 如果日期格式无效
    • getDateInstance

      public static FormatBuilder getDateInstance(int style)
      获取指定日期样式的FormatBuilder实例。
      Parameters:
      style - 日期样式(FULL、LONG、MEDIUM或SHORT)
      Returns:
      FormatBuilder实例
    • getDateInstance

      public static FormatBuilder getDateInstance(int style, Locale locale)
      获取指定日期样式和地域的FormatBuilder实例。
      Parameters:
      style - 日期样式(FULL、LONG、MEDIUM或SHORT)
      locale - 地域
      Returns:
      FormatBuilder实例
    • getDateInstance

      public static FormatBuilder getDateInstance(int style, TimeZone timeZone)
      获取指定日期样式和时区的FormatBuilder实例。
      Parameters:
      style - 日期样式(FULL、LONG、MEDIUM或SHORT)
      timeZone - 时区
      Returns:
      FormatBuilder实例
    • getDateInstance

      public static FormatBuilder getDateInstance(int style, TimeZone timeZone, Locale locale)
      获取指定日期样式、时区和地域的FormatBuilder实例。
      Parameters:
      style - 日期样式(FULL、LONG、MEDIUM或SHORT)
      timeZone - 时区
      locale - 地域
      Returns:
      FormatBuilder实例
    • getTimeInstance

      public static FormatBuilder getTimeInstance(int style)
      获取指定时间样式的FormatBuilder实例。
      Parameters:
      style - 时间样式(FULL、LONG、MEDIUM或SHORT)
      Returns:
      FormatBuilder实例
    • getTimeInstance

      public static FormatBuilder getTimeInstance(int style, Locale locale)
      获取指定时间样式和地域的FormatBuilder实例。
      Parameters:
      style - 时间样式(FULL、LONG、MEDIUM或SHORT)
      locale - 地域
      Returns:
      FormatBuilder实例
    • getTimeInstance

      public static FormatBuilder getTimeInstance(int style, TimeZone timeZone)
      获取指定时间样式和时区的FormatBuilder实例。
      Parameters:
      style - 时间样式(FULL、LONG、MEDIUM或SHORT)
      timeZone - 时区
      Returns:
      FormatBuilder实例
    • getTimeInstance

      public static FormatBuilder getTimeInstance(int style, TimeZone timeZone, Locale locale)
      获取指定时间样式、时区和地域的FormatBuilder实例。
      Parameters:
      style - 时间样式(FULL、LONG、MEDIUM或SHORT)
      timeZone - 时区
      locale - 地域
      Returns:
      FormatBuilder实例
    • getDateTimeInstance

      public static FormatBuilder getDateTimeInstance(int dateStyle, int timeStyle)
      获取指定日期和时间样式的FormatBuilder实例。
      Parameters:
      dateStyle - 日期样式(FULL、LONG、MEDIUM或SHORT)
      timeStyle - 时间样式(FULL、LONG、MEDIUM或SHORT)
      Returns:
      FormatBuilder实例
    • getDateTimeInstance

      public static FormatBuilder getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)
      获取指定日期、时间样式和地域的FormatBuilder实例。
      Parameters:
      dateStyle - 日期样式(FULL、LONG、MEDIUM或SHORT)
      timeStyle - 时间样式(FULL、LONG、MEDIUM或SHORT)
      locale - 地域
      Returns:
      FormatBuilder实例
    • getDateTimeInstance

      public static FormatBuilder getDateTimeInstance(int dateStyle, int timeStyle, TimeZone timeZone)
      获取指定日期、时间样式和时区的FormatBuilder实例。
      Parameters:
      dateStyle - 日期样式(FULL、LONG、MEDIUM或SHORT)
      timeStyle - 时间样式(FULL、LONG、MEDIUM或SHORT)
      timeZone - 时区
      Returns:
      FormatBuilder实例
    • getDateTimeInstance

      public static FormatBuilder getDateTimeInstance(int dateStyle, int timeStyle, TimeZone timeZone, Locale locale)
      获取指定日期、时间样式、时区和地域的FormatBuilder实例。
      Parameters:
      dateStyle - 日期样式(FULL、LONG、MEDIUM或SHORT)
      timeStyle - 时间样式(FULL、LONG、MEDIUM或SHORT)
      timeZone - 时区
      locale - 地域
      Returns:
      FormatBuilder实例
    • getDateTimeInstance

      public static DateTimeFormatter getDateTimeInstance(String pattern)
      创建DateTimeFormatter,使用系统默认时区和地域。
      Parameters:
      pattern - 日期格式
      Returns:
      DateTimeFormatter实例
    • format

      public StringBuffer format(Object object, StringBuffer toAppendTo, FieldPosition pos)
      格式化对象到字符串缓冲区。
      Specified by:
      format in class Format
      Parameters:
      object - 要格式化的对象
      toAppendTo - 目标字符串缓冲区
      pos - 字段位置
      Returns:
      格式化后的字符串缓冲区
    • format

      public String format(long millis)
      格式化毫秒时间。
      Specified by:
      format in interface FormatPrinter
      Parameters:
      millis - 毫秒时间
      Returns:
      格式化后的字符串
    • format

      public String format(Date date)
      格式化日期对象。
      Specified by:
      format in interface FormatPrinter
      Parameters:
      date - 日期对象
      Returns:
      格式化后的字符串
    • format

      public String format(Calendar calendar)
      格式化日历对象。
      Specified by:
      format in interface FormatPrinter
      Parameters:
      calendar - 日历对象
      Returns:
      格式化后的字符串
    • format

      public <B extends Appendable> B format(long millis, B buf)
      格式化毫秒时间到指定缓冲区。
      Specified by:
      format in interface FormatPrinter
      Type Parameters:
      B - Appendable类型
      Parameters:
      millis - 毫秒时间
      buf - 输出缓冲区
      Returns:
      格式化后的缓冲区
    • format

      public <B extends Appendable> B format(Date date, B buf)
      格式化日期对象到指定缓冲区。
      Specified by:
      format in interface FormatPrinter
      Type Parameters:
      B - Appendable类型
      Parameters:
      date - 日期对象
      buf - 输出缓冲区
      Returns:
      格式化后的缓冲区
    • format

      public <B extends Appendable> B format(Calendar calendar, B buf)
      格式化日历对象到指定缓冲区。
      Specified by:
      format in interface FormatPrinter
      Type Parameters:
      B - Appendable类型
      Parameters:
      calendar - 日历对象
      buf - 输出缓冲区
      Returns:
      格式化后的缓冲区
    • parse

      public Date parse(CharSequence source) throws DateException
      解析日期字符串。
      Specified by:
      parse in interface DateParser
      Parameters:
      source - 日期字符串
      Returns:
      解析后的日期对象
      Throws:
      DateException - 如果解析失败
    • parse

      public boolean parse(CharSequence source, ParsePosition pos, Calendar calendar)
      解析日期字符串到日历对象。
      Specified by:
      parse in interface PositionDateParser
      Parameters:
      source - 日期字符串
      pos - 解析位置
      calendar - 日历对象
      Returns:
      如果解析成功返回true
    • parseObject

      public Object parseObject(String source, ParsePosition pos)
      解析日期字符串到对象。
      Specified by:
      parseObject in class Format
      Parameters:
      source - 日期字符串
      pos - 解析位置
      Returns:
      解析后的对象
    • getPattern

      public String getPattern()
      获取日期格式模式。
      Specified by:
      getPattern in interface DatePrinter
      Returns:
      日期格式模式
    • getTimeZone

      public TimeZone getTimeZone()
      获取时区。
      Specified by:
      getTimeZone in interface DatePrinter
      Returns:
      时区
    • getLocale

      public Locale getLocale()
      获取地域。
      Specified by:
      getLocale in interface DatePrinter
      Returns:
      地域
    • getMaxLengthEstimate

      public int getMaxLengthEstimate()
      估算格式化后的最大字符串长度。
      Returns:
      最大长度估算
    • getDateTimeFormatter

      public DateTimeFormatter getDateTimeFormatter()
      获取与当前格式兼容的DateTimeFormatter。
      Returns:
      DateTimeFormatter实例
    • equals

      public boolean equals(Object object)
      判断是否与另一个对象相等。
      Overrides:
      equals in class Object
      Parameters:
      object - 要比较的对象
      Returns:
      如果相等返回true
    • hashCode

      public int hashCode()
      获取对象的哈希码。
      Overrides:
      hashCode in class Object
      Returns:
      哈希码
    • toString

      public String toString()
      返回对象的字符串表示。
      Overrides:
      toString in class Object
      Returns:
      字符串表示