如何从 MaterialDatePicker 获取日期?

How can I get the date from MaterialDatePicker?

我的应用程序使用 MaterialDatePicker。我想创建一个 class,除其他数据外,它将存储来自 MaterialDatePicker 的日期。 我有一些问题:

  1. 如果 class 中的日期应该可以更改,保存日期的最佳方法是什么?
  2. 为此,如何从日期选择器 material 中获取日期? Here's my activity where I'm using the date picker material. By clicking on the checkbox, I want to save all this information, including the date, into a class object.

这是我使用 MaterialDatePicker

activity 的代码
public class NewEvent extends AppCompatActivity {
TextInputEditText etDate, etEventId;
AutoCompleteTextView etType, etNotif;
MaterialToolbar toolbarNE;
SharedPreferences sharedPreferences;

private static final String TAG = "myLogs";
static final String newEv = "newEv";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_event);
    final FragmentManager fragmentManager = getSupportFragmentManager();
    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    final FirstFragment fragment1 = new FirstFragment();

    etDate = findViewById(R.id.etDate);
    etType = findViewById(R.id.etType);
    etNotif = findViewById(R.id.etNotif);
    etEventId = findViewById(R.id.etEventId);
    toolbarNE = findViewById(R.id.menuNewEvent);

    toolbarNE.setOnMenuItemClickListener(new MaterialToolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch(item.getItemId())
            {
                case R.id.applyEvent:
                    /*Bundle bundle = new Bundle();
                    bundle.putString("name", (String) etEventId.getText().toString());
                    fragment1.setArguments(bundle);
                    fragmentTransaction.replace(R.id.q3, fragment1);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();*/

                    Intent intent = new Intent(NewEvent.this, FirstFragment.class);
                    intent.putExtra("name", (String) etEventId.getText().toString());
                    setResult(RESULT_OK, intent);


                    finish();
                    //Toast.makeText(getApplicationContext(), materialDatePicker.getHeaderText(), Toast.LENGTH_SHORT).show();

                    return true;
            }
            return false;
        }
    });



    // MaterialDatePicker stuff

    MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.datePicker();
    builder.setTitleText("Выберите дату");
    final MaterialDatePicker materialDatePicker = builder.build();


    etDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            materialDatePicker.show(getSupportFragmentManager(), "DATE_PICKER");
        }
    });

    materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
        @Override
        public void onPositiveButtonClick(Object selection) {
            etDate.setText(materialDatePicker.getHeaderText());

        }
    });



    // Тип события
    List listEvent = new ArrayList();
    Collections.addAll(listEvent, "Material", "Shit");
    ArrayAdapter adapterEvent = new ArrayAdapter(this,R.layout.list_item,listEvent);
    etType.setAdapter(adapterEvent);

    // Тип уведомления
    List listNotif = new ArrayList();
    //items.add("Material");
    //items.add("Shit");
    Collections.addAll(listNotif, "Каждый день", "Раз в 2 дня", "Раз в 3 дня", "Раз в 5 дней", "Раз в неделю", "Не напоминать");
    //String [] ITEMS = new String[] {"S1", "S2", "S3"};
    //ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.list_item, ITEMS);
    ArrayAdapter adapterNotif = new ArrayAdapter(this,R.layout.list_item, listNotif);
    etNotif.setAdapter(adapterNotif);



}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

这是我未来的代码class,但是里面什么都没有,因为我在考虑日期。

public class Event {
public String Name;
public String Date;   // I don't know what type of variable to specify here, so far I have specified a string.
public String Comment;
public String Type;
public String Notify;



Event()
{
    Name = "Событие";
    Date = "Date";
    Comment = "Comment";
    Type = "Type";
    Notify = "Notify";
}

}

获取日期使用:

MaterialDatePicker<Long> picker = builder.build();

picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
      @Override public void onPositiveButtonClick(Long selection) {
        // Do something...
      }
    });

这样您就可以选择以毫秒为单位的日期。
如果你想格式化为字符串,你可以使用类似的东西:

Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
utc.setTimeInMillis(selection);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String formatted = format.format(utc.getTime());