android - Incorrect date conversion by SimpleDateFormat -
i using simpledateformat convert date dd-mm-yyyy yyyy-mm-dd not display year properly.i trying convert 18-5-2014 2014-05-18 getting 3914-05-18.
public void ondateset(datepicker view, int year,int monthofyear, int dayofmonth) { date selecteddate = new date(year,monthofyear, dayofmonth); string strdate = null; simpledateformat dateformatter = new simpledateformat("yyyy-mm-dd"); strdate = dateformatter.format(selecteddate); txtdeliverydate.settext(strdate); }
i suspect didn't read documentation (deprecated) date
constructor you're using:
parameters:
year - year minus 1900.
month - month between 0-11.
date - day of month between 1-31.
avoid using date
here. either use good date/time library joda time, or use calendar
set year/month/day values - then, month 0-based.
also, method accepting year/month/day values... if you're trying conversion, should accepting string , returning string, e.g.
public static string convertdateformat(string text) { timezone utc = timezone.gettimezone("etc/utc"); simpledateformat parser = new simpledateformat("dd-mm-yyyy", locale.us); parser.settimezone(utc); date date = parser.parse(text); simpledateformat formatter = new simpledateformat("yyyy-mm-dd", locale.us); formatter.settimezone(utc); return formatter.format(date); }
Comments
Post a Comment