我是Java和Android的新手。我从DatePickerDialog上的不同教程中获取了以下代码(主要是官方的)。 现在我希望所选日期采用特定格式(" EE,DD.MM.YY")并尝试使用SimpleDateFormat执行此操作,但我不知道从哪里获取所选日期因为我只有"以年,月,日为参数:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public Date date;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
date = c.getTime();
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
TextView textView = (TextView) getActivity().findViewById(R.id.textView_Date);
String selectedDate = String.valueOf(day) + "." + String.valueOf(month) + "." + String.valueOf(year);
textView.setText(selectedDate);
}
}
所以我试着改变这条线
String selecteDate = ...
类似
SimpleDateFormat simpledateformat = new SimpleDateFormat("EE, DD.MM.YY");
String selectedDate = simpledateformat.format(date);
但是因为" date"不是选定的,而是当前的,因为simpledateformat.format的结果可能不是String,它不起作用。谁能告诉我如何使它工作?它不需要是SimpleDateFormat,这只是我认为可行的方式。非常感谢!
答案 0 :(得分:1)
试试这个:
SimpleDateFormat simpledateformat = new SimpleDateFormat("EE, dd.MM.yy");
Calendar newDate = Calendar.getInstance();
newDate.set(year, month, day);
String selectedDate = simpledateformat.format(newDate.getTime());