Java - 我想获取日历的日期值 java

Java - I want to get the date value of the calendar java

用edittext输入标题,按日历按钮,分别在textview中显示开始日期和结束日期。到这里就完美了。

但是,我尝试使用它在数据库上存储数据,但我认为我无法存储日期。我想我们可以将 StartLabel 中显示的数据放入变量中。这有错吗?

我想在startdate中保存开始日期2022-01-28。 同样,我想将结束日期保存在 enddate 中。 但它没有保存。帮帮我....

public class EventActivity extends AppCompatActivity {
    private EditText event_title;
    private TextView start_date_text;
    private ImageView start_date_btn;

    private TextView end_date_text;
    private ImageView end_date_btn;
    private Button event_save_btn;

    private String startdate, enddate;

    Calendar sCalendar = Calendar.getInstance();
    Calendar eCalendar = Calendar.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);

        start_date_text = (TextView) findViewById(R.id.start_date_text);
        end_date_text = (TextView) findViewById(R.id.end_date_text);

        event_title = (EditText) findViewById(R.id.event_title);
        start_date_btn = (ImageView) findViewById(R.id.start_date_btn);
        end_date_btn = (ImageView) findViewById(R.id.end_date_btn);
        event_save_btn = (Button) findViewById(R.id.event_save_btn);

        mTextViewResult = (TextView)findViewById(R.id.result_text);
        mTextViewResult.setMovementMethod(new ScrollingMovementMethod());

        //show start_date
        DatePickerDialog.OnDateSetListener myDatePicker1 = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                sCalendar.set(Calendar.YEAR, year);
                sCalendar.set(Calendar.MONTH, month);
                sCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                startLabel();
            }
        };

        //show end_date 
        DatePickerDialog.OnDateSetListener myDatePicker2 = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                eCalendar.set(Calendar.YEAR, year);
                eCalendar.set(Calendar.MONTH, month);
                eCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                endLabel();
            }
        };

        //click start calendar btn
        start_date_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(EventActivity.this,
                        myDatePicker1,
                        sCalendar.get(Calendar.YEAR),
                        sCalendar.get(Calendar.MONTH),
                        sCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        //click end calendar btn
        end_date_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(EventActivity.this,
                        myDatePicker2,
                        eCalendar.get(Calendar.YEAR),
                        eCalendar.get(Calendar.MONTH),
                        eCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

   ..........

    }


    private void startLabel() {
        String myFormat = "yyyy-MM-dd";    //2018/11/28
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.English);

        TextView start_date_text = (TextView) findViewById(R.id.start_date_text);
        start_date_text.setText(sdf.format(sCalendar.getTime()));

        startdate = start_date_text.toString(); // for save startdate
    }

    private void endLabel() {
        String myFormat = "yyyy-MM-dd";// 2018/11/28
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.English);

        TextView end_date_text = (TextView) findViewById(R.id.end_date_text);
        end_date_text.setText(sdf.format(eCalendar.getTime()));

        enddate = end_date_text.toString(); // for save enddate

    }

................
     

而不是这个:

private void startLabel() {
    String myFormat = "yyyy-MM-dd";    //2018/11/28
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.English);

    TextView start_date_text = (TextView) findViewById(R.id.start_date_text);
    start_date_text.setText(sdf.format(sCalendar.getTime()));

    startdate = start_date_text.toString(); // for save startdate
}

尝试这样做:

private void startLabel() {
    String myFormat = "yyyy-MM-dd";    //2018/11/28
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.English);

    startdate = sdf.format(sCalendar.getTime()); // for save startdate

    TextView start_date_text = (TextView) findViewById(R.id.start_date_text);
    start_date_text.setText(startdate); 
}

您可以对 endLabel().

执行相同的操作

尝试更改它:

    startdate = start_date_text.getText.toString(); // for save startdate ```

结束日期也一样