Time of day in minutes Java

I expect to calculate the number of minutes given the time of day.

For example: when entering 11:34, the output should be 11 * 60 + 34. The date is irrelevant.

I only need up to the minute scale. Seconds, milliseconds ... don't matter. Is there a way somewhere in Java to do this in a neat way if I don't figure it out?

Now I use theTime.split(":")

, theTime

- a String

holding "11:34" here parsing integers on each side, and perform calculations.

I saw Time

, but what I am doing now seemed more direct.

Nothing in Systems



source to share

5 answers

There is no built-in method for this. However, there is a one-liner for this:

int timeInMins = Calendar.getInstance().get(Calendar.HOUR_OF_DAY) * 60 + Calendar.getInstance().get(Calendar.MINUTE);




Your approach looks good and sonorous, however, to answer your question it would be simple to say that no such assembly exists, which does it. You should calculate it the way you are doing it right now.



If you are looking for non-string input take a look


It has Calendar.HOUR_OF_DAY and Calendar.HOUR and Calendar.MINUTE which could be your input. I'm not sure what the "neat" way of doing this would be. This is a simple calculation.



Hi, maybe you could use JodaTime ? Below is an example of how to get the number of minutes from a parsed string and from the current time. There is a similar api in java 8 , but I didn't find exactly the same method as minutesOfDay ()

public void learnHowManyMinutesPassedToday() {
   DateTime time = DateTimeFormat.forPattern("HH:mm").parseDateTime("11:34");





   Calendar rightNow = Calendar.getInstance();
            int hour = rightNow.get(Calendar.HOUR);
    int min  = rightNow.get(Calendar.MINUTE);
    System.out.println("TimeMinutes:" + hour * 60 + min);


EDIT: Except for using split, use the above.



All Articles