format method

String format(
  1. String pattern
)

格式化时间为指定格式的字符串

支持的格式占位符:

  • YYYY: 四位年份
  • YY: 两位年份
  • MM: 两位月份(01-12)
  • M: 月份(1-12)
  • DD: 两位日期(01-31)
  • D: 日期(1-31)
  • HH: 两位24小时制小时(00-23)
  • H: 24小时制小时(0-23)
  • hh: 两位12小时制小时(01-12)
  • h: 12小时制小时(1-12)
  • mm: 两位分钟(00-59)
  • m: 分钟(0-59)
  • ss: 两位秒数(00-59)
  • s: 秒数(0-59)
  • SSS: 三位毫秒数(000-999)

示例:

DateTime now = DateTime.now();
now.format('YYYY-MM-DD'); // 2024-01-17
now.format('YYYY年MM月DD日'); // 2024年01月17日
now.format('HH:mm:ss'); // 14:30:25
now.format('YYYY-MM-DD HH:mm:ss'); // 2024-01-17 14:30:25

Implementation

String format(String pattern) {
  String result = pattern;
  // 年份
  result = result.replaceAll('YYYY', year.toString().padLeft(4, '0'));
  result = result.replaceAll('YY', (year % 100).toString().padLeft(2, '0'));
  // 月份
  result = result.replaceAll('MM', month.toString().padLeft(2, '0'));
  result = result.replaceAll('M', month.toString());
  // 日期
  result = result.replaceAll('DD', day.toString().padLeft(2, '0'));
  result = result.replaceAll('D', day.toString());
  // 24小时制小时
  result = result.replaceAll('HH', hour.toString().padLeft(2, '0'));
  result = result.replaceAll('H', hour.toString());
  // 12小时制小时
  int hour12 = hour > 12 ? hour - 12 : (hour == 0 ? 12 : hour);
  result = result.replaceAll('hh', hour12.toString().padLeft(2, '0'));
  result = result.replaceAll('h', hour12.toString());
  // 分钟
  result = result.replaceAll('mm', minute.toString().padLeft(2, '0'));
  result = result.replaceAll('m', minute.toString());
  // 秒数
  result = result.replaceAll('ss', second.toString().padLeft(2, '0'));
  result = result.replaceAll('s', second.toString());
  // 毫秒
  result = result.replaceAll('SSS', millisecond.toString().padLeft(3, '0'));
  return result;
}