日期格式化
y 代表年 M 代表月 d 代表日
E 代表星期
H 代表24进制的小时 m 代表分钟 s 代表秒 S 代表毫秒
h 代表12进制的小时
public class DateTest { public static void main(String[] args) { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date=new Date(); String dateStr=sdf.format(date); System.out.println(dateStr); }
字符串转日期
public class DateTest { public static void main(String[] args) { SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String str="2020/10/13 10:11:12"; try { Date date=sdf.parse(str); System.out.println("字符串:"+str+"通过yyyy/MM/dd HH:mm:ss格式转换得到"+date.toString()); }
catch (ParseException e)
{ e.printStackTrace(); } } }