Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 日期被添加到数据库中,但月份增加1。为什么呢?_Java_Mysql_Hibernate_Date_Calendar - Fatal编程技术网

Java 日期被添加到数据库中,但月份增加1。为什么呢?

Java 日期被添加到数据库中,但月份增加1。为什么呢?,java,mysql,hibernate,date,calendar,Java,Mysql,Hibernate,Date,Calendar,我将html字段中的日期作为date/month/year。如果输入为: 01/07/2013 mysql数据库中的日期为2013-08-01。因此,月份增加1。为什么呢 下面的代码段将日期插入DB from html字段 String dateMonthYear[] = issueDate.split("/"); System.out.println("DATE MONT YEAR-------> "+dateMonthYear[1]);

我将
html
字段中的日期作为
date/month/year
。如果输入为:

01/07/2013
mysql
数据库中的日期为
2013-08-01
。因此,月份增加1。为什么呢

下面的代码段将日期插入DB from html字段

        String dateMonthYear[] = issueDate.split("/");

        System.out.println("DATE MONT YEAR-------> "+dateMonthYear[1]);
        // PRINTS THE CORRECT MONTH

        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.YEAR, Integer.parseInt(dateMonthYear[2]));
        cal.set(Calendar.MONTH, Integer.parseInt(dateMonthYear[1]));
        cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateMonthYear[0]));
        Date issueDateDB = cal.getTime();

        issued.setDateOfIssue(issueDateDB);

        // commit operation
db
中对应类型的类型为
Date
。是什么原因使蛾的数量增加了1

这是因为(
Calendar.janur=0
,依此类推)。在向数据库写入数据时,在月份部分中添加1,在从中读取数据时减去1

我建议您将此阅读到类似的问题。

这是因为(
Calendar.janur=0
,依此类推)。在向数据库写入数据时,在月份部分中添加1,在从中读取数据时减去1


我建议您阅读这篇文章来回答一个类似的问题。

因为日历的月份从0开始,而不是从1开始,例如:-因为月份的值是0而不是1。所以您必须将月份减少-1

只是因为日历的月份从0开始,而不是从1开始,例如:-因为月份的值是0而不是1。所以您必须将月份减少-1

Date d = new SimpleDateFormat("dd/MM/yyyy").parse("01/07/2013");
试试这个


改为试试这个

只要打印就行了proper@shreyanshjogi是的。
issueDateDB.getTime
prints
2013年7月1日下午12:42:51
刚刚打印issueDateDB就要来了proper@shreyanshjogi是的..
issuedDateDB.getTime
prints
7月1日,2013年7月1日下午12:42:51
但您可以看到..
issuedDateDB.getTime
打印
2013年7月1日下午12:42:51
而不是
2013年8月12日
。为什么会这样?但是你看,你设置了一个1..12的值,但是API期望的值是0..11。这是正确的。Shreyans Jogi提出了一个不同的。。。也许更好。。。使用SimpleDataFormat().parse()。@paulsm4的方法。您设置的值为1..12,但API预期的值为0..11..“我没有理解您的意思。该点正好在将日期插入数据库之前,月份是
july
@苏海尔古普塔那很有趣。。。Calendar cal=Calendar.getInstance()的输出是什么;校准设置(日历月,1);System.out.println(cal.getTime())?这里我得到了“Fri Feb 01(…)”@RandomSeed same.
Fri Feb 01 13:55:17 IST 2013
但是你看到了.
issuedDateDB.getTime
打印了
2013年7月1日下午12:42:51
而不是
2013年8月12日
。为什么会这样?但是你看,你设置了一个1..12的值,但是API期望的值是0..11。这是正确的。Shreyans Jogi提出了一个不同的。。。也许更好。。。使用SimpleDataFormat().parse()。@paulsm4的方法。您设置的值为1..12,但API预期的值为0..11..“我没有理解您的意思。该点正好在将日期插入数据库之前,月份是
july
@苏海尔古普塔那很有趣。。。Calendar cal=Calendar.getInstance()的输出是什么;校准设置(日历月,1);System.out.println(cal.getTime())?这里我得到了“Fri Feb 01(…)”@RandomSeed same.
Fri Feb 01 13:55:17 IST 2013