Skip to content

Commit

Permalink
feat: Add python and java time conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Romuald Rousseau committed Aug 31, 2024
1 parent 8368ef0 commit 60c0842
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,55 @@ public static String toPython(final String javaPattern) {
.replaceAll("YYYY", "%G")
.replaceAll("yyyy", "%Y")
.replaceAll("yy", "%y")
.replaceAll("y", "%-y")
.replaceAll("MMMMM", "%B")
.replaceAll("MMM", "%b")
.replaceAll("MM", "%m")
.replaceAll("M", "%-m")
.replaceAll("DDD", "%j")
.replaceAll("dd", "%d")
.replaceAll("d", "%-d")
.replaceAll("EEEEE", "%A")
.replaceAll("EEE", "%a")
.replaceAll("ww", "%W")
.replaceAll("u", "%u");
.replaceAll("u", "%u")
.replaceAll("HH", "%H")
.replaceAll("H", "%-H")
.replaceAll("hh", "%I")
.replaceAll("h", "%-I")
.replaceAll("mm", "%M")
.replaceAll("m", "%-M")
.replaceAll("ss", "%S")
.replaceAll("s", "%-S");
}

public static String toJava(final String pythonPattern) {
return pythonPattern
.replaceAll("%G", "YYYY")
.replaceAll("%Y", "yyyy")
.replaceAll("%y", "yy")
.replaceAll("%-y", "y")
.replaceAll("%B", "MMMMM")
.replaceAll("%b", "MMM")
.replaceAll("%m", "MM")
.replaceAll("%-m", "M")
.replaceAll("%j", "DDD")
.replaceAll("%d", "dd")
.replaceAll("%-d", "d")
.replaceAll("%A", "EEEEE")
.replaceAll("%a", "EEE")
.replaceAll("%W", "ww")
.replaceAll("%w", "u")
.replaceAll("%u", "u")
.replaceAll("%U", "ww")
.replaceAll("%V", "ww");
.replaceAll("%V", "ww")
.replaceAll("%H", "HH")
.replaceAll("%-H", "H")
.replaceAll("%I", "hh")
.replaceAll("%-I", "h")
.replaceAll("%M", "mm")
.replaceAll("%-M", "m")
.replaceAll("%S", "ss")
.replaceAll("%-S", "s");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ public void testPythonSimpleDateformat() throws ParseException {
assertEquals("Sun,24/09/23", formatter.format(Date.from(LocalDate.of(2023, 9, 24).atStartOfDay(ZoneId.systemDefault()).toInstant())));
assertEquals("Sun,05/12/99", formatter.format(formatter.parse("Sun,05/12/99")));
}

@Test
public void testPythonSimpleDateformatWithTime() throws ParseException {
final PythonSimpleDateFormat formatter = new PythonSimpleDateFormat("%Y.%m.%d. %H:%M");
assertEquals("2023.03.16. 11:36", formatter.format(formatter.parse("2023.03.16. 11:36")));
}
}

0 comments on commit 60c0842

Please sign in to comment.