I'm getting integer value from db as example integer value = 47. But when I pass that value to front end I need to convert it as minutes.
My scenario is I have 4 steps each step I'm getting an integer value, and after getting integer values I get the SUM of that 4 values. Then I have to convert that to minutes or hours.
Integer totalDuration = (engageDuration evaluateDuration explainDuration extendDuration);
OutPut:-
"totalDuration": 47
I need to convert this output for minutes and send it back to the front-end.
CodePudding user response:
Does the integer represent days?
days to minutes
minutes = days * 24 *60
days to hours
minutes = days * 24
If the integer doesn't represent days, then please comment it.
CodePudding user response:
There's java.time.Duration
and depending on the unit of your totalDuration
, you could simply build up a Duration
based on that value and then convert to minutes.
Let's assume totalDuration
is a value of seconds. You could something like the following code:
public static void main(String[] args) {
// example values
int engageDuration = 1200;
int evaluateDuration = 320;
int explainDuration = 1200;
int extendDuration = 100;
// your summming operation
int totalDuration = (engageDuration
evaluateDuration
explainDuration
extendDuration);
// THE IMPORTANT PART: making it a Duration of seconds
Duration duration = Duration.ofSeconds(totalDuration);
// print the amount of seconds
System.out.println("Total duration: " totalDuration "s");
// print the amount of full minutes and remaining seconds
System.out.println(String.format("Total duration: %d minutes %d seconds",
duration.toMinutes(),
duration.toSecondsPart()));
}
Output:
Total duration: 2820s
Total duration: 47 minutes 0 seconds
CodePudding user response:
According to your question, you need an Integer duration of an hour and minutes like 00:00
long miliSeconds=durection*60*100;
String formatDuration = DurationFormatUtils.formatDuration(miliSeconds, "HH:mm", true);
if you are using DurationFormatUtils then you need to add a dependency in pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
Below example of the main class:
import org.apache.commons.lang3.time.DurationFormatUtils;
public class DurationExample {
public static void main(String[] args) {
long duration=47*60*1000;
String formatDuration = DurationFormatUtils.formatDuration(duration, "HH:mm", true);
System.out.println(formatDuration);
}
}