标签:当前时间 sys 年月日 gety void str pat cal main
public static void main(String[] args) {
System.out.println(LocalDate.now());//年月日
System.out.println(LocalDateTime.now());//年月日时分秒
System.out.println(LocalTime.now());//时分秒
//localDateTime 获取当前时间
DateTimeFormatter dateTimeFormatter= DateTimeFormatter.ofPattern("yy-MM-dd");
LocalDate localDateTime=LocalDate.now();
localDateTime.format(dateTimeFormatter);
System.out.println(localDateTime);
//字符转成LocalDateTime
String time="2018-05-18 23:24:25";
DateTimeFormatter dateTimeFormatter2=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//严格遵守大小写
LocalDateTime localDate=LocalDateTime.parse(time,dateTimeFormatter2);
LocalDate localDate1= LocalDate.of(localDate.getYear(),localDate.getMonth(),localDate.getDayOfMonth());
System.out.println(localDate1);//2018-05-18
//date 转LocalDateTime
java.util.Date date = new java.util.Date();
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime2 = LocalDateTime.ofInstant(instant, zone);
System.out.println(localDateTime2);
// LocalDateTime 转date
LocalDateTime localDateTime3 = LocalDateTime.now();
Instant instan3 = localDateTime3.atZone(zone).toInstant();
java.util.Date date3 = Date.from(instant);
System.out.println(date3);
}
标签:当前时间 sys 年月日 gety void str pat cal main
原文地址:https://www.cnblogs.com/jinjian91/p/11507238.html