-4

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.

deHaar
  • 17,687
  • 10
  • 38
  • 51
david_23
  • 5
  • 4
  • 2
    What does this integer value represent - seconds, milliseconds, something else? – Chaosfire Feb 01 '23 at 07:25
  • 2
    Not mentioning the unit of the `totalDuration` makes this more a guessing game than an actual question. – deHaar Feb 01 '23 at 07:44
  • totalDuration = 47 I'm considering as a minuets. yes you are right i need to give it like this way (47: - > 0:47). if "totalDuration" greater than 60 i have to convert it to hours right? – david_23 Feb 01 '23 at 07:59
  • 2
    @david_23 put this information into your question. You can still edit it. – Queeg Feb 01 '23 at 08:15

3 Answers3

2

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 write 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.toMinutesPart(),
                                    duration.toSecondsPart()));
}

Output:

Total duration: 2820s
Total duration: 47 minutes 0 seconds

Note: The methods toMinutesPart() and toSecondsPart() were added with Java 9. They are equivalent to calculating remaining units by % 60. That means you'll have to write duration.toMinutes() % 60 if you want the returned minutes to be less than 60 (remaining ones apart from those forming whole hours).

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    Thanks for showing the OP the use of modern date-time API! Change `duration.toMinutes()` to `duration.toMinutes() % 60`. A better way is to use just Java-9 API instead of mixing the Java-8 and Java-9 APIs. Check https://stackoverflow.com/a/65531362/10819573 for detailed formatting. – Arvind Kumar Avinash Feb 01 '23 at 13:28
  • Good point, @ArvindKumarAvinash, thanks! I edited the question accordingly. – deHaar Feb 02 '23 at 07:28
0

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.

0

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);
        }
    }
Jai Prakash
  • 162
  • 1
  • 10