当前位置:网站首页>日期 数据库日期 字符串 之间互相转换
日期 数据库日期 字符串 之间互相转换
2022-06-27 06:03:00 【YUELEI118】
日期:java.util.Date
数据库日期:java.sql.Date
日期 转换为 数据库日期
- 重点:日期的
getTime()获得时间戳,转换为数据库日期
// util.Date --> sql.Date
java.util.Date nowDate1 = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(nowDate1.getTime());
System.out.println("sqlDate = " + sqlDate);
数据库日期 转换为 日期
- 重点:数据库日期的
getTime()获得时间戳,转换为日期
java.util.Date Date = new java.util.Date(sqlDate.getTime());
日期 转换为 字符串
- 重点:SimpleDateFormat 类中的format()方法
// util.Date --> String
java.util.Date nowDate = new java.util.Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); //格式化工具
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");
String format = sf.format(nowDate);
System.out.println("format = " + format);
String format1 = sf1.format(nowDate);
System.out.println("format1 = " + format1);
字符串 转换为日期
- 重点:格式化工具中定义的参数格式必须与字符串的格式相同
SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd"); //格式化工具
String stDate = "2020/01/01"; // 字符串
java.util.Date parseDate = sf.parse(stDate);
System.out.println("parseDate = " + parseDate);
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");
String stDate1 = "2021年3月20日 19点20分30秒";
java.util.Date parseDate1 = sf1.parse(stDate1);
System.out.println("parseDate1 = " + parseDate1);
数据库日期 转换为 字符串
- 和日期转换为字符串是一样的方法
java.util.Date nowDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(nowDate.getTime());
SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd"); //格式化工具
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");
String format = sf.format(sqlDate);
String format1 = sf1.format(sqlDate);
System.out.println("format = " + format);
System.out.println("format1 = " + format1);
- 数据库日期的toString()方法
输出只有年月日,不会弄时分秒
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
System.out.println("sqlDate = " + sqlDate);
字符串 转换为 数据库日期
- 目前不会直接转,只能通过 日期 中转
LocalDate 与 sql.Date转换
sql.Date类中关于LocalDate的方法只有这两个
LocalDate 转换为 数据库日期
- 数据库日期中只有关于date的没有datetime,想要显示时分秒,估计还是要util.date
LocalDate nowLocalDate = LocalDate.now();
Date date = Date.valueOf(nowLocalDate);
System.out.println("date = " + date);
数据库日期 转换为 LocalDate
LocalDate localDate = sqlDate.toLocalDate();
边栏推荐
- 信息系统项目管理师---第七章 项目成本管理
- 多线程基础部分Part2
- Double position relay rxmd2-1mrk001984 dc220v
- 【QT小点】QT下载链接
- Wholestagecodegen of spark
- Free SSH and telnet client putty
- 1317. convert an integer to the sum of two zero free integers
- Two position relay rxmvb2 r251 204 110dc
- Built in functions of spark
- Quick personal site building guide using WordPress
猜你喜欢

Two position relay hjws-9440

汇编语言-王爽 第13章 int指令-笔记

openresty使用文档

IAR systems fully supports Centrino technology 9 series chips

My opinion on test team construction

JVM整体结构解析

Assembly language - Wang Shuang Chapter 13 int instruction - Notes
![[FPGA] realize the data output of checkerboard horizontal and vertical gray scale diagram based on bt1120 timing design](/img/80/c258817abd35887c0872a3286a821f.png)
[FPGA] realize the data output of checkerboard horizontal and vertical gray scale diagram based on bt1120 timing design

427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)

Proxy-Reflect使用详解
随机推荐
vscode korofileheader 的配置
expect脚本中使用scp命令的方法,expect脚本中scp命令获取不了值的问题完美解决方法
Comprehensive application of OpenCV in contour detection and threshold processing
Wholestagecodegen of spark
Dev++ 环境设置C语言关键字显示颜色
The SCP command is used in the expect script. The perfect solution to the problem that the SCP command in the expect script cannot obtain the value
Unicast, multicast and broadcast of IP network communication
[cocos creator 3.5.1] addition of coordinates
Redis4.0新特性-主动内存碎片整理
Proxy reflect usage details
使用 WordPress快速个人建站指南
Matlab quickly converts two-dimensional coordinates of images into longitude and latitude coordinates
【Cocos Creator 3.5.1】input. Use of on
427- binary tree (617. merge binary tree, 700. search in binary search tree, 98. verify binary search tree, 530. minimum absolute difference of binary search tree)
函数栈帧的形成与释放
How to check the frequency of memory and the number of memory slots in CPU-Z?
MATLAB快速将影像的二维坐标转换为经纬度坐标
Proxy-Reflect使用详解
LeetCode-515. Find the maximum value in each tree row
NLP-D62-nlp比赛D31&刷题D15