Vinicius Araujo Vinicius Araujo - 2 years ago 73
Java Question

Java - Store amount of time (not interval)

I work with an spring4 webapp that needs the logic of storing and calculating an amount of time(hours and minutes), but it isn't an interval since the amount is inserted by user or retrieved by third part app with the HH:MM format.

By now it's done with Float values which I want to change because in float the minutes are in 100 base, and not in 60 base as it's correct.
I've tried the java.time.LocalTime but it doesn't work since it's not acceptable more than 24 hrs.

I think there may be a cleaner way to deal with it.

Thanks in advance


This webapp calculate overtime work. The user inputs the amount of hours the employee have worked off contractual time.

At the moment it is mapped as a float field, which is converted an calculated as below:

float hours = //Conversion of the HH:MM string input to HH.MM float within the framework
valueToPay = hours * employee.getHourSalary();

This way isn't right because the minutes are not being calculated correctly. I could convert the entire time to amount of minutes, but I'm searching for a cleaner way, since the java.time API offers a lot.

Answer Source

LocalTime is for time of the day.

If you need a ducation, you have java.time.Duration. For example, a duration of 30 hours (which would not fit in a LocalTime):

Duration thirtyHours = Duration.ofHours(30);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download