public static void main(String[] args) { SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date=new Date(); System.out.print(dateFormater.format(date)); }
打印结果:
2017-04-28 11:16:21
字符串转日期:
public static void main(String[] args) { String time = "2017-10-23 17:57:10"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { System.out.println(sdf.parse(time)); } catch (ParseException e) { e.printStackTrace(); } }
打印结果:
Mon Oct 23 17:57:10 CST 2017