将java.util.Date转换为java.sql.Date会导致日期更改 [英] Converting java.util.Date to java.sql.Date resulting in date changes

查看:54
本文介绍了将java.util.Date转换为java.sql.Date会导致日期更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将java.util.Date转换为java.sql.Date时遇到问题.我传入的date参数最初是一个格式为2019-01-31的字符串.然后,我尝试使用以下代码将其转换为java.util.date:

I was having some problem when trying to convert java.util.Date to java.sql.Date. My date parameter passed in was originally a string in this format: 2019-01-31. Then I am trying to convert it to java.util.date using this code:

    SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");
    Date date = null;
    try {
        date =  formatter.parse(this._dDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

在我的SQL语句中,我试图将其转换为java.sql.Date:

In my SQL statement, I am trying to convert it to java.sql.Date:

buffer.append("SELECT * FROM TABLE1 WHERE LAST_LOGIN_DT < ");
buffer.append("TO_DATE('" + new java.sql.Date(date.getTime()) + "','YYYY-MM-DD') ");

但是,当我以字符串形式打印缓冲区时,我意识到日期已更改.例如,我传入的数据字符串是2018-01-01,但在SQL语句中它变成了2017-12-31.有什么想法吗?

However, when I printed out the buffer in string, I realized that the date is changed. For instance, the data string I passed in is 2018-01-01, but in the SQL statement it became 2017-12-31. Any ideas?

谢谢!

推荐答案

让我们首先看下面的示例:

Let's take a look at following example first:

String date = "2018-01-01";

SimpleDateFormat sdf1 = new SimpleDateFormat("YYYY-MM-DD");
System.out.println(sdf1.parse(date));
System.out.println(sdf1.parse(date).getYear());
System.out.println(sdf1.parse(date).getMonth());
System.out.println(sdf1.parse(date).getDay());

SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf2.parse(date));
System.out.println(sdf2.parse(date).getYear());
System.out.println(sdf2.parse(date).getMonth());
System.out.println(sdf2.parse(date).getDay());

控制台输出:

2017年12月31日星期日00:00:00 CST
117
11
0
2018年1月1日星期一10:00:00 CST
118
0
1

Sun Dec 31 00:00:00 CST 2017
117
11
0
Mon Jan 01 00:00:00 CST 2018
118
0
1

现在我想您已经注意到了差异!
引自 Class SimpleDateFormat :

Now I think you have already noticed the difference!
Quoted from Class SimpleDateFormat:

Y 表示星期,而 D 表示一年中的某天.
y 表示年份,而 d 表示每月的日期.

Y means Week year and D means Day in year.
y means Year and d means Day in month.

yyyy YYYY 都代表年份,而 yyyy 代表日历年,而 YYYY 代表年份.星期.并且, dd 代表日历日,而 DD 代表月份的日.因此,有两种方法可以解决您的问题:

yyyy and YYYY both represent a year but yyyy represents the calendar year while YYYY represents the year of the week. And, dd represents the calendar day while DD represents the day of the month. Therefore, there are 2 ways to fix your issue:

方法1
如果仍要转换 this._dDate,请将 SimpleDateFormat 的格式从 YYYY-MM-DD 更改为 yyyy-MM-dd .依次为 java.util.Date java.sql.Date ,但我强烈建议您不要这样做.

Method 1
Change the format for SimpleDateFormat from YYYY-MM-DD to yyyy-MM-dd if you still want to convert this._dDate to java.util.Date then java.sql.Date, but I strongly recommend you don't do this.

方法2
更好的方法是直接使用 java.sql.Date.valueOf(this._dDate),而 this._dDate 的格式应为 yyyy-MM-dd .

这篇关于将java.util.Date转换为java.sql.Date会导致日期更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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