在Android中更改DatePickerDialog日历的日期格式 [英] Change date format of the DatePickerDialog Calendar in Android

查看:142
本文介绍了在Android中更改DatePickerDialog日历的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中使用DatePickerDialog.我想知道,如何更改dd mm yyyy格式的DatePickerDialog日历的日期格式.

I'm using DatePickerDialog in my code. And I want to know, How to change date format of the DatePickerDialog Calendar in format dd mm yyyy.

我的代码是:-

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);    
        }
    }

我要显示日期选择器对话框

推荐答案

不确定要实现的目标,但是可以将 Calendar 实例与 SimpleDateFormat 一起使用根据需要设置日期的格式.

Not sure what you would like to achieve, but you could use a Calendar instance with SimpleDateFormat to format your Date according to your needs.

例如:

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);
}

这篇关于在Android中更改DatePickerDialog日历的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆