如何更改YYY.MM-DD中物料日期选择器的格式



我想更改物料日期选择器的格式。我从字符串中得到它,就像2021年4月13日。但我想更改2021-04-13的格式。

Calendar calendar=Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.clear();
Long today= MaterialDatePicker.todayInUtcMilliseconds();
//materialDatePicker
MaterialDatePicker.Builder start_builder=MaterialDatePicker.Builder.datePicker();
start_builder.setTitleText("SELECT A  START DATE");
start_builder.setSelection(today);

start_DatePicker=start_builder.build();
start_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start_DatePicker.show(getSupportFragmentManager(),"Date_Picker");
}
});

start_DatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {

@Override
public void onPositiveButtonClick(Object selection) {
start_date.setText(start_DatePicker.getHeaderText());
String get_start_date=start_date.getText().toString();
DateFormat formatter = new SimpleDateFormat("yyyy MM dd");
String today = formatter.format(get_start_date);
String get_end_date=end_date.getText().toString();
Toast.makeText(this, "Start Date"+today + "End date" +get_end_date, Toast.LENGTH_SHORT).show();

}
});

}

我在下面有一个错误。无法将给定对象格式化为日期

addOnPositiveButtonClickListener监听器返回所选日期作为Long
不要使用HeaderText。

start_DatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
@Override public void onPositiveButtonClick(Long selection) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.setTimeInMillis(selection);
SimpleDateFormat format = new SimpleDateFormat("yyyy MM dd");
String formattedDate  = format.format(calendar.getTime());
}
});

使用以下代码

@Override
public void onPositiveButtonClick(Object selection) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy");
Date varDate=dateFormat.parse(start_DatePicker.getHeaderText());
dateFormat=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Date :"+dateFormat.format(varDate));}

相关内容

  • 没有找到相关文章

最新更新