Example, I get a String with the time of day "16:20". The day has 1440 minutes. How to know in what interval from zero to 1440 is this time? An integer value.
Example, I get a String with the time of day "16:20". The day has 1440 minutes. How to know in what interval from zero to 1440 is this time? An integer value.
I do not think you need a class like Calendar
to solve, just a split
and do a math operation, like this:
String horacompleta = "12:39";
String[] horamin = horacompleta.split(":");
int hora = Integer.parseInt(horamin[0]);
int min = Integer.parseInt(horamin[1]);
int minutos = (hora * 60) + min; //Pega o total
You can put in a function like this:
public static int hour2minutes(String fullhour)
{
String[] parts = fullhour.split(":");
int hour = Integer.parseInt(parts[0]);
int min = Integer.parseInt(parts[1]);
return (hour * 60) + min;
}
And to use it, do so:
hour2minutes("12:39"); //759
hour2minutes("16:20"); //980
hour2minutes("23:59"); //1439
See a test at IDEONE: link
There are several ways to do this. Another one besides Guilherme would be, for example, using SimpleDataFormat
to convert the string into date format, and then use the class TimeUnit
converting milliseconds in minutes using the toMinutes()
method. Here's how it would look:
public int hour2min(String hour) {
try {
Date date = new SimpleDateFormat("kk:mm").parse(hour);
return (int) (TimeUnit.MILLISECONDS.toMinutes(date.getTime())-180);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
To use just do so:
Log.wtf("",""+hour2min("18:23"));
Output:
1103