-1

Wondering how to format output of a localtime with two different longs: My goal here is to format the long minutes and long seconds with the until part

Example of desired output: 9:02 As in 9 minutes and 2 seconds

    public static String getTimeUntilSomething() {
        LocalTime currentTime = LocalTime.now();
        
        long minutes = currentTime.until(currentTimeMore, ChronoUnit.MINUTES);
        long seconds = currentTime.until(currentTimeMore, ChronoUnit.SECONDS);

        String L = currentTime.format(DateTimeFormatter.ofPattern("m:ss"));
        return L;
    }
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
fadjeca
  • 9
  • 4
  • added it, basicly im trying to figure out how to calculate times in bettween then formating it. – fadjeca Mar 21 '21 at 19:03
  • I don't understand what using the date format call is giving you. - why not just use `String.format()` to do this? Is it that the `minutes` and `seconds` values don't trivially map to your output (ie: aren't necessarily `9` and `2` in your example)? – CryptoFool Mar 21 '21 at 19:04
  • The String before was returning `return minutes % 60 + ":" + seconds % 60;` and String.format was retuning a null value, and i need to to display the 0 when the second is less than 10 as "09 secs left" – fadjeca Mar 21 '21 at 19:08

2 Answers2

2

I suggest you do it using java.time.Duration which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation. With Java-9 some more convenience methods were introduced.

Demo:

import java.time.Duration;
import java.time.LocalTime;

class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getTimeUntilSomething(LocalTime.of(21, 10, 20)));
    }

    public static String getTimeUntilSomething(LocalTime currentTimeMore) {
        LocalTime currentTime = LocalTime.now();
        Duration duration = Duration.between(currentTime, currentTimeMore);

        // ####################################Java-8####################################
        return String.format("%2d:%02d", duration.toMinutes(), duration.toSeconds() % 60);
        // ##############################################################################

        // ####################################Java-9####################################
        // return String.format("%d:%02d", duration.toMinutes(), duration.toSecondsPart());
        // ####################################Java-9####################################
    }
}

A sample run:

94:40
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Given what you say in your comment, I think you can just use String.format(), like this:

public static String getTimeUntilSomething() {
    LocalTime currentTime = LocalTime.now();
    
    long minutes = currentTime.until(currentTimeMore, ChronoUnit.MINUTES);
    long seconds = currentTime.until(currentTimeMore, ChronoUnit.SECONDS);

    String L = String.format("%d:%02d", minutes % 60, seconds % 60);
    return L;
}
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Changed this a little bit but i got it as i wanted, thank you – fadjeca Mar 21 '21 at 19:19
  • @fadjeca - This will return a wrong value. Try it for something like `LocalTime.of(21, 10, 20)` e.g. it's `19:38:27` in my timezone and this solution will give `31:53` which is wrong. – Arvind Kumar Avinash Mar 21 '21 at 19:40
  • @ArvindKumarAvinash - I know that. I was going by what the OP was asking for, which was just a formatting question. I didn't try to change the "two longs" part of the problem...that seemed built into the question. - I think both answers have value. I don't see my answer as wrong...and neither did the OP it seems. – CryptoFool Mar 21 '21 at 19:41
  • 1
    @CryptoFool - You should correct it. The answer should be correct and complete. – Arvind Kumar Avinash Mar 21 '21 at 19:49
  • 1
    I recommend against doing your own time math, as in your modulo operations. java.time can do all of your calculations for you, with a smaller risk of errors and clearer to read code. – Ole V.V. Mar 22 '21 at 16:32