格式化字符串
日期和事件字符串格式化
当需要显示自己需要的时间和日期格式时,如果像输出满意的日期和时间格式,可以通过String类提供的format()方法来实现。
常用的日期格式化转换符如下所示:
转换符 | 说明 |
---|---|
%te | 一个月中的某一天(1~31) |
%tb | 指定语言环境中的月份简称 |
%tB | 指定语言环境的月份全称 |
%tA | 指定语言环境的星期几全称 |
%ta | 指定语言环境的星期几简称 |
%tc | 包括全部日期和时间信息 |
%tY | 4为年份 |
%tj | 一年中的第几天(001~366) |
%tm | 月份 |
%td | 一个月中的第几天(01~31) |
%ty | 2为年份 |
例如:
public static void main(String[] args) {
Date date = new Date();
System.out.println(String.format("%te", date));
System.out.println(String.format("%tb", date));
System.out.println(String.format("%tB", date));
System.out.println(String.format("%tA", date));
}
out:
31
3月
三月
星期三
####参照Java从入门到精通