String.format()作为文本处理工具,为我们提供强大而丰富的字符串格式化功能,这里根据查阅的资料做个学习笔记,整理成如下文章,供后续复习查阅。
format(String format, Object ... args)
该方法使用指定的格式字符串和参数返回一个格式化的字符串,格式化后的新字符串使用本地默认的语言环境。
format(Local l, String format, Pbject ... args)
其中,参数l为格式化过程中要应用的语言环境。如果l为null,则不进行本地化。
常用的日期格式转换符如下表所示:
转换符 | 说明 | 示例 |
%te | 一个月中的某一天(1~31) | 2 |
%tb | 指定语言环境的月份简称 | Feb(英文)、二月(中文) |
%tB | 指定语言环境的月份全称 | February(英文)、二月(中文) |
%tA | 指定语言环境的星期几全称 | Monday(英文)、星期一(中文) |
%ta | 指定语言环境的星期几简称 | Mon(英文)、星期一(中文) |
%tc | 包括全部日期和时间信息 | 星期二 三月 25 13:37:22 CST 2008 |
%tY | 4位年份 | 2019 |
%tj | 一年中的第几天(001~366) | 085 |
%tm | 月份 | 03 |
%td | 一个月中的第几天(01~31) | 02 |
%ty | 2位年份 | 19 |
举个例子,如下:
1 public class Eval {2 public static void main(String[] args) {3 Date date = new Date();4 String day = String.format("%te", date);5 System.out.println("今天是2019年8月:" + day + "号");6 String month = String.format("%tb", date);7 System.out.println("现在是2019年:" + month);8 String xingqi = String.format("%tA", date);9 System.out.println("今天是:" + xingqi);
10 String year = String.format("%tY", date);
11 System.out.println("现在是:" + year + "年");
12 }
13 }
输出结果:
1 今天是2019年8月:20号
2 现在是2019年:八月
3 今天是:星期二
4 现在是:2019年
常用的时间格式转换符如下表所示:
转换符 | 说明 | 示例 |
%tH | 2位数字的24时制的小时(00~23) | 14 |
%tI | 2位数字的12时制的小时(01~12) | 05 |
%tk | 2位数字的24时制的小时(0~23) | 5 |
%tl | 2位数字的12时制的小时(1~12) | 10 |
%tM | 2位数字的分钟(00~59) | 05 |
%tS | 2位数字的秒数(00~60) | 12 |
%tL | 3位数字的毫秒数(000~999) | 920 |
%tN | 9位数字的微秒数(000000000~999999999) | 062000000 |
%tp | 指定语言环境下上午或下午标记 | 下午(中文)、pm(英文) |
%tz | 相对于GMT RFC 82格式的数字时区偏移量 | +0800 |
%tZ | 时区缩写形式的字符串 | CST |
%ts | 1970-01-01 00:00:00至现在经过的秒数 | 1206345534 |
%tQ | 1970-01-01 00:00:00至现在经过的毫秒数 | 12923409349034 |
举个例子,如下:
1 public class GetDate {2 public static void main(String[] args) {3 Date date = new Date();4 String hour = String.format("%tH", date);5 String minute = String.format("%tM", date);6 String second = String.format("%tS", date);7 System.out.println("现在是:" + hour + "点" + minute + "分" + second + "秒");8 System.out.println("##################################");9 String hour2 = String.format("%tI", date);
10 String pm = String.format("%tp", date);
11 System.out.println("现在是:" + pm + hour2 + "点" + minute + "分" + second + "秒");
12 }
13 }
输出结果:
1 现在是:15点06分37秒
2 ##################################
3 现在是:下午03点06分37秒
常见的日期和时间组合的格式如下表所示:
转换符 | 说明 | 示例 |
%tF | “年-月-日”格式(4位年份) | 2019-08-20 |
%tD | “年/月/日”格式(2位年份) | 08/20/19 |
%tc | 全部日期和时间信息 | 星期二 三月 25 15:20:00 CST 2019 |
%tr | “时:分:秒 PM(AM)”格式(12时制) | 03:22:06 下午 |
%tT | “时:分:秒”格式(24时制) | 15:23:50 |
%tR | “时:分”格式(24时制) | 15:25 |
举个例子,如下:
1 public class DateAndTime {2 public static void main(String[] args) {3 Date date = new Date();4 String time = String.format("%tc", date);5 String form = String.format("%tF", date);6 String form2 = String.format("%tD", date);7 String form3 = String.format("%tr", date);8 String form4 = String.format("%tT", date);9 String form5 = String.format("%tR", date);
10 System.out.println("全部的时间信息是:" + time);
11 System.out.println("年-月-日格式:" + form);
12 System.out.println("年/月/日格式:" + form2);
13 System.out.println("时:分:秒 PM(AM)格式:" + form3);
14 System.out.println("时:分:秒格式:" + form4);
15 System.out.println("时:分格式:" + form5);
16 }
17 }
输出结果:
1 全部的时间信息是:星期二 八月 20 15:14:20 CST 2019
2 年-月-日格式:2019-08-20
3 年/月/日格式:08/20/19
4 时:分:秒 PM(AM)格式:03:14:20 下午
5 时:分:秒格式:15:14:20
6 时:分格式:15:14
以上内容为format()方法的一些常用功能,也是在工作场景中经常用到的。整理归纳方便后续学习查阅,如果后面还有遇到相关方法的其他用法,后期再对该篇文章进行补充。