java语法里, Date类型数据按格式输出.
//当前时间
final Date date = new Date();
//定义format格式
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS E z");
//按照格式,把时间转化成字符串
final String dateStr = dateFormat.format(date);
System.out.println(dateStr);
输出如下:
2023-06-07 03:12:35:522 周三 CST
进程已结束,退出代码0
附表:列出了各种format标识符的含义.
标识符 | 含义 | 介绍 | 举例 |
---|---|---|---|
G | era designator | Tex | AD |
y | year | Number | 2009 |
M | month in year | Text & | July & 07 |
d | day in month | Number | 10 |
h | hour in am/pm (1-1 | Number | 12 |
H | hour in day (0-23) | Number | 0 |
m | minute in hour | Number | 30 |
s | second in minute | Number | 55 |
S | millisecond | Num | 978 |
E | day in week | Tex | Tuesday |
D | day in year | Num | 189 |
F | day of week in mon | Number | 2 (2nd Wed in July) |
w | week in year | Number | 27 |
W | week in month | Number | 2 |
a | am/pm marker | Text | PM |
k | hour in day (1-24) | Number | 24 |
K | hour in am/pm (0-1 | Number | 0 |
z | time zone | Text | Pacific Standard Time |
’ | escape for text | Del | (none) |
’ | single quote | Literal | ’ |