从 java.util.Date 转换为 java.sql.Timestamp 的问题 [英] Issue converting from java.util.Date to java.sql.Timestamp

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

问题描述

我正在尝试将用户提供的日期放入 SQL 数据库中,我有以下几行来处理字符串并将其转换为 java.sql.Timestamp 对象.

I'm trying to put a user provided date into an SQL database, and I have the following lines to process the string and convert it to a java.sql.Timestamp object.

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm");
java.util.Date badDate = formatter.parse(request.getParameter("datetime")); 
Timestamp date = new Timestamp(badDate.getTime());

问题是,badDate 是用户输入的正确日期,但日期总是设置为错误的月份和日期,通常是 1 月 2 日,以及正确的时间和年份.当我尝试转换为 java.sql.Date 对象时遇到了同样的问题,但随后时间也设置为午夜.经过一番搜索,我找不到任何有类似问题的人,也许这里有人见过这样的事情?

The issue is, badDate is the correct date that the user input, but date always gets set to the wrong month and day, usually January 2, and the correct time and year. I had the same problem when I tried to convert to a java.sql.Date object, but then the time was set to midnight as well. I couldn't find anyone with a similar problem after some searching, maybe someone here has seen something like this?

推荐答案

日期格式错误,应该是yyyy-MM-dd'T'HH:mm.

The date format is wrong, it should be yyyy-MM-dd'T'HH:mm.

请参考本文档有关日期格式的详细信息.

Please refer this document for details about date format.

m 代表分钟
M 表示月
h 表示上午/下午的小时数 (1-12)
H 表示 24 小时制的小时

m stands for Minutes
M for Months
h for hours in am/pm (1-12)
H for hours in 24 hour clock

如果您能提供您尝试解析的日期值的示例会更好.

It will be better if you can give a sample of the date value you are trying to parse.

这篇关于从 java.util.Date 转换为 java.sql.Timestamp 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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