Android:如何在edittext中显示默认日期?

时间:2016-02-22 10:24:31

标签: android datepicker

我希望在用户使用datepicker选择自己选择的日期之前在edittext中显示默认日期。我的datepicker工作正常。在" From" edittext我想默认显示上个月的日期,用户可以使用datepicker更改默认日期。" To" edittext我想显示当前的日期。

示例:In" From"它显示2016-01-22这是上个月的日期。在" TO"它显示今天的日期2016-02-22。

enter image description here

以下是datefrom和dateupto的代码:

    public void datefrom() {

    showdatefrom = new DatePickerDialog.OnDateSetListener() {

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

            myCalendarfrom.set(Calendar.YEAR, year);
            myCalendarfrom.set(Calendar.MONTH, monthOfYear);
            myCalendarfrom.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateLabelFrom();
        }

    };

    myCalendarfrom = Calendar.getInstance();

    dateFrom.setInputType(InputType.TYPE_NULL);                    
    dateFrom.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            new DatePickerDialog(SelectDate.this,
                    showdatefrom,
                    myCalendarfrom.get(Calendar.YEAR),
                    myCalendarfrom.get(Calendar.MONTH),
                    myCalendarfrom.get(Calendar.DAY_OF_MONTH)).show();
        }
    });
}

public void dateupto() {

    showdateupto = new DatePickerDialog.OnDateSetListener() {

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

            myCalendarupto.set(Calendar.YEAR, year);
            myCalendarupto.set(Calendar.MONTH, monthOfYear);
            myCalendarupto.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateLabelTo();
        }

    };

    myCalendarupto = Calendar.getInstance();

    dateTo.setInputType(InputType.TYPE_NULL);                     
    dateTo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            new DatePickerDialog(SelectDate.this, showdateupto,
                    myCalendarupto.get(Calendar.YEAR) ,
                    myCalendarupto.get(Calendar.MONTH),
                    myCalendarupto.get(Calendar.DAY_OF_MONTH)).show();
        }
    });
}
private void updateLabelFrom() {

    String myFormat = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    dateFrom.setText(sdf.format(myCalendarfrom.getTime()));
}

private void updateLabelTo() {

    String myFormat = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    dateTo.setText(sdf.format(new Date()));
}

2 个答案:

答案 0 :(得分:1)

您可以使用此功能获取当前日期:

 public String getCurrentDate()
    {
         Calendar c = Calendar.getInstance();
            System.out.println("Current time => " + c.getTime());

            SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
            String formattedDate = df.format(c.getTime());

            return formattedDate;
    }

这将返回当前日期。然后你可以在EditText中设置这个日期。

例如:

EditText etCurrentDate = (EditText) findViewById(R.id.etCurrentDate);
etCurrentDate.setText( getCurrentDate());

答案 1 :(得分:0)

获取上个月的日期:

 public String getPreviousDate()
  {
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MONTH, -1);

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    String formattedPreviousDate = df.format(c.getTime());
    return formattedPreviousDate;
  }

获取当前月份日期[请参阅@Vivek Kumar答案]:

 public String getCurrentDate()
  {
    Calendar c = Calendar.getInstance();

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    String formattedCurrentDate = df.format(c.getTime());

    return formattedCurrentDate;
  }