fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.time.*;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14.  
  15. System.out.println( "Java version: " + System.getProperty("java.version") ) ;
  16.  
  17. // OffsetDateTime parses ISO 8601 format with offset that includes a colon.
  18. OffsetDateTime odt1 = OffsetDateTime.parse( "2017-05-16 06:24:36-07:00".replace( " " , "T" ) ) ;
  19. System.out.println( "odt1.toString(): " + odt1 ) ;
  20.  
  21. // But OffsetDateTime fails to parse if the optional colon in offset is omitted.
  22. // BUG in Java 8, fixed in Java 9.
  23. OffsetDateTime odt2 = OffsetDateTime.parse( "2017-05-16 06:24:36-0700".replace( " " , "T" ) ) ; // BUG: Throws DateTimeParseException.
  24. System.out.println( "odt2.toString(): " + odt2 ) ;
  25.  
  26. }
  27. }
Runtime error #stdin #stdout #stderr 0.14s 4386816KB
stdin
Standard input is empty
stdout
Java version: 1.8.0_112
odt1.toString(): 2017-05-16T06:24:36-07:00
stderr
Exception in thread "main" java.time.format.DateTimeParseException: Text '2017-05-16T06:24:36-0700' could not be parsed at index 19
	at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
	at java.time.OffsetDateTime.parse(OffsetDateTime.java:402)
	at java.time.OffsetDateTime.parse(OffsetDateTime.java:387)
	at Ideone.main(Main.java:23)