Datepicker date return problems

Asked

Viewed 206 times

0

I’m using a datepicker for an application that will be used in the course, but when I convert it from int to Date, it returns the date 29/03/2018 as follows:

Wed Sep 08 00:00:00 GMT 34

Code that is called the datepicker

@SuppressLint("ValidFragment")
    public class DatePickerFragment extends DialogFragment
            implements DatePickerDialog.OnDateSetListener{


        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Sao_Paulo"));

            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);


            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            month++;
            etDataNascimentoCondutor.setText(day+ "/" + month + "/" + year);
        }
    }

Converting to date

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String sDataNascimento = etDataNascimentoCondutor.getText().toString();
try {
    dataNascimentoCondutor = df.parse(sDataNascimento);
    Log.v("Data Capturada", dataNascimentoCondutor.toString());
} catch (ParseException e) {
    e.getMessage();
    throw new RuntimeException();
}

1 answer

0

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");//variavel na classe    
Calendar calendar; //variavel na classe

/////////////////////

@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
    calendar = Calendar.getInstance(); //faça variavel calendar na classe
    calendar.set(year,month,day);
    Date date = calendar.getTime();//metodo getTime converte o Calendar para Date
    etDataNascimentoCondutor.setText(df.format(date));
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.