In my Android Studio project I have a DatePickerDialog. On the onDateSet, I have 3 arguments :
I would like to make a setText using the local format of the device.
For exemple, in USA the date is like : mm/dd/yyyy , but in France it's like dd-mm-yyyy
How can I do that ?
This is my code :
@Override
public void onClick(View view) {
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePicker;
datePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker datePicker, int selectYear, int selectedMonth, int selectedDay) {
textViewDate.setText("HERE I HAVE TO PUT THE DATE WITH THE LOCAL FORMAT);
}
}, year, month, day);
datePicker.show();
}
});
Thanks
You can use this methods:
public String formatDateStyles(Date date, Locale currentLocale) {
String result = "";
DateFormat formatter;
int[] styles = { DateFormat.DEFAULT, DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL };
for (int k = 0; k < styles.length; k++) {
formatter = DateFormat.getDateInstance(styles[k], currentLocale);
result = formatter.format(date);
}
return result;
}
public Date getDate(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
by this way:
textViewDate.setText(formatDateStyles(getDate(selectYear, selectedMonth, selectedDay), new Locale("en", "US")));
More details here .
Try this:
public void onDateSet(DatePicker datePicker, int selectYear, int selectedMonth, int selectedDay) {
DataFormat dateFormat= android.text.format.DateFormat.getDateFormat(getApplicationContext());
cal.set(selectYear, selectedMonth, selectedDay)
textViewDate.setText(dateFormat.format(cal.getTime()));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.