Date formatter not working

Asked

Viewed 26 times

0

I have a code that supposedly goes to format dd-mm-yyyy or for example 02-10-2017 but my problem is that this code instead of passing to 02-10-2017 is passing to 2-10-2017.

Code :

dt = 2 + "-" + 10 + "-" + 2017;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
try {
    sdf.parse(dt);
    date_text.setText(dt);
    Log.d(tag,""+dt);
} catch (ParseException e) {
    e.printStackTrace();
}

In the log I have this result :

07-13 20:42:25.914 12020-12020/com.pedrogouveia.averagemaker D/tag: 2-10-2017

Updated code :

        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            month = month + 1;
            date_button.setVisibility(View.GONE);
            date_text = (TextView) rootView.findViewById(R.id.date_text);
            date_text.setVisibility(View.VISIBLE);
            dt = day + "-" + month + "-" + year;
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            dataInterpetrada = sdf.format(dt);     <---------Esta linha  esta vermelha e esta dizendo incompatible types java.util.dates and java.lang.string 
            dt = sdf.format(dataInterpetrada);
            date_text.setText(dt);

        }
    };

Further down add dt to my database as string

  • Strange, you wrote it dt = 2 + "-" + 10 + "-" + 2017;, but the log says D/tag: 14-7-2017, did you make any confusion?

  • It was not just an example the 2-10-2017 mine has a dataPicker

1 answer

1


The parse of SimpleDateFormat returns the date that was interpreted.

So it’s supposed to be something like:

Date dataInterpretada = sdf.parse(dt);

And if you want to write it on Log with the previous format it is necessary to use the format of the same object of the format:

Log.d(tag,""+ sdf.format(dataInterpretada ));

Small java test to confirm.

Completing the code shown:

dt = 2 + "-" + 10 + "-" + 2017;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
try {
    Date dataInterpretada = sdf.parse(dt); //agora guarda a data interpretada
    date_text.setText(sdf.format(dataInterpretada )); //agora com format
    Log.d(tag,""+sdf.format(dataInterpretada )); //e aqui com format também
} catch (ParseException e) {
    e.printStackTrace();
}

Edit:

The SimpleDateFormat returns a date of type java.util.date and not java.sql.date. To solve this problem or exchange the import or you can change only when creating the date by:

java.util.Date dataInterpretada = sdf.parse(dt);
  • I didn’t really want to turn this date into a string, just a string in the 02-10-2017 format, as I do ?

  • 1

    @Pedrogouveia seems to me that this is exactly what the code of Isac does, turns into string. Got it? parse converts to an object, the correct is to use . format.

  • When I use . format instead of . parse, I have this error : java.lang.Illegalargumentexception: Bad class: class java.lang.String

  • What code line is the format

  • How Voce is saying no because it says you need to be a string to receive.

  • sdf.parse(dt)

  • The format returns the date in format String, so it can be used anywhere you expect a String. And it has to have both parts. When to interpret the date use only parse, and when to show on Log or elsewhere use only format

  • I’ll edit with the updated code then tell me something

  • I’ve already edited can say your opinion please ?

  • The problem is in import parse returns a java.util.date and not a java.sql.date. Or exchange the import or when it receives the date it has to indicate that it is of the type java.util.Date. I’ll edit the answer to contemplate that too

  • Father saved me , If I could even give you 10 certain Fodasse, See what for ? You’re Uga! It’s well ! Thank you

Show 6 more comments

Browser other questions tagged

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