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. import java.time.temporal.*;
  9. import java.time.format.*;
  10.  
  11. // By Basil Bourque.
  12. // Example code for a Question & Answer at Stack Overflow.
  13. // http://stackoverflow.com/a/44011744/642706
  14.  
  15. /* Name of the class has to be "Main" only if the class is public. */
  16. class Ideone
  17. {
  18. public static void main (String[] args) throws java.lang.Exception
  19. {
  20.  
  21. String input = "Tue, 16 May 2017 13:02:16 GMT" ;
  22.  
  23. // Built-in formatter.
  24. DateTimeFormatter f = DateTimeFormatter.RFC_1123_DATE_TIME ;
  25. ZonedDateTime zdt = ZonedDateTime.parse( input , f ) ;
  26.  
  27. System.out.println( "input: " + input ) ;
  28. System.out.println( "zdt.toString(): " + zdt ) ;
  29.  
  30. // Custom formatter
  31. DateTimeFormatter f2 = DateTimeFormatter.ofPattern( "EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH ) ;
  32. ZonedDateTime zdt2 = ZonedDateTime.parse( input , f2 ) ;
  33.  
  34. System.out.println( "zdt2.toString(): " + zdt2 ) ;
  35.  
  36. }
  37. }
Success #stdin #stdout 0.16s 4386816KB
stdin
Standard input is empty
stdout
input: Tue, 16 May 2017 13:02:16 GMT
zdt.toString(): 2017-05-16T13:02:16Z
zdt2.toString(): 2017-05-16T13:02:16Z[GMT]