java7及以前
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(now);
System.out.println(String.format("日期格式化 : %s", date));
SimpleDateFormat sdft = new SimpleDateFormat("HH:mm:ss");
String time = sdft.format(now);
System.out.println(String.format("时间格式化 : %s", time));
SimpleDateFormat sdfdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datetime = sdfdt.format(now);
System.out.println(String.format("日期时间格式化 : %s", datetime));
java8及以后
LocalDate date = LocalDate.now();
System.out.println(String.format("日期格式化 : %s", date));
LocalTime time = LocalTime.now().withNano(0);
System.out.println(String.format("时间格式化 : %s", time));
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("日期时间格式化:yyyy-MM-dd HH:mm:ss");
String dateTimeStr = localDateTime.format(dateTimeFormatter);
System.out.println(dateTimeStr);