I am getting month
and year
in string format from date picker, how can I convert it to complete date using any date formatter?
Asked
Active
Viewed 660 times
-1

ΦXocę 웃 Пepeúpa ツ
- 47,427
- 17
- 69
- 97

Sumit T
- 1,273
- 2
- 17
- 32
-
we need to see more code, it depends of how they look like... – ΦXocę 웃 Пepeúpa ツ Feb 19 '16 at 18:59
-
What you mean by complete date ? In that case what should be the day (dd) ? – Midhun MP Feb 19 '16 at 19:05
-
@MidhunMP : Complete date means something like "19th Feb 2016". – Sumit T Feb 19 '16 at 19:10
2 Answers
1
You can parse a string
into a date
, therefore you need the format that associates the String and a date:
Use a dateformat to convert the string into a date:
Example:
String startDateString = "Feb 2007"; //this is the var comming from the datepicker
DateFormat df = new SimpleDateFormat("MMM yyyy");
Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println(newDateString);
} catch (ParseException e) {
e.printStackTrace();
}

Community
- 1
- 1

ΦXocę 웃 Пepeúpa ツ
- 47,427
- 17
- 69
- 97
-
-
then you can by default asume that it is always the 1st date of that, and parse it to a date – ΦXocę 웃 Пepeúpa ツ Feb 19 '16 at 19:05
-
Yea, that can work, I can do that when I am picking up date from datepicker but that's not an ideal solution. What if I want to convert it to default date format, I might give you error on parsing for different date formats. What do you think? – Sumit T Feb 19 '16 at 19:18
-
thanks for suggestion. I am doing it same but after parsing to ' startDate = df.parse(startDateString); ' I am getting milliseconds from it 'strarDate.getTime()' and then converting to new Date(). – Sumit T Feb 20 '16 at 00:43
1
private DatePickerDialog.OnDateSetListener pDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
try{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String dt = String.valueOf(dayOfMonth) + String.valueOf(monthOfYear) + String.valueOf(year);
Date d = sdf.parse(dt);
log.d(d);
}catch(ParseException ex){}
}
};

Hardik Pithva
- 1,729
- 1
- 15
- 31
-
I am hiding day from date on date set because I don't want to show it. – Sumit T Feb 19 '16 at 19:16