M.Usman
M.Usman

Reputation: 2119

Set calendar class instance with the values provided in edit-text

I am creating one application which requires calendar instance containing date and time entered by user.

I've done half of the task by getting values from date and time picker and set it to edittext but now how to create calendar instance having same date and time provided in edittext.

Edittext for Date having value as : `01/03/2018`
Edittext for Time having value as : `12:50`

Any help will be appericated.

Upvotes: 1

Views: 251

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521409

In this answer I assume that you have date and time data in the exact format of your question. If so, then one option is to build a timestamp string and then use a SimpleDateFormat to convert to a Date. Finally, set the time of a Calendar instance using that date.

String editText1 = "01/03/2018";
String editText2 = "12:50";
String ts = editText1 + " " + editText2;

String pattern = "MM/dd/yyyy HH:mm";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = sdf.parse(ts);
Calendar cal = Calendar.getInstance();
cal.setTime(date);

Demo

Upvotes: 1

Related Questions