简述
用于Date类格式化。
抽象类,已知直接实现子类:SimpleDateFormat。
常用方法
format(Date date)
parse(String source)
将source转化为日期并返回。
个人测试
代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DemoDateFormat {
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String text = format.format(new Date());
System.out.println(text);
Date date = format.parse(text);
System.out.println(date);
String text1 = format.format(new Date());
System.out.println(text1);
}
}
运行结果
2021年01月30日 16:34:13
Sat Jan 30 16:34:13 CST 2021
2021年01月30日 16:34:13
对照表
Letter | Date or Time Component | Examples |
---|---|---|
y | 年 | 1996 ; 96 |
M | 月 | July ; Jul ; 07 |
d | 日(Day in month) | 10 |
H | 时 | 0 |
m | 分 | 36 |
s | 秒 | 55 |
S | 毫秒 | 846 |
G | Era designator | AD |
Y | Week year | 2009 ; 09 |
D | Day in year | 189 |
w | Week in year | 27 |
W | Week in month | 2 |
F | Day of week in month | 3 (第三周) |
E | Day name in week | Tuesday ; Tue |
u | Day number of week | 1 (Monday) |
a | Am/pm marker | PM |
h | Hour in am/pm (1-12) | 12 |
注意的几点
小时:hh/HH/KK/kk。其中大写(0-11),小写(1-12),hh与KK需要加aa表示上午或者下午。
星期:
F 当月第几周
EEE 星期的名称
u 星期的序号(1-7)