因爲java版本的迭代,一個使用java開發的項目中可能出現多種日期對象,例如LocalDateTime、LocalDate、Date,不像C#只有一個DateTime,所以在各類日期格式或者對象之間的轉換顯得有點複雜,老是記不住,在須要用到時老是須要依靠搜索引擎,有點浪費時間,因此特地把經常使用的轉換場景總結以下:java
一、 LocalDateTime轉爲String、TimeStamp、Long、Instant、 Date搜索引擎
System.out.println("----------------LocalDateTime----------------"); //LocalDateTime -> String String localDateTimeToString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); System.out.println("LocalDateTime -> String: " + localDateTimeToString); //LocalDateTime -> TimeStamp Timestamp localDateTimeToTimeStamp = Timestamp.valueOf(LocalDateTime.now()); System.out.println("LocalDateTime -> TimeStamp: " + localDateTimeToTimeStamp); //LocalDateTime -> Long Long localDateTimeToLong = Timestamp.valueOf(LocalDateTime.now()).getTime(); System.out.println("LocalDateTime -> Long: " + localDateTimeToLong); //LocalDateTime -> Instant Instant localDateTimeToInstant = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant(); System.out.println("LocalDateTime -> Instant: " + localDateTimeToInstant); //LocalDateTime -> Date Date LocalDateTimeToDate = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()); System.out.println("LocalDateTime -> Date: " + LocalDateTimeToDate);
二、String轉爲LocalDateTime、 Dateorm
System.out.println("----------------String----------------"); //String -> LocalDateTime LocalDateTime stringToLocalDateTime = LocalDateTime.parse("2018-03-11 15:30:11", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); System.out.println("String -> LocalDateTime: " + stringToLocalDateTime); //String -> Date try { Date stringToDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2018-03-11 15:30:11"); System.out.println("String -> Date: " + stringToDate); } catch (ParseException ex) { }
三、Timestamp轉爲LocalDateTime、 Date對象
System.out.println("---------------Timestamp-----------------"); //Timestamp -> LocalDateTime LocalDateTime timeStampToLocalDateTime = LocalDateTime.ofInstant(new Timestamp(1520754566856L).toInstant(), ZoneId.systemDefault()); System.out.println("Timestamp -> LocalDateTime: " + timeStampToLocalDateTime); //Timestamp -> Date Date timestampToDate = Date.from(new Timestamp(1520754566856L).toInstant()); System.out.println("Timestamp -> Date: " + timestampToDate);
四、Long轉爲LocalDateTime、 Dateblog
System.out.println("---------------Long-----------------"); //Long -> LocalDateTime LocalDateTime longToLocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1520754566856L), ZoneId.systemDefault()); System.out.println("Long -> LocalDateTime: " + longToLocalDateTime); //Long -> Date Date longToDate = new Date(1520754566856L); System.out.println("Long -> Date: " + longToDate);
五、Instant轉爲LocalDateTime、 Date索引
System.out.println("----------------Instant----------------"); //Instant -> LocalDateTime LocalDateTime instantToLocalDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()); System.out.println("Instant -> LocalDateTime: " + instantToLocalDateTime); //Instant -> Date Date instantToDate = Date.from(Instant.now()); System.out.println("Instant -> Date: " + instantToDate);
六、Date轉爲LocalDateTime、String、TimeStamp、Long、Instant開發
System.out.println("----------------Date----------------"); //Date -> String String dateToString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); System.out.println("Date -> String: " + dateToString); //Date -> LocalDateTime LocalDateTime dateToLocalDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()); System.out.println("Date -> LocalDateTime: " + dateToLocalDateTime); //Date -> Timestamp Timestamp dateToTimestamp = new Timestamp(new Date().getTime()); System.out.println("Date -> Timestamp: " + dateToTimestamp); //Date -> Long Long dateToLong = new Date().getTime(); System.out.println("Date -> Long: " + dateToLong); //Date -> Instant Instant dateToInstant = new Date().toInstant(); System.out.println("Date -> Instant: " + dateToInstant);