===java8中时间的各种转换(LocalDateTime)===

1.将LocalDateTime转为自定义的时间格式的字符串

public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
  return localDateTime.format(formatter);
}

2.将long类型的timestamp转为LocalDateTime

public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
  Instant instant = Instant.ofEpochMilli(timestamp);
  ZoneId zone = ZoneId.systemDefault();
  return LocalDateTime.ofInstant(instant, zone);
}

3.将LocalDateTime转为long类型的timestamp

public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
  ZoneId zone = ZoneId.systemDefault();
  Instant instant = localDateTime.atZone(zone).toInstant();
  return instant.toEpochMilli();
}

4.将某时间字符串转为自定义时间格式的LocalDateTime

public static LocalDateTime parseStringToDateTime(String time, String format) {
  DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
  return LocalDateTime.parse(time, df);
}

链接:https://blog.csdn.net/wsywb111/article/details/79815481

最新文章

  1. Search for a Range
  2. __thread关键字[转]
  3. unity3d 特殊文件夹和脚本编译顺序
  4. poj蚂蚁问题
  5. mongoexport导出数据
  6. Android实时获取音量(单位:分贝)
  7. 安卓---apk反编译
  8. PAT乙级1004. 成绩排名 (20)
  9. highstock
  10. 开发快速定位需求(Coding之前的工作)
  11. shell脚本解压多个jar包
  12. windows端运行.sh脚本
  13. Kibana查询
  14. (转)Java动态追踪技术探究
  15. 招聘移动APP、接口、自动化、性能和安全方面的兼职测试讲师
  16. JAVA中接口与抽象类
  17. Thread.currentThread().getContextClassLoader().getResourceAsStream
  18. php把数据转换为json格式
  19. 使用terraform-provider-s3 操作minio
  20. R语言编程艺术(3)R语言编程基础

热门文章

  1. CF468C 【Hack it!】
  2. Hash算法解决冲突的四种方法
  3. 初识es
  4. js如何将汉字转化为拼音
  5. 【python】学习笔记之遇到的坑print输出报错
  6. Windows上node.js的多版本管理工具
  7. 【软工实践】Alpha冲刺(5/6)
  8. uniapp - 微信公众号授权登录
  9. gradle 使用maven repository 的设置
  10. Laravel 入门常见问题汇总