simpledateformat - Java: Date Time Conversion -
i have date format "sa25may"; need convert date time variable , want add 1 day in that. , need return answer in same format. please needful
try { string str_date = "sa25may"; dateformat formatter; date date; formatter = new simpledateformat("ddd-dd-mmm"); date = (date) formatter.parse(str_date); system.out.println("today " + date); } catch (exception e) { e.printstacktrace(); }
error:
java.text.parseexception: unparseable date: "sa25may" @ java.text.dateformat.parse(dateformat.java:337) @ javadatatable.javadatatable.main(javadatatable.java:29)
here don't know how resolve problem.
you can add 1 day if know year because of leap years (29th of february).
in case year current year, following solution should the job:
for "sa25may":
try { string str_date = "sa25may"; // remove sa str_date = str_date.replacefirst("..", ""); // add current year calendar c = calendar.getinstance(); str_date = c.get(calendar.year) + str_date; // parse date date date; simpledateformat formatter = new simpledateformat("yyyyddmmm"); date = formatter.parse(str_date); system.out.println("today " + date); // add day c.settime(date); c.add(calendar.date, 1); // rebuild old pattern new date simpledateformat formatter2 = new simpledateformat("eeeddmmm"); string tomorrow = formatter2.format(c.gettime()); tomorrow = tomorrow.touppercase(); tomorrow = tomorrow.substring(0, 2) + tomorrow.substring(3); system.out.println("tomorrow " + tomorrow); } catch (exception e) { e.printstacktrace(); }
or "sa-25-may":
try { string str_date = "sa-25-may"; // remove sa str_date = str_date.replacefirst("..-", ""); // add current year calendar c = calendar.getinstance(); str_date = c.get(calendar.year) + "-" + str_date; // parse date date date; simpledateformat formatter = new simpledateformat("yyyy-dd-mmm"); date = formatter.parse(str_date); system.out.println("today " + date); // add day c.settime(date); c.add(calendar.date, 1); // rebuild old pattern new date simpledateformat formatter2 = new simpledateformat("eee-dd-mmm"); string tomorrow = formatter2.format(c.gettime()); tomorrow = tomorrow.touppercase(); tomorrow = tomorrow.substring(0, 2) + tomorrow.substring(3); system.out.println("tomorrow " + tomorrow); } catch (exception e) { e.printstacktrace(); }
Comments
Post a Comment