Date Conversion from String to sql Date in Java giving different output?(Java中从字符串到sql日期的日期转换给出不同的输出?)
问题描述
我有一个字符串形式的日期.我必须将其更改为 Sql Date.所以为此我使用了以下代码.
I have a string form of Date. I have to change it to Sql Date. so for that i have used the following code.
String startDate="01-02-2013";
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date date = sdf1.parse(startDate);
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
当我使用上面的代码并运行它时.我得到以下输出.
when i used the above code and run that. I got the following output.
2013-01-01.
此处月份未正确转换.
请告诉我问题出在哪里并提供示例代码以获得正确的结果?
Here Month is not converted correctly.
Please tell me where is the problem and provide sample code to get correct result?
推荐答案
mm 是 minutes.你想要 MM 个月:
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
不要难过 - 这个错误经常出现.
Don't feel bad - this exact mistake comes up a lot.
这篇关于Java中从字符串到sql日期的日期转换给出不同的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java中从字符串到sql日期的日期转换给出不同的输出?
基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
