1

I'm having trouble, finding out how to calculate or get the date from mysql (using java program) after 1 month of the initial saved date given below:

Date rDate = new Date();
java.sql.Date regDate = new java.sql.Date(rDate.getTime());

I am saving the date into a date column in mysql and I want to have another column which contains the date but one month ahead. In other words I have a registration date and I want to have an expiration date calculated automatically which allows only 1 month. Is it possible?

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
Randy
  • 13
  • 6

2 Answers2

3

Grabbing the current date and set it into the Calendar format and add 1 to the month.

You can give this a try.

Date rDate = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(rDate);
cal.add(Calendar.MONTH, 1);
Alvin
  • 408
  • 3
  • 14
3

You can use Calendar class to manipulate date fields:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
Date futureDate = cal.getTime();
ujulu
  • 3,289
  • 2
  • 11
  • 14
  • Thank you @ujulu. I had searched a lot for the answer. Guessed I should have just read up on the Calendar class. – Randy Aug 02 '16 at 21:30