===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

Java8时间转换的更多相关文章

  1. java8 时间使用

    为什么需要新的时间API 文章来源:https://www.cnblogs.com/guozp/p/10342775.html 在Java 8之前的日期/时间API之前,现有的与日期和时间相关的类存在 ...

  2. Java8 时间日期类操作

    Java8 时间日期类操作 Java8的时间类有两个重要的特性 线程安全 不可变类,返回的都是新的对象 显然,该特性解决了原来java.util.Date类与SimpleDateFormat线程不安全 ...

  3. Java日期时间API系列21-----Jdk8中java.time包中的新的日期时间API类,xk-time时间转换,计算,格式化,解析的工具

    通过工作之余,对Java8中java.time包源码的不断学习,使用和总结,开发了xk-time,初步完成,欢迎试用和提出建议! xk-time xk-time is a datetime conve ...

  4. 迄今为止最硬核的「Java8时间系统」设计原理与使用方法

    为了使本篇文章更容易让读者读懂,我特意写了上一篇<任何人都需要知道的「世界时间系统」构成原理,尤其开发人员>的科普文章.本文才是重点,绝对要读,走起! Java平台时间系统的设计方案 几乎 ...

  5. [jquery]将当前时间转换成yyyymmdd格式

    如题: function nowtime(){//将当前时间转换成yyyymmdd格式 var mydate = new Date(); var str = "" + mydate ...

  6. MySQL 日期、时间转换函数

    MySQL 日期.时间转换函数:date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式.它是 str_to ...

  7. java时间类型的转换/获取当前时间/将时间转换成String/将String转换成时间

    对于我的脑子,我已经服气了...写了N遍的东西,就是记不住...既然记不住那就记下来... 利用java获取当前的时间(String类型,年-月-日 时:分:秒) //我要获取当前的日期 Date d ...

  8. inner join ,left join ,right join 以及java时间转换

    1.inner join ,left join 与 right join (from 百度知道) 例表aaid adate1    a12    a23    a3表bbid  bdate1     ...

  9. Python基本时间转换

    时间转换 python中处理时间的时候,最常用的就是字符形式与时间戳之间的转换. 把最基本的转换在这里记下来 string -> timestamp import time import dat ...

随机推荐

  1. HAVING 搜索条件在进行分组操作之后应用

    HAVING 搜索条件在进行分组操作之后应用: 如:查询帖子访问量大于15的用户id: select t.user_id,u.name,sum(count_view) from t_topic t l ...

  2. 洛谷 CF448D Multiplication Table

    目录 题目 思路 \(Code\) 题目 CF448D Multiplication Table 思路 二分答案.这个矩阵的每一排都是递增的,所以二分\(ans\),去计算有多少个数等于\(ans\) ...

  3. Python3菜鸟教程笔记

    多行语句 同一行显示多条语句 Print 输出

  4. 转载:基于 Hive 的文件格式:RCFile 简介及其应用---推酷

    Hadoop 作为MR 的开源实现,一直以动态运行解析文件格式并获得比MPP数据库快上几倍的装载速度为优势.不过,MPP数据库社区也一直批评Hadoop由于文件格式并非为特定目的而建,因此序列化和反序 ...

  5. GoCN每日新闻(2019-10-19)

    GoCN每日新闻(2019-10-19) Go 1.13中的错误处理 https://tonybai.com/2019/10/18/errors-handling-in-go-1-13 golang核 ...

  6. SDN上机第四次作业

    1. 解压安装OpenDayLight控制器(本次实验统一使用Beryllium版本) 安装ODL后安装插件 2.3用Python脚本搭建如下拓扑,连接OpenDayLight控制器 拓扑如图: Py ...

  7. vue data不可以使用箭头函数的问题解析

    这篇文章主要介绍了vue data不可以使用箭头函数问题,本文通过源码解析给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 首先需要明确,a() {}和 b: () => {} ...

  8. Hbuilder提交项目到GitHub出现cannot open git-upload-pack

    问题描述 Hbuilder上传本地项目到GitHub时是通过下载的Egit插件,然而提交代码时出现下图问题 网上有说添加http的sslVerify=false,然并卵. 解决方案 不用hbuilde ...

  9. mybatis自动生成后无法获取主键id问题

    使用mybatis的自动生成工具生成的主键id: <selectKey resultType="java.lang.Integer" keyProperty="id ...

  10. 【python基础】python开启GPU加速

    前言 训练时使用GPU可以加速程序运行,本文介绍如何使用GPU加速. 前提条件 1. 机子有GPU显卡,并安装GPU显卡驱动: 2. 安装GPU的使用环境,CUDA等: 3. 打开nvidia-smi ...