Junior Antoniolli Junior Antoniolli - 1 year ago 51
Android Question

How can I convert a "String HH:MM" to a "int hour HH" and "int minute MM"

I have this code. And I need convert a string "horaTarefa HH:MM" to int "hour = HH" and int "minute = MM"

I guess can I do the same thing with "dateTarefa DD/MM/YYYY"?

SQLiteDatabase db;
db = openOrCreateDatabase("AgendadorTarefas.db",SQLiteDatabase.CREATE_IF_NECESSARY,null); { }

Cursor c = db.query("Tarefa", new String[] {
"codTarefa", "dateTarefa", "horaTarefa", "textoTarefa"}, null, null, null, null, null);


final ArrayList<String> result = new ArrayList<String>();

String Date = c.getString(1);
String Hora = c.getString(2);
String Texto = c.getString(3);
result.add(Data + " às " + Hora + " \n" + Texto);


hour = Calendar.HOUR;
minute = Calendar.MINUTE;



Answer Source

If you know the position of the hour/minute/etc and it is always the same, you can get it with substring, and then convert it to numbers with Integer.parseInt

String Hora = "12:34";
int hour = Integer.parseInt(Hora.substring(0, 2));
int minute = Integer.parseInt(Hora.substring(3, 5));

String Date = "12/34/5678";
int day = Integer.parseInt(Date.substring(0, 2));
int month = Integer.parseInt(Date.substring(3, 5));
int year = Integer.parseInt(Date.substring(6, 10));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download