博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java8 LocalDateTime 类型转换
阅读量:7263 次
发布时间:2019-06-29

本文共 1450 字,大约阅读时间需要 4 分钟。

hot3.png

1、用DateTimeFormatter转换一个LocalDateTime类型

import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;public class TestDate1 {    public static void main(String[] args) {        //Get current date time        LocalDateTime now = LocalDateTime.now();        System.out.println("Before : " + now);        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");        String formatDateTime = now.format(formatter);        System.out.println("After : " + formatDateTime);          }}
Before : 2016-11-09T11:44:44.797After  : 2016-11-09 11:44:44

 

2、String转LocalDateTime

import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;public class TestDate2 {    public static void main(String[] args) {        String now = "2016-11-09 10:30";        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");        LocalDateTime formatDateTime = LocalDateTime.parse(now, formatter);        System.out.println("Before : " + now);        System.out.println("After : " + formatDateTime);        System.out.println("After : " + formatDateTime.format(formatter));    }}
Before : 2016-11-09 10:30After : 2016-11-09T10:30After : 2016-11-09 10:30

 

3、timestamp转localDateTime

Timestamp time = new Timestamp(LocalDateTime.now());LocalDateTime startTime = time.toLocalDateTime();

4、long类型转timestamp类型

Timestamp time = new Timestamp((long)1525607567619);

 

转载于:https://my.oschina.net/niithub/blog/1807841

你可能感兴趣的文章
python 3串口操作
查看>>
单张图片轮换
查看>>
服务器安全设置(转载)
查看>>
pygame编写贪吃蛇
查看>>
IE 兼容模式
查看>>
利用组策略禁用Oultook 各个版本的缓存模式!
查看>>
asp.net中Repeater控件用法笔记
查看>>
nginx源代码分析--配置文件解析
查看>>
8大排序算法总结 JS 实现
查看>>
python singleton
查看>>
DFS PKU 1562
查看>>
单例、观察者、代理、备忘录、工厂
查看>>
利用CDLinux里面的水滴破解路由器密码的教程
查看>>
Matlab lugui
查看>>
WPF 图片显示中的保留字符问题
查看>>
【Xamarin挖墙脚系列:移动设备应用的开发周期及准则】
查看>>
【Android】Http Digest 认证在android平台的实现
查看>>
[LeetCode] Sparse Matrix Multiplication 稀疏矩阵相乘
查看>>
Java递归算法——三角数字
查看>>
AngularJS 模态对话框
查看>>