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
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 ()
@Test
public void learnHowManyMinutesPassedToday() {
DateTime time = DateTimeFormat.forPattern("HH:mm").parseDateTime("11:34");
System.out.println(time.getMinuteOfDay());
System.out.println(DateTime.now().getMinuteOfDay());
}
source to share