Reputation: 1395
I'm hoping you guys can help me on this. When i use DatePicker, i display my date in "EEE, MMM d, yyyy" format.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String strDate = new SimpleDateFormat("EEE, MMM d, yyyy").format(cal.getTime());
editTextStartDate.setText(strDate);
But after setting the date i will process the data, And I need the data to be formatted into "dd/mm/yyyy" so that i can use it by splitting it:
String startDate = editTextStartDate.getText().toString().trim();
if (!startDate.isEmpty()) {
String getTo[] = startDate.split("/");
getToDateYear = Integer.parseInt(getTo[2]);
getToDateMonth = Integer.parseInt(getTo[1]);
getToDateDay = Integer.parseInt(getTo[0]);
toTime.set(Calendar.YEAR, getToDateYear);
toTime.set(Calendar.MONTH, getToDateMonth-1);
toTime.set(Calendar.DAY_OF_MONTH, getToDateDay);
}
What is the best way for me to do this? Hopefully you guys can help teach and guide me. Thank you guys!
Upvotes: 0
Views: 671
Reputation: 717
There will not be any / in String strDate = new SimpleDateFormat("EEE, MMM d, yyyy").format(cal.getTime());
So, Your String getTo[] will not have any Array Output.
So Solution is below to convert your Date format to another format as below:
SimpleDateFormat sourceDateFormat = new SimpleDateFormat("EEE, MMM d, yyyy");
Date sourceDate = null;
try {
sourceDate = sourceDateFormat.parse(startDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat targetFormat = new SimpleDateFormat("dd/MM/yyyy");
String targetdatevalue= targetFormat.format(sourceDate);
So targetdatevalue will be your desired output.
Upvotes: 1