Shudy
Shudy

Reputation: 7936

Formating String to Date in Android, but Result date is not OK

In the app, I have done that the user choose some date and time with DAte and Time PickerFragments.

When the choose the date , I put it in TextViews. after that I need to verify that the dates that the user choose are correct. I mean, the end date can't be before that the start date.

To test that the conversion is doing OK I have donde this code:

String start_date = (String) tv_start_date.getText();
String end_date = (String) tv_end_date.getText();
String start_hour = (String) tv_start_hour.getText();
String end_hour = (String) tv_end_hour.getText();
String auxiliar = start_date  + " " + start_hour;
String auxiliar2 = end_date + " " + end_hour;

SimpleDateFormat sdf_aux = new SimpleDateFormat("dd/mm/yy hh:mm");

Date auuuux = sdf_aux.parse(auxiliar);
Date auuuux2 = sdf_aux.parse(auxiliar2);

Log.d("Meeting", "Inicio: Start--> "+auuuux.toString());
Log.d("Meeting", "FIN: END--> "+auuuux2.toString());

But when I see the Log, I can see that the date, is not correct:

Few Images:

enter image description here enter image description here

Log:

03-26 13:25:27.657: D/Meeting(15255): Start--> Mon Jan 26 13:25:00 CET 2015
03-26 13:25:27.657: D/Meeting(15255): END--> Tue Jan 27 14:26:00 CET 2015

As you can see, today is Thursday 26/3/15 , but if you see the log it says that is Monday!

How can I fix it? I have done somthing wrong?

Upvotes: 0

Views: 52

Answers (1)

Opiatefuchs
Opiatefuchs

Reputation: 9870

You have put the wrong String in Your date Format:

 SimpleDateFormat sdf_aux = new SimpleDateFormat("dd/mm/yy hh:mm");

it must be:

 SimpleDateFormat sdf_aux = new SimpleDateFormat("dd/MM/yy hh:mm");

because the capital M is for month, the small for Minutes...

EDIT

To get the time in 24 hour format, You have to change the h to H:

  SimpleDateFormat sdf_aux = new SimpleDateFormat("dd/MM/yy HH:mm");

Upvotes: 2

Related Questions