fork download
  1. import java.time.OffsetDateTime;
  2. import java.time.format.DateTimeParseException;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. System.out.println("Java Version: " + System.getProperty("java.version"));
  7.  
  8. String[] timestamps = {
  9. "2023-10-05T15:14:29.123456789Z", // 9 digits
  10. "2023-10-05T15:14:29.12345678Z", // 8 digits
  11. "2023-10-05T15:14:29.1234567Z", // 7 digits
  12. "2023-10-05T15:14:29.123456Z", // 6 digits
  13. "2023-10-05T15:14:29.12345Z", // 5 digits
  14. "2023-10-05T15:14:29.1234Z", // 4 digits
  15. "2023-10-05T15:14:29.123Z", // 3 digits
  16. "2023-10-05T15:14:29.12Z", // 2 digits
  17. "2023-10-05T15:14:29.1Z", // 1 digit
  18. "2023-10-05T15:14:29Z" // no fractional seconds
  19. };
  20.  
  21. for (String timestamp : timestamps) {
  22. try {
  23. System.out.println("Parsed date: " + OffsetDateTime.parse(timestamp));
  24. } catch (DateTimeParseException e) {
  25. System.err.println("Failed to parse: " + timestamp + " - " + e.getMessage());
  26. }
  27. }
  28. }
  29. }
Success #stdin #stdout 0.13s 56556KB
stdin
Standard input is empty
stdout
Java Version: 12.0.1
Parsed date: 2023-10-05T15:14:29.123456789Z
Parsed date: 2023-10-05T15:14:29.123456780Z
Parsed date: 2023-10-05T15:14:29.123456700Z
Parsed date: 2023-10-05T15:14:29.123456Z
Parsed date: 2023-10-05T15:14:29.123450Z
Parsed date: 2023-10-05T15:14:29.123400Z
Parsed date: 2023-10-05T15:14:29.123Z
Parsed date: 2023-10-05T15:14:29.120Z
Parsed date: 2023-10-05T15:14:29.100Z
Parsed date: 2023-10-05T15:14:29Z