简体   繁体   中英

Get the date from the edittext

How can i get the value of date which i set in edittext

 mDateSelect.setText(getString(R.string.strSelectedDate,
            new StringBuilder()

            .append(mDay).append("/")
            .append(mMonth + 1).append("/")
            .append(mYear).append(" "))
            );  

This is my code for setting the date in the edittext from the datepicker(mDateSelect is the edittext).I am setting my date from the datepicker into the edittext.Now i have to get the date which has been set in dd/mm/yyyy format.Below is my code.

String dob_var=(mDateSelect.getText().toString());
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    try {
        dateObject = formatter.parse(dob_var);
        Log.e("date is ", dateObject+"");
    } catch (ParseException e) {

        e.printStackTrace();
    }

this is giving me the time as Wed Aug 03 00:00:00 EDT 2011

如果您正确地将日期值设置为EditText,现在又要获取您设置的EditText值,则意味着使用此代码即可解决。

String datevalue = mDateSelect.getText().toString();

If you want to get date object for your editText then you can do something like this :

@SuppressLint("SimpleDateFormat")
    public static String formatDate(String inputDate) {
        String formattedDate;
        try {
            DateFormat originalFormat = new SimpleDateFormat("dd/mm/yyyy"); //Input date format

            DateFormat targetFormat = new SimpleDateFormat("MM/dd/yyyy"); //Output date format
            Date date = originalFormat.parse(inputDate);
            formattedDate = targetFormat.format(date);
            return formattedDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "no_date";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM