我有了这个editText,当你点击它时,它会打开一个datepickerdialog,所以我选择一个日期,我想在edittext中显示它,但我想格式化日期区域设置。对于美国人和其他dd / mm / yyyy,我希望它是mm / dd / yyyy。
我不想使用simpledateformat,我想使用dateformat。
editDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//To show current date in the datepicker
Calendar mcurrentDate=Calendar.getInstance();
mYear=mcurrentDate.get(Calendar.YEAR);
mMonth=mcurrentDate.get(Calendar.MONTH);
mDay=mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
final Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, selectedyear);
c.set(Calendar.MONTH, selectedmonth);
c.set(Calendar.DAY_OF_MONTH, selectedday);
// Date date = c.getTime();
// editDate.setText(date);
}
},mYear, mMonth, mDay);
mDatePicker.setTitle("Select date");
mDatePicker.show(); }
});
答案 0 :(得分:1)
我在ondateset方法中这样做了:
java.text.DateFormat f = java.text.DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
editDate.setText(f.format(c.getTime()));
答案 1 :(得分:0)
您需要一种机制来获取用户的区域设置,然后您可以格式化日期Java样式
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
System.out.println(df.format(new Date()));
// Nov 18, 2014
df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.FRANCE);
System.out.println(df.format(new Date()));
// 18 nov. 2014