-4

I need help writing "01:16:50" as 76 Minutes in Android.

I have already tried searching with no help.

Web Services
  • 77
  • 1
  • 8
  • Is there anything you have tried so far – Aniketh Malyala Aug 03 '21 at 22:04
  • I'm new to Android so not so much. The examples I found are not close to my case. – Web Services Aug 03 '21 at 22:07
  • 1
    Your "case" is literally taking the number of hours, multiplying it by 60 and adding the number of minutes. Or converting the timestamp to some kind of timestamp object, take the beginning of day and counting the interval between the two using whatever date API Kotlin has. Surely it can't be that difficult. – AlgorithmFromHell Aug 03 '21 at 22:13
  • @AlgorithmFromHell I can tell your explanation will be helpful as I can learning more. Any chance you can offer some code help for the first option you mentioned? – Web Services Aug 03 '21 at 22:18
  • 2
    If you're really struggling with this you should probably not write Android apps as it's too difficult for a beginner. Better write simple standalone programs, study primitive types, how to convert them etc. – AlgorithmFromHell Aug 03 '21 at 22:24
  • You're welcome, happy learning – AlgorithmFromHell Aug 03 '21 at 22:42
  • On the occasion of your question I have written a new answer to the linked original question [here](https://stackoverflow.com/a/68645898/5772882). – Ole V.V. Aug 04 '21 at 19:20

2 Answers2

2

tl;dr

A simple one-liner:

Duration.between( 
    LocalTime.MIN ,
    LocalTime.parse( "01:16:50" )
)
.toMinutes() 

See this code run live at IdeOne.com.

76

Details

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()

About java.time

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-2

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"));
}
}
Dequog
  • 124
  • 13
  • 2
    Not my down-votes, but they may result from your use of complicated string manipulations instead of the *java.time* classes bundled with Java for the purpose of handling date-time values. – Basil Bourque Aug 04 '21 at 04:13
  • 1
    @CoomptKing it's a very bad implementation, you are using string manipulations instead of just converting the string into time based object and extracting what you need from that. – MihaiC Aug 04 '21 at 05:15