-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
309a37a
commit 5dd9f4b
Showing
4 changed files
with
52 additions
and
15 deletions.
There are no files selected for viewing
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
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
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
31 changes: 31 additions & 0 deletions
31
src/main/java/ru/pinkgoosik/kitsun/util/DurationUtils.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ru.pinkgoosik.kitsun.util; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.time.Duration; | ||
import java.time.format.DateTimeParseException; | ||
|
||
public class DurationUtils { | ||
|
||
@Nullable | ||
public static Duration parse(String str) { | ||
try { | ||
return Duration.parse("pt" + str); | ||
} | ||
catch (DateTimeParseException e) { | ||
System.out.println("String cant be parsed"); | ||
} | ||
return null; | ||
} | ||
|
||
public static String format(Duration duration) { | ||
long seconds = duration.getSeconds(); | ||
long absSeconds = Math.abs(seconds); | ||
String positive = String.format( | ||
"%dh%02dm%02ds", | ||
absSeconds / 3600, | ||
(absSeconds % 3600) / 60, | ||
absSeconds % 60); | ||
return seconds < 0 ? "-" + positive : positive; | ||
} | ||
} |