Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time of the day in minutes Java

I am looking to calculate the number of minutes given the time of the day.

Eg.: when input is 11:34, the output should be 11*60+34. The date doesn't matter.

I only need it down to the minutes scale. Seconds, milliseconds... don't matter. Is there a method somewhere in Java doing this the neat way without me calculating it?

Right now, i'm using theTime.split(":"), theTime is a String holding "11:34" here, parsing the integers on each side and doing the calculation.

I saw Time but what I'm doing right now seemed more direct.

Nothing in Systems either.

like image 479
user3880721 Avatar asked Dec 19 '22 11:12

user3880721


2 Answers

There is no build in method for it. However here is a one-liner for it:

int timeInMins = Calendar.getInstance().get(Calendar.HOUR_OF_DAY) * 60 + Calendar.getInstance().get(Calendar.MINUTE);
like image 166
gkrls Avatar answered Dec 22 '22 00:12

gkrls


Your approach looks good and sound, however to answer your question it would be simple to say that there is no such build in method which does that. You have to calculate it the way you are doing it right now.

like image 21
Rahul Tripathi Avatar answered Dec 21 '22 23:12

Rahul Tripathi