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日期的日期转换给出不同的输出?
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01