best way to add a month to a Give date and get that aniversary date in a String using java -


this question has answer here:

for example: have date in formate (mm/dd/yyyy)

    string currentmaturitydate = 02/03/2017     want date after adding 1 month maturity date in        string    expected_date = maturity date + 1 month.  

i have done given below , know better way , can done less code

    simpledateformat sdf = new simpledateformat("mm/dd/yyyy");     try {         calendar cal = calendar.getinstance();         date dt =   sdf.parse(currentmaturitydate);         cal.settime(dt);         cal.add(calendar.month, 1);         date dt1 = cal.gettime();          expected_date = sdf.format(dt1);     } catch (parseexception e) {         // todo auto-generated catch block         e.printstacktrace();     } 

it seems did few years ago, converting milliseconds, doing calculation. i'll see if can find code, making 30 or 31 days converting milliseconds. did indeed that. made long object , converted so.

long dbtimestmp = sysdateret.gettime(); final long thurtydaydiff = 2592000000l; //long dbtimest2 = sysdate2.gettime(); long secthirty = dbtimestmp - thurtydaydiff; final long tempvallg = 2592000000l; long daydiffconst = dbtimestmp - tempvallg;  simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); sdf.settimezone(timezone.gettimezone("america/dallas")); string finalretsql = sdf.format(new java.util.date()); java.sql.date finalretsql2 = java.sql.date.valueof(finalretsql); string enddateresult = (sdf.format(new java.sql.date(daydiffconst))); java.sql.date endsqldate = date.valueof(enddateresult); 

then later added calendar objects. not sure if needed or not though. used these calculations deal on action items had date difference needed, do.

hope helps.


Comments