时间日期格式转换对应API
指定日期格式看自己业务需求而定
public class Demo01 {
public static void main(String[] args) throws ParseException {
Date date = new Date();//创建时间日期对象 java.util.Date;
System.out.println(date); //当前输出结果为 : Mon May 24 10:37:23 CST 2021
//(格式一)时间日期格式化对象----指定转换格式 "yyyy-MM-dd HH:mm:ss" 2021-03-25 12:35:45
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//(格式二)时间日期格式化对象----指定转换格式 "yyyyMMdd HH:mm:ss" 20210325 12:35:45
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
//(格式三)时间日期格式化对象----指定转换格式 "yyyy-MM-dd HH:mm:ss" 20210325 12:35:45
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
//将当前时间转换成指定格式字符串
String format = simpleDateFormat.format(date);
System.out.println(format); //输出结果为: 2021-05-24 10:37:23
//将字符串格式时间日期转换成 Date 格式
Date parse = simpleDateFormat.parse(format);
System.out.println(parse); //输出结果为 Mon May 24 10:37:23 CST 2021
}
}