-1

i want to add one month in current date with this format "YYYY-MM-DD" i want function which return date type thanks Example

2020-04-13 will be 2020-05-13 

if there is no way please how can i use STR_TO_DATE here

String requeteInsert = "INSERT INTO `jardin`.`abonnement` (`id`,`enf_id`,  `data_debut`, `date_fin`,  `type`,`description`, `statu`, `statu_paiment`) VALUES ( '" + A.getId()+  "','" + A.getEnf_id()+  " ','"  + A.getData_debut()+  "','" + A.getDate_fin()+  "','" + A.getType()+  "','" + A.getDescription()+"','" + A.getStatu()+"','" + A.getStatu_paiment()+"');";

i'm already use LocalDate and other most of them return String after add a month .

thanks so much

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Ayoub Bsl
  • 159
  • 2
  • 6
  • 14

1 Answers1

1

As a starter: you should use prepared statements instead of concatenating variables in the query string, which is error-prone, unsafe and inefficient.

When it comes to doing date computation: you can do the arithmetics directly in the database. This is somewhat simpler than performing it in your application code.

In MySQL, if you want the current date plus 1 month, just do: current_date + interval 1 month.

Here is an example of your statement, assuming that you want the new date in column date_fin:

INSERT INTO `jardin`.`abonnement` (
    `id`,
    `enf_id`,
    `data_debut`,
    `date_fin`,
    `type`,
    `description`, 
    `statu`, 
    `statu_paiment`
) VALUES (
    ?,
    ?,
    ?,
    current_date + interval 1 month,
    ?,
    ?,
    ?,
    ?
)

Alternatively, if you want to add one month to a date given as parameter:

INSERT INTO `jardin`.`abonnement` (
    `id`,
    `enf_id`,
    `data_debut`,
    `date_fin`,
    `type`,
    `description`, 
    `statu`, 
    `statu_paiment`
) VALUES (
    ?,
    ?,
    ?,
    ? + interval 1 month,
    ?,
    ?,
    ?,
    ?
)

PS: date parameters should be given in format YYYY-MM-DD, with an optional time part (YYYY-MM-DD HH:MI:SS).

GMB
  • 216,147
  • 25
  • 84
  • 135