JAVA 各种时间类型转换
final Date date = new Date();
final Timestamp timestamp = new Timestamp(date.getTime());
final Calendar calendar = Calendar.getInstance(); final Instant instant = Instant.now();
final LocalDateTime localDateTime = LocalDateTime.now();
final ZonedDateTime zonedDateTime = ZonedDateTime.now(); initialize
转Date
//Timestamp转Date
Date timestampDate = new Date(timestamp.getTime());
//Calendar转Date
Date calendarDate = calendar.getTime();
//Instant转Date
Date instantDate = Date.from(instant);
//LocalDateTime转Date
Date localDateTimeDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
//ZonedDateTime转Date
Date zonedDateTimeDate = Date.from(zonedDateTime.toInstant());
转Instant
//Date转Instant
Instant dateInstant = date.toInstant();
//Timestamp转Instant
Instant timestampInstant = timestamp.toInstant();
//Calendar转Instant
Instant calendarInstant = calendar.toInstant();
//LocalDateTime转Instant
Instant localDateTimeInstant = localDateTime.toInstant(ZoneOffset.of(ZoneId.systemDefault().getId()));
//ZonedDateTime转Instant
Instant zonedDateTimeInstant = zonedDateTime.toInstant();
转LocalDateTime
//Date转LocalDateTime
LocalDateTime dateLocalDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
//Timestamp转LocalDateTime
LocalDateTime timestampLocalDateTime = timestamp.toLocalDateTime();
//Calendar转LocalDateTime
LocalDateTime calendarLocalDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault());
//Instant转LocalDateTime
LocalDateTime instantLocalDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
//ZonedDateTime转LocalDateTime
LocalDateTime zonedDateTimeLocalDateTime = zonedDateTime.toLocalDateTime();
转ZonedDateTime
//Date转ZonedDateTime
ZonedDateTime dateZonedDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
//Timestamp转ZonedDateTime
ZonedDateTime timestampZonedDateTime = ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
//Calendar转ZonedDateTime
ZonedDateTime calendarZonedDateTime = ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
//Instant转ZonedDateTime
ZonedDateTime instantZonedDateTime = instant.atZone(ZoneId.systemDefault());
//LocalDateTime转ZonedDateTime
ZonedDateTime localDateTimeZonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
转Timestamp
//Date转Timestamp
Timestamp dateTimestamp = new Timestamp(date.getTime());
//Calendar转Timestamp
Timestamp calendarTimestamp = new Timestamp(calendar.getTimeInMillis());
//Instant转Timestamp
Timestamp instantTimestamp = Timestamp.from(instant);
//LocalDateTime转Timestamp
Timestamp localDateTimeTimestamp = Timestamp.valueOf(localDateTime);
//ZonedDateTime转Timestamp
Timestamp zonedDateTimeTimestamp = Timestamp.from(zonedDateTime.toInstant());
转Calendar
//Date转Calendar
Calendar dateCalendar = Calendar.getInstance();
dateCalendar.setTime(date);
//Timestamp转Calendar
Calendar timestampCalendar = Calendar.getInstance();
timestampCalendar.setTimeInMillis(timestamp.getTime());
//Instant转Calendar
Calendar instantCalendar = GregorianCalendar.from(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()));
//LocalDateTime转Calendar
Calendar localDateTimeCalendar = GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()));
//ZonedDateTime转Calendar
Calendar zonedDateTimeInstantCalendar = GregorianCalendar.from(zonedDateTime);
JAVA 各种时间类型转换的更多相关文章
- java时间类型转换 JsonValueProcessor
问题描述: java里面时间类型转换成json数据就成这样了:"createTime":{"date":30,"day":3,"h ...
- 将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型
问题描述: java里面时间类型转换成json数据就成这样了: "createTime":{"date":30,"day":3," ...
- Java 日期时间 Date类型,long类型,String类型表现形式的转换
Java 日期时间 Date类型,long类型,String类型表现形式的转换 1.java.util.Date类型转换成long类型 java.util.Date dt = new Date(); ...
- Java中数据类型转换大全(个人总结)
一.字符串转换为其他类型 1.将字符串转化为int型 (1)方法一 int i = Integer.parseInt(String str); (2)方法二 int i = Integer.value ...
- 数据库时间类型和 util 包下时间类型转换
Java 中的类型 1. java.sql 包下给出三个数据库相关的日期时间类型,分别是 java.sql.Date, 表示日期,只有年月日,没有时分秒. java.sql.Time, 表示时间, 只 ...
- Java 日期时间 Date类型,long类型,String类型表现形式的转换 (转)
Java 日期时间 Date类型,long类型,String类型表现形式的转换 1.java.util.Date类型转换成long类型java.util.Date dt = new Date();Sy ...
- Java实现时间动态显示方法汇总
这篇文章主要介绍了Java实现时间动态显示方法汇总,很实用的功能,需要的朋友可以参考下 本文所述实例可以实现Java在界面上动态的显示时间.具体实现方法汇总如下: 1.方法一 用TimerTask: ...
- java中强制类型转换
在Java中强制类型转换分为基本数据类型和引用数据类型两种,这里我们讨论的后者,也就是引用数据类型的强制类型转换. 在Java中由于继承和向上转型,子类可以非常自然地转换成父类,但是父类转换成子类则需 ...
- Java 对时间和日期的相关处理
1. 获取当前系统时间和日期并格式化输出 import java.util.Date; import java.text.SimpleDateFormat; public class NowStrin ...
随机推荐
- Python + unittest知识点回顾
postman 安装Newman 先安装node.js,把npm添加到环境变量中. npm install newman --registry=https://registry.npm.taobao. ...
- 祝贺|合肥.NET俱乐部第二期技术沙龙活动圆满成功
热烈祝贺合肥.NET俱乐部第二期技术沙龙圆满成功,感恩参与活动的每一位小伙伴!正是因为有你们才促成了这次聚会的成功.现对此次活动进行简单回顾并附上精彩的活动图片,每一位参与活动者名单,以及此次活动讲师 ...
- 微信小程序云开发-云存储-上传单个视频到云存储并显示到页面上
一.wxml文件 <!-- 上传视频到云存储 --> <button bindtap="chooseVideo" type="primary" ...
- 微信小程序云开发-数据库-更新数据
一.js文件代码使用.update更新数据 写一个更新数据的函数,函数内使用.update更新数据.一定要通过.doc指定修改哪一条数据. 二.wxml文件修改数据的按钮 在wxml文件中写[修改] ...
- Spring 框架中都用到了哪些设计模式
Spring 框架中都用到了哪些设计模式? Spring 框架中使用到了大量的设计模式,下面列举了比较有代表性的: 1.代理模式-在 AOP 和 remoting 中被用的比较多. 2.单例模式:在 ...
- 第十四篇 -- CPU学习二——此部分重点在AMD CPU
一.CPU的一些英文简写或解释 Definitions: ACPI--Advanced Configuration and Power Interface APP--Adjusted Peak Per ...
- 【阅读笔记】Java核心技术卷一 #2.Chapter4
4 对象和类 4.1 面向对象程序设计概述(略) 4.2 使用预定义类 java.time.LocalDate static LocalDate now(); static LocalDate of( ...
- Go是一门什么样的语言?
Go语言基本介绍 首先Go语言诞生于2007年由谷歌公司研发,2009年开源,2012年推出1.0版本,Go是一种语言层面支持并发(Go最大的特色.天生支持并发).内置runtime,支持垃圾回收(G ...
- 解决iOS上网页滑动不流畅问题
body { overflow:auto; /* 用于 android4+,或其他设备 */ -webkit-overflow-scrolling:touch; /* 用于 ios5+ */ }说明: ...
- Linux 基础指令初识
Linux 基础指令初识 01. ls 指令 语法: ls [选项] [目录或文件] 功能:对于目录,该命令列出该目录下的所有子目录与文件.对于文件,将列出文件名以及其他信息 -a 列出目录下的所有文 ...