//第一种
String format = DateFormat.getDateTimeInstance().format(new Date());
System.out.println(format);
//第二种
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format1 = simpleDateFormat.format(new Date());
System.out.println(format1);
第三种:
添加依赖
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.9.4</version> </dependency>
DateTime dateTime = new DateTime();
String s = dateTime.toString("yyyy-MM-dd HH:mm:ss");
System.out.println(s);
//如果将long类型的毫秒值进行格式化,newnew DateTime(这个值必须是long类型,不然会报错),如果是string类型的毫秒值,将string转换为long即可
articleVo.setCreateDate(new DateTime(date).toString("yyyy-MM-dd HH:mm:ss"));
第四种:jdk1.8
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormatter.format(LocalDateTime.now());
System.out.println(format);