将string类型的时间转换成SQL date 型

本文介绍了一种将String类型的日期字符串转换为SQL Date格式的方法。通过使用SimpleDateFormat解析指定格式的字符串,然后将其转换为java.util.Date,最后创建SQL Date对象。
[size=medium][color=indigo]// 将string类型的时间转换成SQL date 型
public Date dateChange(String date1)
{
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
java.util.Date cDate;
Date dd=null;
try {
cDate = sdf.parse(date1);
dd =new Date(cDate.getTime());

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dd;

}[/color][/size]
Java 中,将时间类型字符串转换为可插入到 SQL 中 `DATE` 类型数据,可借助 `java.sql.Date` 类来实现。以下是几种常见的转换方法: #### 方法一:使用 `SimpleDateFormat` 类解析字符串并转换 ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.sql.Date; public class DateConversionExample { public static void main(String[] args) { String dateString = "2024-10-01"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { java.util.Date utilDate = sdf.parse(dateString); java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); System.out.println(sqlDate); } catch (ParseException e) { e.printStackTrace(); } } } ``` 在上述代码中,先使用 `SimpleDateFormat` 类按照指定的日期格式(`yyyy-MM-dd`)解析字符串,得到 `java.util.Date` 对象,再通过 `getTime()` 方法获取其时间戳,最后使用该时间戳创建 `java.sql.Date` 对象。 #### 方法二:使用 `DateTimeFormatter` 类(Java 8 及以上) ```java import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.sql.Date; public class DateConversionExampleJava8 { public static void main(String[] args) { String dateString = "2024-10-01"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate localDate = LocalDate.parse(dateString, formatter); java.sql.Date sqlDate = Date.valueOf(localDate); System.out.println(sqlDate); } } ``` 在 Java 8 及以上版本中,可使用 `DateTimeFormatter` 类按照指定的日期格式解析字符串,得到 `LocalDate` 对象,再通过 `Date.valueOf()` 方法将 `LocalDate` 对象转换为 `java.sql.Date` 对象。 ### 插入到 SQL 中 将转换后的 `java.sql.Date` 对象插入到 SQL 中,可使用 `PreparedStatement` 的 `setDate` 方法,示例如下: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.sql.Date; public class InsertDateExample { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/your_database"; String username = "your_username"; String password = "your_password"; String dateString = "2024-10-01"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { java.util.Date utilDate = sdf.parse(dateString); java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); try (Connection connection = DriverManager.getConnection(url, username, password); PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO your_table (date_column) VALUES (?)")) { preparedStatement.setDate(1, sqlDate); preparedStatement.executeUpdate(); } } catch (ParseException | SQLException e) { e.printStackTrace(); } } } ``` 在上述代码中,使用 `PreparedStatement` 的 `setDate` 方法将 `java.sql.Date` 对象插入到 SQL 语句的占位符中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值