设置java.time日期选择器

xdnvmnnf  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(463)

我正在将我的应用程序从使用内置的公历切换到java.time,因为我被告知这是一个更好的方法。有没有一种方法可以像我在下面的代码中使用java.time那样执行日期选择器?如果没有,是否有类似的情况?

private void buildDatePicker(){
        final String myFormat = "MMM dd, yyyy"; //sets format in which to show date
        final SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.getDefault());

        Date c = calendar.getTime(); //gets the current date
        String formattedDate = sdf.format(c); //runs date through formatter
        etDate.setText(formattedDate); // sets the etDate edittext to the current date

        final DatePickerDialog.OnDateSetListener datePicker = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {

                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, monthOfYear);
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                etDate.setText(sdf.format(calendar.getTime()));
            }

        };

        etDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(getContext(), datePicker, calendar
                        .get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
    });
xriantvc

xriantvc1#

你的代码既不完整也不一致。而且我不做android,所以我不熟悉具体的框架类。所以我不能提供一个完整的例子。
但是我可以展示一些用java.time翻译的粗略代码。

private void buildDatePicker ( )
{

    // Automatically localize the format of the date string.
    // Perhaps not make `formatter` object `final` as the user's locale might change during runtime.
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( Locale.getDefault() );
    etDate.setText( myLocalDate.format( formatter ) ); // sets the etDate edittext to the current date

    final DatePickerDialog.OnDateSetListener datePicker = new DatePickerDialog.OnDateSetListener()
    {

        @Override
        public void onDateSet ( DatePicker view , int year , int monthOfYear , int dayOfMonth )
        {
            LocalDate localDate = LocalDate.of( year , monthOfYear , dayOfMonth );  // Uses sane numbering for year and month, unlike the `Calendar`/`Date`.
            etDate.setText( myLocalDate.format( formatter ) );
        }
    };

    etDate.setOnClickListener( new View.OnClickListener()

    {
        @Override
        public void onClick ( View v )
        {
            // TODO Auto-generated method stub
            new DatePickerDialog( getContext() , datePicker , myLocalDate.getYear() , myLocalDate.getMonthValue() , myLocalDate.getDayOfMonth() )
                    .show();
        }
    } );
}

使用java.time比使用糟糕的遗留类要简单得多 Calendar , Date , SimpleDateFormat ,等等。
这已经在堆栈溢出上讨论过很多次了。搜索以了解更多信息。

相关问题