java - How can I create a DateTime String using a date String and time String? -


i want create datetime string format 2013-08-30t15:06:00 using 3 strings.one string has date "30-aug-2013" , second string has time "15:06" , last string has days value "2".now want use these 3 strings create resulted string "2013-08-28t15:06:00". days value create date in past, in case date changes 30th aug 28 aug

solution have tried:

public string getformatteddate(string date, string selecteddays, string time) {     string dtstart = date;       simpledateformat  format = new simpledateformat("dd-mm-yyyy");       try {           date dateobject = format.parse(dtstart);           system.out.println(date);           calendar calendar = new gregoriancalendar();         calendar.settime(dateobject);         calendar.add(calendar.day_of_month, integer.valueof(selecteddays));         system.out.println(calendar.gettime());      } catch (parseexception e) {           // todo auto-generated catch block           e.printstacktrace();       } } 

in above solution calendar.add add days instead of removing days date.also don't know how use time string set on date object.

if time string in format, you'll need use time.split(":") hours , minutes.

you might want specify output format instead of using locale default format also.

add minus sign substract instead of adding calendar.add().

your code use dd-mm-yyyy month part written, 08-aug-2013 should parsed using dd-mmm-yyyy instead.

this should output specified

  public static string getformatteddate(string date, string selecteddays, string time) {     string dtstart = date;     calendar calendar = new gregoriancalendar();     calendar.clear();     simpledateformat outputformat = new simpledateformat("yyyy-mm-dd't'hh:mm:ss");     simpledateformat format = new simpledateformat("dd-mmm-yyyy"); // should mmm , not mm according date format 08-aug-2013     try {       date dateobject = format.parse(dtstart);       system.out.println(date);       calendar.settime(dateobject);       string[] hoursmins = time.split(":");       int hours = integer.valueof(hoursmins[0]);       int minutes = integer.valueof(hoursmins[1]);       calendar.set(calendar.hour_of_day, hours);       calendar.set(calendar.minute, minutes);       calendar.set(calendar.second, 0); // here, have no idea seconds, set them 0       calendar.add(calendar.day_of_month, -integer.valueof(selecteddays)); // add minus sign substract       // system.out.println(calendar.gettime());       // use simpledateformat instead        system.out.println(outputformat.format(calendar.gettime()));       // system.out.println(calendar.gettime());     } catch (parseexception e) {       // todo auto-generated catch block       e.printstacktrace();     }      return outputformat.format(calendar.gettime());   } 

for place read formatting symbols, check the bottom of link, it's pretty explained.

calling getformatteddate("08-aug-2013", "2", "15:05"); code output 2013-08-06t15:05.

edit : forgot seconds, output : 2013-08-06t15:05:00


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -