由于java版本的迭代,一个使用java开发的项目中可能出现多种日期对象,例如LocalDateTime、LocalDate、Date,不像C#只有一个DateTime,因此在各种日期格式或者对象之间的转换显得有点复杂,总是记不住,在需要用到时总是需要依靠搜索引擎,有点浪费时间,所以特意把常用的转换场景总结如下:
1、 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);
2、String转为LocalDateTime、 Date
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) { }
3、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);
4、Long转为LocalDateTime、 Date
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);
5、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);
6、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);