将日期转换为MYSQL日期格式 [英] Convert Date into MYSQL Date Format

查看:132
本文介绍了将日期转换为MYSQL日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以以下格式使用6个月的MySQL Date数据:

I have this MySQL Date data for 6 months in this format:

2010-01-012010-07-01

但是从UI中, ToDate FromDate 是以以下格式传递的:

But from the UI the ToDate and FromDate are passed in this format:

Jan 1, 2010July 1, 2010

请告诉我如何将这些数据转换为MySQL等效格式?

Please tell me how can I convert this data into MySQL equivalent format?

推荐答案

首先创建一个

First create a SimpleDateFormat for parsing your input from the UI:

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");

接下来,将输入解析为java.sql.Date(不幸的是,该名称与java.util.Date不同).例如:

Next parse an input into a java.sql.Date (which is unfortunately named and different from java.util.Date). So for example:

java.sql.Date date = new java.sql.Date(sdf.parse(fromDate).getTime());

最后,在进行数据库查询时,使用date传递给JDBC.如:

Finally use the date to pass to JDBC when making your database queries. Such as:

Connection con; // assuming you have a database connection
PreparedStatement ps = con.prepareStatement("SELECT * FROM table WHERE x = ?");
ps.setDate(1, date);
ResultSet resultSet = ps.executeQuery();

这篇关于将日期转换为MYSQL日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆