JDK1.8 LocalDateTime 時間類與字符互轉
阿新 • • 發佈:2018-05-19
nth formatter ear class light 大小寫 orm ava 1.8
public static void main(String[] args) { DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yy-MM-dd"); LocalDate localDateTime=LocalDate.now(); localDateTime.format(dateTimeFormatter); System.out.println(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
}
說明: jdk1.8 嚴格區分LocalDate(日期) LocalDateTime(時間日期) LocalTime(時間)
JDK1.8 LocalDateTime 時間類與字符互轉