I just implemented a DatePicker for my app. But I’m having a hard time understanding how to convert it(‘theDate’) to a SimpleDateFormat(“yyyy-MM-dd HH:mm:ss Z”) just like the (‘submitDate’) in my code below.
Could someone kindly show me how to do it in my code please? Thank you!
protected Dialog onCreateDialog(int id) {
switch (id) {
case SDATE_DIALOG_ID:
return new DatePickerDialog(this,
sDateSetListener, mYear, mMonth, mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener sDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view,
int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDate();
}
};
private void updateDate() {
inputDate.setText(
new StringBuilder()
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
class CreateNewRequest extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
Calendar c = Calendar.getInstance();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
String submitDate = sd.format(c.getTime());
String theDate = inputDate.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("request_date", theDate));
params.add(new BasicNameValuePair("submitDate", submitDate));
}
Step 1) You need to parse
theDateto Date object usingSimpleDateFormatterYou may need separate
SimpleDateFormatterfor above step, the one you have already works for below step only.Your thisDate is formatted with hyphen as separator, so your SimpleDateFormatter for step1 should use hyphen:
Example:
new SimpleDateFormat("MM-dd-yyyy");Step2) Pass Date object constructed in above step as input instead of c.getTime(), then you will get formatted date as you want.
EDIT: