Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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_Date - Fatal编程技术网

Java 将从数据库获取的日期(时间戳)增加1年

Java 将从数据库获取的日期(时间戳)增加1年,java,date,Java,Date,我通过bean从数据库中获取日期值。我想将从数据库获得的日期增加+1年。 有人能告诉我怎么做吗? 这就是我到目前为止所做的 if (reviewDate != null || !(reviewDate.equals("0"))) { //convert reviewDate string to date Date currentDate = new SimpleDat

我通过bean从数据库中获取日期值。我想将从数据库获得的日期增加+1年。 有人能告诉我怎么做吗? 这就是我到目前为止所做的

    if (reviewDate != null || !(reviewDate.equals("0"))) 
                {    
                    //convert reviewDate string to date
                    Date currentDate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(reviewDate);
                    System.out.println("currentDate = "+currentDate);

                    //get the current year
                    int currentMonth = currentDate.getMonth();
                    int currentDay = currentDate.getDate();
                    long currentYear = currentDate.getYear();
                    long currentTime = currentDate.getTime();   
                    System.out.println("current month="+currentMonth);
                    System.out.println("current day="+currentDay);
                    System.out.println("current year="+currentYear);
                    System.out.println("current time="+currentTime);

                    Date newDate = null;

                    //increment year
                    currentYear = currentYear+1;
                    System.out.println("current year after increment="+currentYear);

                    //add this to currentDate and assign to newDate
                    reviewDate = currentMonth + "/" + currentDay + "/" + currentYear + " " + currentTime;

                    System.out.println("ReviewDate=" + reviewDate);
                } 
我的输入是-04/22/1980 11:30:20 我的输出应该是-04/22/1981 11:30:20

然而,我得到的是3/22/81/32527982000

我想我不能使用日历,因为我不想按当前日期递增。有人能为我推荐一个有效的解决方案吗?

你可以试试这种方法

 String rDate="04/22/1980 11:30:20"; // your received String date
 DateFormat df=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");// date format
 Date date=df.parse(rDate);// parse String to date
 Calendar calendar=Calendar.getInstance();
 calendar.setTime(date); // set calender instance to date value
 calendar.add(Calendar.YEAR,1); // add one year to current
 System.out.println(df.format(calendar.getTime()));
输出:

 04/22/1981 11:30:20
关于
Java

您可以这样尝试

 String rDate="04/22/1980 11:30:20"; // your received String date
 DateFormat df=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");// date format
 Date date=df.parse(rDate);// parse String to date
 Calendar calendar=Calendar.getInstance();
 calendar.setTime(date); // set calender instance to date value
 calendar.add(Calendar.YEAR,1); // add one year to current
 System.out.println(df.format(calendar.getTime()));
输出:

 04/22/1981 11:30:20

关于
Java

以下是Java 8解决方案:


以下是Java 8解决方案:


您可以使用Calendar,只需先用setTime()调用即可。检查:您可以使用Calendar,只需先用setTime()调用即可。检查: