当前位置:网站首页>日期 数据库日期 字符串 之间互相转换
日期 数据库日期 字符串 之间互相转换
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();
边栏推荐
- 【QT小点】实现看门狗功能,检测外部程序是否在运行
- 【Cocos Creator 3.5.1】event.getButton()的使用
- Small program of C language practice (consolidate and deepen the understanding of knowledge points)
- 汇编语言-王爽 第8章 数据处理的两个基本问题-笔记
- Wechat applet refreshes the current page
- [FPGA] design and implementation of frequency division and doubling based on FPGA
- Using CSDN to develop cloud and build navigation websites
- Spark 之 WholeStageCodegen
- 爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)
- 【Cocos Creator 3.5.1】this.node.getPosition(this._curPos)的使用
猜你喜欢
随机推荐
openresty使用文档
Spark 之 WholeStageCodegen
JVM整体结构解析
【Cocos Creator 3.5.1】input. Use of on
表单校验 v-model 绑定的变量,校验失效的解决方案
DAST black box vulnerability scanner part 6: operation (final)
Matlab quickly converts two-dimensional coordinates of images into longitude and latitude coordinates
Force buckle 179, max
Double position relay jdp-1440/dc110v
我对于测试团队建设的意见
Leetcode99 week race record
Two position relay xjls-8g/220
JVM tuning ideas
Using CSDN to develop cloud and build navigation websites
Go log -uber open source library zap use
[cocos creator 3.5.1] addition of coordinates
C语言练手小项目(巩固加深知识点理解)
LeetCode-515. Find the maximum value in each tree row
JVM overall structure analysis
MATLAB快速将影像的二维坐标转换为经纬度坐标









