1、用于区域设置敏感的方式格式化和解析日期,日期和时间格式由日期和时间模式字符串指定。
2、在日期和时间模式字符串中,从“A”到“Z”,以及从“a”到“z”的字母被解释为表示日期或时间字符串组成的模式字母。
常用的模式字母对应关系如下:
y---年 d---日 m---分
M---月 H---时 s---秒
2、构造方法:
1、public SimpleDateFormat() 表示构造一个SimpleDateFormat,使用默认模式和日期格式
2、public SimpleDateFormat(String pattern) 表示构造一个SimpleDateFormat,使用给定的模式和默认的日期格式
3、SimpleDateFormat 格式化和解析日期
1、格式化: 从 Date------>String
public final fomate(Date date) 将日期格式化成日期/时间字符串
public class new001 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat s=new SimpleDateFormat();
Date d=new Date();
String format = s.format(d);
System.out.println("format = " + format);
}
}
编译运行结果:
2、解析 从 String --------->Date
public Date parse(String sourse) 从给定字符串开始解析文本以生成日期
public class date{
public static void main(String[] args){
String s="2021-08-03 22:20:00";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf.parse(s);
System.out.println(dd);
}
}
编译运行结果为: