Last active
June 6, 2018 10:20
-
-
Save InsulaVentus/c3e54efbf5e413a2cf78e7919d535771 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Stack; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
class Scratch { | |
public static void main(String[] args) { | |
System.out.println(reformat("01:20")); //1t 20m | |
System.out.println(reformat("01:00")); //1t | |
System.out.println(reformat("00:23")); //23m | |
} | |
// Reformat '01:20' to '1t 20m' | |
private static String reformat(final String time) { | |
Stack<String> postFixes = new Stack<>(); | |
postFixes.push("m"); | |
postFixes.push("t"); | |
return Stream | |
.of(time) | |
.flatMap(s -> Stream.of(s.split(":"))) | |
.map(s -> String.format("%s%s", Integer.parseInt(s), postFixes.pop())) | |
.filter(s -> !s.startsWith("0")) | |
.collect(Collectors.joining(" ")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment