fork(1) download
  1. import java.time.LocalDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.format.DateTimeParseException;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. System.out.println("Java Version: " + System.getProperty("java.version"));
  8.  
  9. String[] timestamps = {
  10. "2023-10-05T15:14:29.123456789Z", // 9 digits
  11. "2023-10-05T15:14:29.12345678Z", // 8 digits
  12. "2023-10-05T15:14:29.1234567Z", // 7 digits
  13. "2023-10-05T15:14:29.123456Z", // 6 digits
  14. "2023-10-05T15:14:29.12345Z", // 5 digits
  15. "2023-10-05T15:14:29.1234Z", // 4 digits
  16. "2023-10-05T15:14:29.123Z", // 3 digits
  17. "2023-10-05T15:14:29.12Z", // 2 digits
  18. "2023-10-05T15:14:29.1Z", // 1 digit
  19. "2023-10-05T15:14:29Z" // no fractional seconds
  20. };
  21.  
  22. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSSSSSSSS]'Z'");
  23.  
  24. for (String timestamp : timestamps) {
  25. try {
  26. LocalDateTime dateTime = LocalDateTime.parse(timestamp, formatter);
  27. System.out.println("Parsed date: " + dateTime);
  28. } catch (DateTimeParseException e) {
  29. System.err.println("Failed to parse: " + timestamp + " - " + e.getMessage());
  30. }
  31. }
  32. }
  33. }
Success #stdin #stdout #stderr 0.16s 58932KB
stdin
Standard input is empty
stdout
Java Version: 12.0.1
Parsed date: 2023-10-05T15:14:29.123456789
Parsed date: 2023-10-05T15:14:29.123456780
Parsed date: 2023-10-05T15:14:29
stderr
Failed to parse: 2023-10-05T15:14:29.1234567Z - Text '2023-10-05T15:14:29.1234567Z' could not be parsed at index 19
Failed to parse: 2023-10-05T15:14:29.123456Z - Text '2023-10-05T15:14:29.123456Z' could not be parsed at index 19
Failed to parse: 2023-10-05T15:14:29.12345Z - Text '2023-10-05T15:14:29.12345Z' could not be parsed at index 19
Failed to parse: 2023-10-05T15:14:29.1234Z - Text '2023-10-05T15:14:29.1234Z' could not be parsed at index 19
Failed to parse: 2023-10-05T15:14:29.123Z - Text '2023-10-05T15:14:29.123Z' could not be parsed at index 19
Failed to parse: 2023-10-05T15:14:29.12Z - Text '2023-10-05T15:14:29.12Z' could not be parsed at index 19
Failed to parse: 2023-10-05T15:14:29.1Z - Text '2023-10-05T15:14:29.1Z' could not be parsed at index 19