我在我的代码中使用DatePickerDialog。我想知道,如何以dd mm yyyy格式更改DatePickerDialog日历的日期格式。
我的代码是: -
public static class DatePickerDialogTheme5 extends DialogFragment implements DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
SimpleDateFormat sdf=new SimpleDateFormat("dd MM yyyy");
Date d=new Date();
sdf.format(d);
final Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
calendar.set(calendar.YEAR,d.getYear());
calendar.set(calendar.MONTH,d.getMonth());
calendar.set(calendar.DAY_OF_MONTH,d.getDay());
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
//AlertDialog.THEME_TRADITIONAL,this,day,month,year);
AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,year,month,day);
return datepickerdialog;
}
public void onDateSet(DatePicker view, int year, int month, int day){
TextView textview = (TextView)getActivity().findViewById(R.id.textView1);
textview.setText(day + ":" + (month+1) + ":" + year);
}
}
答案 0 :(得分:2)
不确定您希望实现的目标,但您可以使用Calendar
实例与SimpleDateFormat
根据您的需要设置Date
格式。
例如:
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd:MM:yyyy");
String dateString = dateFormat.format(calendar.getTime());
TextView textview = (TextView)getActivity().findViewById(R.id.textView1);
textview.setText(dateString);
}
答案 1 :(得分:0)
Here i disabled previous dates also
inside onclick listener
edit_event_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerFragment dFragment = new DatePickerFragment();
dFragment.show(DashBoardActivity.this.getFragmentManager(), "Date Picker");
}
});
@SuppressLint("ValidFragment")
public class DatePickerFragment extends android.app.DialogFragment implements DatePickerDialog.OnDateSetListener {
int year, month, day;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, year, month, day);
dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
return dpd;
}
public void onDateSet(DatePicker view, int syear, int selectedmonth, int selectedday) {
view.setMinDate(System.currentTimeMillis() - 1000);//disable dates
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0); ////disable dates
cal.set(syear, selectedmonth, selectedday, 0, 0, 0);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String dateString = dateFormat.format(cal.getTime());
//in loolipop disable dates also we can select so for that restriction below condition
if (syear == year && (selectedmonth + 1) == month + 1) {
if (selectedday < day) {
Toast.makeText(getActivity(), "invalid date", Toast.LENGTH_LONG).show();
return;
}
}
edit_event_date.setText(dateString);
}
}
答案 2 :(得分:0)
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
edt_date_of_birth.setText(null);
edt_date_of_birth.setInputType(InputType.TYPE_NULL);
int day = dayOfMonth;
int yy = year;
int month = ++monthOfYear;
String str_day = day < 10 ? "0" + day : "" + day;
String str_month = month < 10 ? "0" + month : "" + month;
String date_select = String.valueOf(yy) + "-" + str_month + "-" + str_day;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
try {
date1 = format.parse(date_select);
} catch (ParseException e) {
e.printStackTrace();
}
String date = format.format(date1);
if(date.endsWith("01") && !date.endsWith("11"))
format = new SimpleDateFormat("d'st' MMM, yyyy");
else if(date.endsWith("02") && !date.endsWith("12"))
format = new SimpleDateFormat("d'nd' MMM, yyyy");
else if(date.endsWith("03") && !date.endsWith("13"))
format = new SimpleDateFormat("d'rd' MMM, yyyy");
else
format = new SimpleDateFormat("d'th' MMM, yyyy");
String yourDate = format.format(date1);
edt_date_of_birth.setText(yourDate);
}