user4850912
user4850912

Reputation:

How to extract number of minutes from time sentence in java? e.g xx hrs xx mins

I know the title is confusing but I couldn't think of a better one.

Say I've got a string, which could look something like ([number] hr(s)) [number] min(s)

Some examples of that would be 1 min, 15 mins, 3 hrs 50 mins, 1 hr 15 mins, 4 hrs 1 min etc

How do I efficiently extract from such strings the number of minutes? I want it to take into account hours too, so 3 hrs 50 mins would yield 230. The only thing I can think of is doing a lot of if clauses and using regex but I'm wondering if there is a more efficient way.

Upvotes: 0

Views: 411

Answers (4)

Marvin
Marvin

Reputation: 14255

A different approach offering you more flexibility:

public long getMinutes(String input) {
    input = input.toLowerCase()
                 .replaceAll("mins?", "M") 
                 .replaceAll("hrs?", "H")
                 .replaceAll("\\s+", "");
    Duration d = Duration.parse("PT" + input);
    return d.toMinutes();
}

(Working sample on ideone)

Outputs for your sample data:

1 min: 1
15 mins: 15
3 hrs 50 mins: 230
1 hr 15 mins: 75
4 hrs 1 min: 241

The basic idea is to convert your input to a string that is supported by Java's Duration#parse method (PT<hours>H<minutes>M) and then let that work out the magic. You end up with a Duration object that provides various ways of working with it afterwards.

Note that this prints 230 for 3 hrs 50 mins. If you require only the 50 then a simple regular expression would be the easier way.

(In production code I would merge the various replaceAll calls into one and replace the toLowerCase with case-insensitive pattern matching for better efficiency. It is left here explicitly for better understandability.)

Upvotes: 4

Hugo G
Hugo G

Reputation: 16494

Here's a RegEx solution:

((\d+)\s*(hours|hrs|hr|h)\s*)?(\d\d?)\s*(minutes|mins|min|m)

Number of minutes:

minutes = $1 * 60 + $4

Rules:

  • you must provide mins, even if it's a full hour
  • you can omit the hours, so 1 min is equivalent to 0 hrs 1 min
  • if the pattern doesn't match, the format is invalid
  • for best results use with case insensitive matching (usually i option)

Playground: https://regex101.com/r/u8BMtl/1

Upvotes: 1

JVOneLife
JVOneLife

Reputation: 74

You can use the following code, hope it helps.

String time="3 hrs 50 mins";
    String[] array = time.split(" ");
    int minutes = 0;
    if(array.length>2){  // if it has minute and hour content 
        minutes = Integer.parseInt(array[0])*60 + Integer.parseInt(array[2]);
    } else{  // if time has only minute content
        minutes = Integer.parseInt(array[0]);
    }

Upvotes: 0

sheplu
sheplu

Reputation: 2975

if your time is X hrs Y min

String time = "3 hrs 10 min";
String[] parts = time.split(" ");
int total = Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[2]);

this wait it will give you the string of hours, multiply by 60 minutes and add minutes. If you only need minutes, just take parts[2]

you could use Regex too

Upvotes: -1

Related Questions