6

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.

1
  • 3
    Is there a problem here? It sounds like your solution works fine. No, there is not a standard method in Java that will get the time of day in minutes. Sep 12, 2014 at 17:48

5 Answers 5

8

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);
2

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.

0
2

Hi maybe you could use JodaTime? Below example how to get number of minutes from parsed string and from current time. In java 8 there is similar api but I haven't found exactly method like minutesOfDay()

@Test
public void learnHowManyMinutesPassedToday() {
   DateTime time = DateTimeFormat.forPattern("HH:mm").parseDateTime("11:34");
   System.out.println(time.getMinuteOfDay());

   System.out.println(DateTime.now().getMinuteOfDay());
} 
1

If you are looking to have input not from a String, take a look at

Java.util.Calendar.

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. It is a simple calculation.

0
   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 using split use the above.

10
  • 3
    Please add an explanation of how your code solves the problem, so that your answer is clearer to people who need guidance.
    – Bobulous
    Sep 12, 2014 at 19:03
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. Sep 12, 2014 at 19:20
  • @Arkanon Did you really down this post because of that? I believe it was actually obvious. He was using split to get minutes instead he can use this.
    – Rika
    Sep 12, 2014 at 19:56
  • @user503413 Did you down the post? I believe it was actually obvious. He was using split to get minutes instead he can use this.
    – Rika
    Sep 12, 2014 at 20:01
  • 1
    @Rika no, I didn't down the post - usually I post the reason I downed it and pointing clearly why I downed it in the first place. You should start a witch hunt to see who down voted, better look into what the comments are saying and improving your response. As you can see, this is a quite meritocratic and open community and each of its members has the right to up/down vote a question/response if they find it fit to do so. So...to conclude as a recommendation look at other high voted responses and see how they look then start following that direction on your posts. Sep 13, 2014 at 8:48

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.