0

Hello i have a problem with the result of java.sql.Date. This is my code with adding 1 week from current date

Date kini = new Date();
java.sql.Date jadwalPengobatan = new java.sql.Date(kini.getTime()+7*24*60*60*1000);

If current date is 2016-02-27, then the result in my MySQL is 2016-03-05. But if i want to add 1 month (for my case its always 31 days) from current date using this line code

 java.sql.Date jadwalPengobatan = new java.sql.Date(kini.getTime()+31*24*60*60*1000);

It should be 2016-03-28 but in my MySQL result is 2016-02-05. Anyone can help me whats wrong with this result??

6 Answers6

3

java.time

The other Answers use the outmoded old date-time classes. After proving to be confusing, troublesome, and flawed, they have been supplanted by the java.time framework in Java 8 and later.

Do not use the java.sql classes in your business logic. They are a messy extension of those old date-time classes, and are a badly-designed hack. Use them only for transferring your data in/out of the database. Convert to java.time types immediately.

To convert, use the new methods added to the old classes.

In the case of java.sql.Date, it is pretending to hold a date-only value without time-of-day nor time zone. The java.time classes include a true date-only value, LocalDate. To convert from java.sql.Date to java.time.LocalDate, call java.sql.Date::toLocalDate.

LocalDate ld = mySqlDate.toLocalDate();

Adding a true month is built-in.

LocalDate monthLater = ld.plusMonths( 1 );

Convert back to java.sql for storage in the database.

java.sql.Date sqlDate = java.sql.Date.valueOf( monthLater );

Hopefully some day we will see the JDBC drivers updated to support the java.time types directly. Then the java.sql types will fade away as old relics of the past. But until then we need to perform these java.sqljava.time conversions.

Update: JDBC 4.2 and later provides for passing java.time types directly with PreparedStatement::setObject, and fetching with ResultSet::getObject. So no need for conversion to java.sql types.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Use Calendar:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
Date result = cal.getTime();
Sardor Dushamov
  • 1,665
  • 3
  • 17
  • 44
0

One solution to your problem would be to alter the date in MySQL using the DATE_ADD() function:

DATE_ADD(yourDate, INTERVAL 1 MONTH)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

The problem with your solution is that you are not doing the calculations in long.

You have to replace 31*24*60*60*1000 with 31l*24l*60l*60l*1000l.

Here is the corrected code snippet:

public static void main (String[] args)
{
    Date kini = new Date();
    java.sql.Date jadwalPengobatan = new java.sql.Date(kini.getTime() + 31l*24l*60l*60l*1000l);
    System.out.println(jadwalPengobatan);
}

Please note that this will only fix your calculation where you are adding 31 days to the current date. If you are looking to add a month which can be of 30, 28 or 29 days also then perhaps you should make use of Calendar.

Today's Date:

2016-02-27

Output:

2016-03-29
user2004685
  • 9,548
  • 5
  • 37
  • 54
0
  SimpleDateFormat date= new SimpleDateFormat("dd/MM/yyyy");
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date()); 
    calendar.add(Calendar.MONTH, 1); 
    String output= date.format(calendar.getTime());

java.sql.Date jadwalPengobatan = new java.sql.Date(output);
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
0

You could do the following, using Calendar:

Date kini = new Date(); 
Calendar cal = Calendar.getInstance();
cal.setTime(kini);
cal.add(Calendar.DATE, 7); // add 10 days, e.g. 1 week
cal.add(Calendar.DATE, 31); // add 31 days, e.g. ~1 month
java.sql.Date jadwalPengobatan = new java.sql.Date(cal.getTime());

Also see this discussion.

Froodooo
  • 177
  • 1
  • 9