Class FormatBuilder

java.lang.Object
java.text.Format
org.aoju.bus.core.date.formatter.FormatBuilder
All Implemented Interfaces:
Serializable, Cloneable, DateMotd, DateParser, DatePrinter

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

    • FULL

      public static final int FULL
      完全地区相关的日期或时间样式
      See Also:
    • LONG

      public static final int LONG
      长地区相关的日期或时间样式
      See Also:
    • MEDIUM

      public static final int MEDIUM
      中等地区相关的日期或时间样式
      See Also:
    • SHORT

      public static final int SHORT
      短地区相关的日期或时间样式
      See Also:
  • Constructor Details

    • FormatBuilder

      protected FormatBuilder(String pattern, TimeZone timeZone, Locale locale)
      构造
      Parameters:
      pattern - 使用SimpleDateFormat 相同的日期格式
      timeZone - 非空时区 TimeZone
      locale - Locale 日期地理位置
      Throws:
      NullPointerException - 如果模式、时区或区域设置为空
    • FormatBuilder

      protected FormatBuilder(String pattern, TimeZone timeZone, Locale locale, Date centuryStart)
      构造
      Parameters:
      pattern - 使用SimpleDateFormat 相同的日期格式
      timeZone - 非空时区 TimeZone
      locale - Locale 日期地理位置
      centuryStart - 使用100年周期的开始作为2位数年解析的“默认世纪”。如果centuryStart为空,则默认为- 80年
      Throws:
      NullPointerException - 如果模式、时区或地区为空.
  • Method Details

    • getInstance

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

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

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

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

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

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

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

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

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

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

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

      public static FormatBuilder getTimeInstance(int style, TimeZone timeZone)
      获得 FormatBuilder 实例 支持缓存
      Parameters:
      style - 时间格式: FULL, LONG, MEDIUM, or SHORT
      timeZone - 可选时区,覆盖格式化时间的时区
      Returns:
      本地化 FormatBuilder
    • getTimeInstance

      public static FormatBuilder getTimeInstance(int style, TimeZone timeZone, Locale locale)
      获得 FormatBuilder 实例 支持缓存
      Parameters:
      style - 时间格式: FULL, LONG, MEDIUM, or SHORT
      timeZone - 可选时区,覆盖格式化时间的时区
      locale - Locale 日期地理位置
      Returns:
      本地化 FormatBuilder
    • getDateTimeInstance

      public static FormatBuilder getDateTimeInstance(int dateStyle, int timeStyle)
      获得 FormatBuilder 实例 支持缓存
      Parameters:
      dateStyle - 日期格式: FULL, LONG, MEDIUM, or SHORT
      timeStyle - 时间格式: FULL, LONG, MEDIUM, or SHORT
      Returns:
      本地化 FormatBuilder
    • getDateTimeInstance

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

      public static FormatBuilder getDateTimeInstance(int dateStyle, int timeStyle, TimeZone timeZone)
      获得 FormatBuilder 实例 支持缓存
      Parameters:
      dateStyle - 日期格式: FULL, LONG, MEDIUM, or SHORT
      timeStyle - 时间格式: FULL, LONG, MEDIUM, or SHORT
      timeZone - 时区TimeZone
      Returns:
      本地化 FormatBuilder
    • getDateTimeInstance

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

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

      public String format(long millis)
      Description copied from interface: DatePrinter
      格式化日期表示的毫秒数
      Specified by:
      format in interface DatePrinter
      Parameters:
      millis - 日期毫秒数
      Returns:
      格式化的字符串
    • format

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

      public String format(Calendar calendar)
      Description copied from interface: DatePrinter
      格式化 Calendar 对象
      Specified by:
      format in interface DatePrinter
      Parameters:
      calendar - Calendar
      Returns:
      格式化后的字符串
    • format

      public <B extends Appendable> B format(long millis, B buf)
      Description copied from interface: DatePrinter
      将毫秒long值格式化为提供的Appendable
      Specified by:
      format in interface DatePrinter
      Type Parameters:
      B - 附加类类型,通常是StringBuilder或StringBuffer
      Parameters:
      millis - 要格式化的毫秒值
      buf - 要格式化为的缓冲区
      Returns:
      指定的字符串缓冲区
    • format

      public <B extends Appendable> B format(Date date, B buf)
      Description copied from interface: DatePrinter
      使用GregorianCalendarDate对象格式化为提供的Appendable
      Specified by:
      format in interface DatePrinter
      Type Parameters:
      B - 附加类类型,通常是StringBuilder或StringBuffer
      Parameters:
      date - 格式的日期
      buf - 要格式化为的缓冲区
      Returns:
      指定的字符串缓冲区
    • format

      public <B extends Appendable> B format(Calendar calendar, B buf)
      Description copied from interface: DatePrinter
      Calendar对象格式化为提供的Appendable 日历上设置的时区仅用于调整时间偏移。解析器构造期间指定的时区将确定格式化字符串中使用的时区
      Specified by:
      format in interface DatePrinter
      Type Parameters:
      B - 附加类类型,通常是StringBuilder或StringBuffer
      Parameters:
      calendar - 要格式化的日历
      buf - 要格式化为的缓冲区
      Returns:
      指定的字符串缓冲区
    • parse

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

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

      public boolean parse(String source, ParsePosition pos, Calendar calendar)
      Description copied from interface: DateParser
      根据给定格式转换日期字符串 使用解析的字段更新日历。成功之后,将更新ParsePosition索引,以指示消耗了多少源文本。并不是所有的源文本都需要使用 在解析失败时,ParsePosition错误索引将更新为源文本的偏移量,该偏移量与提供的格式不匹配
      Specified by:
      parse in interface DateParser
      Parameters:
      source - 被转换的日期字符串
      pos - 定义开始转换的位置,转换结束后更新转换到的位置
      calendar - 用于设置已解析字段的日历
      Returns:
      如果源已被解析(pos parsePosition 已更新);否则为false(并更新 pos errorIndex)
    • parseObject

      public Object parseObject(String source, ParsePosition pos)
      Description copied from interface: DateParser
      根据 ParsePosition 给定将日期字符串解析并转换为 Date 对象
      Specified by:
      parseObject in interface DateParser
      Specified by:
      parseObject in class Format
      Parameters:
      source - 应该解析其开头的字符串
      pos - 解析的位置
      Returns:
      日期对象
      See Also:
    • getPattern

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

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

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

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

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

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

      public String toString()
      Overrides:
      toString in class Object