java - Subtract one day from date with format as mm/dd/yyyy -


this question has answer here:

i have date field being placed .csv , need subtract 1 day each date. these dates taken 1 csv string variable, , placed string variable. these dates formatted mm/dd/yyyy, , need subtract 1 day each of these dates. looking best solution on how this.

for example have date 7/28/2016 need taken 7/27/2016.

all appreciated, in advance.

edit: appreciate answers fast, ended separating steps first comment, have used jodatime answer went other options seeing around.

this code:

private string subtractdate(string date) {         simpledateformat formatter = new simpledateformat("mm/dd/yyyy");         string returnvalue = "";         try {             date newdate = formatter.parse(date);             calendar cal = calendar.getinstance();             cal.settime(newdate);             cal.add(calendar.date, -1);             date minusone = cal.gettime();             returnvalue = formatter.format(minusone);         } catch (exception e) {             e.printstacktrace();         }         return returnvalue;     } 

this working correctly now, again appreciate answers. i'm going accept first answer see work, did not go route.

simpledateformat dateformat = new simpledateformat("mm/dd/yyyy");  date date = dateformat.parse("your date string"); 

after can subtract day using jodatime.

datetime datetime = new datetime(date); 

datetime.minusdays(1); `


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -