Simpledateformat cast String does not query in the database

Asked

Viewed 46 times

0

I’m making a collaborative application for android, so I made a server in PHP, and send the data to this server.

For a certain query I need to pass a date, from a Date Picker, here is the code:

String myFormat = "dd-MM-yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
et_data.setText(sdf.format(myCalendar.getTime()));
String data = sdf.format(myCalendar.getTime()) +"";

The date variable is passed to the server, but on the server it is not interpreting as text.

But if I set the date variable to:

String data = "12-02-2018";

Works normally. The value that arrives on the server is this same, and the type is also String.

Does anyone know what it can be?

  • I did not understand your problem, I tested your code and it is working, at least as I imagine it should, since it is not clear your problem: https://ideone.com/ntrhqD

  • That’s right, to me the code seems right. When I receive it on the PHP server it does not interpret this -> (String data = sdf.format(myCalendar.getTime()) +"";), as a String.

  • So the problem is not on the java side.

  • How are you receiving this on the server side in PHP ? How do you "upload" the information ?

  • You said if you set the variable as 12-02-2018 works. But the SimpleDateFormat that you created uses dd-MM-yy, which only prints the year with two digits. Try switching to dd-MM-yyyy

  • Another detail is that the method format() already returns a String, and concatenate with the String empty (the part at the end +"") is redundant and can be removed. So it would be only String data = sdf.format(myCalendar.getTime());

Show 1 more comment

1 answer

0

Change your format to dd-MM-yyyy because if you put with 2 y only it will take the literal value and not numerical.

Ex:

String myFormat = "dd-MM-yyyy";

Browser other questions tagged

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