错误的datPicker格式于editText中。

huangapple go评论72阅读模式
英文:

Wrong datPicker format in editText

问题

我有一个日期选择器(datePicker),我想在从中获取日期时在编辑框(editText)中显示它,但格式不对。
例如,我选择了 1 月 01 日 2020 年。
但在编辑框内显示的是 2020/0/1 而不是 2020/01/01。

打开日期选择器:

 new SpinnerDatePickerDialogBuilder()
                        .context(AddProjectActivity.this)
                        .callback(AddProjectActivity.this)
                        .spinnerTheme(R.style.NumberPickerStyle)
                        .showTitle(true)
                        .showDaySpinner(true)
                        .defaultDate(2017, 0, 1)
                        .maxDate(2100, 0, 1)
                        .minDate(2020, 0, 1)
                        .build()
                        .show();
 @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        meEdtDate.setText(year + "/" + (monthOfYear + 1) + "/" + dayOfMonth);
        date = getDate(year, monthOfYear, dayOfMonth);
    }

    private Date getDate(int year, int monthOfYear, int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, monthOfYear, dayOfMonth);

        return calendar.getTime();
    }

将日期设置到编辑框中:

 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            String dateStr = simpleDateFormat.format(projectEntity.getDate());
            meEdtDate.setText(dateStr);
英文:

I have a datePicker that I want to display in the editText when I take the date from it, it has the wrong format.
For example, I
Jan / 01/2020
I choose.
But inside text editing
It shows 2020/0/1 instead of 2020/01/01

open datePicker:

new SpinnerDatePickerDialogBuilder()
                       .context(AddProjectActivity.this)
                       .callback(AddProjectActivity.this)
                       .spinnerTheme(R.style.NumberPickerStyle)
                       .showTitle(true)
                       .showDaySpinner(true)
                       .defaultDate(2017, 0, 1)
                       .maxDate(2100, 0, 1)
                       .minDate(2020, 0, 1)
                       .build()
                       .show();
 @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        meEdtDate.setText(year + "/" + monthOfYear + "/" + dayOfMonth);
        date = getDate(year, monthOfYear, dayOfMonth);
    }

    private Date getDate(int year, int monthOfYear, int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, monthOfYear, dayOfMonth);

        return calendar.getTime();
    }

set date to editText:

 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            String dateStr = simpleDateFormat.format(projectEntity.getDate());
            meEdtDate.setText(dateStr);

答案1

得分: 0

这是使用当前包java.time中的类来处理日期的解决方案。

您可以像这样根据年份、月份和日期创建一个LocalDate:

private static LocalDate getDate(int year, int monthOfYear, int dayOfMonth) {
    return LocalDate.of(year, monthOfYear, dayOfMonth);
}

然后可以将其格式化为字符串,就像这样:

LocalDate date = getDate(2020, 1, 1);
String dateString = date.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
英文:

Here is a solution using classes from the current package, java.time, for date handling.

You can create a LocalDate from year, month and day like this

private static LocalDate getDate(int year, int monthOfYear, int dayOfMonth) {
    return LocalDate.of(year, monthOfYear, dayOfMonth);
}

And then format it into a String like this

LocalDate date = getDate(2020, 1, 1);
String dateString = date.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));

huangapple
  • 本文由 发表于 2020年10月10日 17:28:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64291944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定