I need help writing "01:16:50" as 76 Minutes in Android.
I have already tried searching with no help.
I need help writing "01:16:50" as 76 Minutes in Android.
I have already tried searching with no help.
A simple one-liner:
Duration.between(
LocalTime.MIN ,
LocalTime.parse( "01:16:50" )
)
.toMinutes()
See this code run live at IdeOne.com.
76
Your input string is unfortunately using time-of-day format rather than the ISO 8601 standard format for durations.
So we must pull a trick in order to decipher your input as a span-of-time rather than a time-of-day.
First we parse the input as a time-of-day, a LocalTime
object.
LocalTime lt = LocalTime.parse( "01:16:50" ) ;
Then we calculate elapsed time (Duration
) from 00:00 (LocalTime.MIN
) to that pretend time-of-day. That effectively gives us the amount of time intended by your input.
Duration duration = Duration.between( LocalTime.MIN , lt ) ;
Lastly, we interrogate that Duration
object for its total length as a count of whole minutes.
long minutes = duration.toMinutes() ;
See this code run live at IdeOne.com.
duration: PT1H16M50S
minutes: 76
The above code is in Java syntax. Of course you could do the same in Kotlin syntax. See bullets below about versions of Android.
I recommend you educate the publisher of your data about ISO 8601. Those standard formats are designed for data exchange of date-time values textually while avoiding ambiguity. Your use of time-of-day format for a duration is inherently ambiguous, confusing, and error-prone.
Duration.parse( "PT1H16M50S" ).toMinutes()
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
Try this:
public class Help{
//method start
public static String convertHoursToMinutes(String time){
try{
if(time.charAt(0) == '0' && time.charAt(3) == '0'){
time = String.valueOf(Integer.parseInt(String.valueOf(time.charAt(1))) * 60 + Integer.parseInt(String.valueOf(time.charAt(4))));
}else if(time.charAt(0) != '0' && time.charAt(3) == '0'){
time = String.valueOf(Integer.parseInt(String.valueOf(time.charAt(0)) + String.valueOf(time.charAt(1))) * 60 + Integer.parseInt(String.valueOf(time.charAt(4))));
}else if(time.charAt(0) == '0' && time.charAt(3) != '0'){
time = String.valueOf(Integer.parseInt(String.valueOf(time.charAt(1))) * 60 + Integer.parseInt(String.valueOf(time.charAt(3)) + String.valueOf(time.charAt(4))));
}else if(time.charAt(0) != '0' && time.charAt(3) != '0'){
time = String.valueOf(Integer.parseInt(String.valueOf(time.charAt(0)) + String.valueOf(time.charAt(1))) * 60 + Integer.parseInt(String.valueOf(time.charAt(3)) + String.valueOf(time.charAt(4))));
}else{
System.err.println("ERROR");
}
}catch(Exception e){
System.err.println("ERROR");
}
return time;
}
//method end
public static void main(String[] args) {
System.out.println(convertHoursToMinutes("01:16:50"));
}
}