更改 Android 中 DatePickerDialog 日历的日期格式

2024-02-22

我在代码中使用 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);    
        }
    }

我想像这样显示日期选择器对话框 https://i.stack.imgur.com/1EU89.png


不确定你想实现什么,但你可以使用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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更改 Android 中 DatePickerDialog 日历的日期格式 的相关文章

随机推荐