由于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);

  

最新文章

  1. MySQL备份命令mysqldump参数说明与示例
  2. OneProxy安全策略
  3. wpf 窗体内容旋转效果 网摘
  4. XMPP即时通讯
  5. XCode 7上传遇到ERROR ITMS-90535 Unexpected CFBundleExecutable Key. 的解决办法(转)
  6. getDeclaredConstructor()与getConstructor的差别
  7. SQL导入txt以及SQL中的时间格式操作
  8. 调试设置移动端Web开发环境搭建实践
  9. JavaWeb之Filter、Listener
  10. [LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器
  11. UA大全
  12. mysql杯观锁与乐观锁
  13. Job集群设计
  14. salesforce lightning零基础学习(十一) Aura框架下APP构造实现
  15. 声源测向: TDOA-GCC-PATH方法
  16. json如果不在pom中添加依赖会抛出500异常
  17. 启动vmware虚拟机报错:“无法获得VMCI驱动程序的版本:句柄无效”
  18. 标准JSF的生命周期
  19. Serial Wire Debugging the STM32 via the Bus Pirate
  20. Notification详解(含工具类)

热门文章

  1. QT中的线程与事件循环理解(2)
  2. Codeforces Round #264 (Div. 2) E. Caisa and Tree 树上操作暴力
  3. [au3]复制选择性粘贴文本到excel
  4. ASP.NET Web API 框架研究 Controller创建 HttpController介绍
  5. ElementTriArgyris
  6. Java学习--基本数据类型的定义和运算2
  7. 雪花算法(snowflake)delphi版
  8. xampp 80端口被占用后这么办??解决了
  9. 背水一战 Windows 10 (59) - 控件(媒体类): Image, MediaElement
  10. PHP进行数据库操作时遇到的一个问题