1 byjy 0 5/12/2025, 2:33:58 PM

Comments (0)

byjy · 8h ago
Java's built-in DateTimeFormatter is powerful, but hard to remember and painful to read.

DateTimeFormats is a small Java library that parses date/time strings based on actual examples (like “Tue, 2024-03-19 15:00:00 America/New_York”) instead of using pattern letters like EEEE, zzz, VV, SSS.

Think of it as an example-driven wrapper around DateTimeFormatter.

Examples:

  parseLocalDate("2024-03-19")

  parseZonedDateTime("Tue, 19 Mar 2024 15:00:00 UTC")

  formatOf("Tue, 19 Mar 2024 15:00:00 UTC") → E, dd LLL yyyy HH:mm:ss zzz

  formatOf("<Tue>, dd-MM-yyyy") → mixed example + explicit pattern
All inferred formatters are verified roundtrip-safe by JDK DateTimeFormatter to ensure correctness.

Ambiguous examples such as 01/02/2025 are rejected, but you can use unambiguous examples like 10/30/2025.

You can also combine literal examples with pattern fragments (e.g. to express optional precision).

More at:

- Blog: https://github.com/google/mug/wiki/Parsing-Date-Time-Should-...

- Javadoc: https://google.github.io/mug/apidocs/com/google/mu/time/Date...

- GitHub: https://github.com/google/mug

Would love feedback — especially if you've had to parse db dumps, scrape dates from logs, or reimplement Go’s time parsing in Java.