Rob
Rob

Reputation: 53

Android : How to show default date in edittext?

I want to show default date in edittext before the user select date of own choice using datepicker.My datepicker is working fine.In "From" edittext i want to show the previous month date by default and the user will feel free to change the default date using datepicker.In "To" edittext i want to show the current day date.

Example : In "From" it shows 2016-01-22 which is previous month date. And in "TO" it shows today's date 2016-02-22.

enter image description here

Here is the code of datefrom and 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()));
}

Upvotes: 0

Views: 2292

Answers (2)

Rob
Rob

Reputation: 53

To get previous month date :

 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;
  }

To get current month date [refer to @Vivek Kumar answer]:

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

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

    return formattedCurrentDate;
  }

Upvotes: 0

Vivek Bhardwaj
Vivek Bhardwaj

Reputation: 528

You can use this function to get the current date:

 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;
    }

this will return you the current date. And then your can set this date in the EditText.

for eg:

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

Upvotes: 1

Related Questions