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.format.*;
  9. import java.time.temporal.*;
  10. import java.time.zone.*;
  11. import java.util.concurrent.*;
  12.  
  13.  
  14. /* Name of the class has to be "Main" only if the class is public. */
  15. class Ideone
  16. {
  17. public static void main (String[] args) throws java.lang.Exception
  18. {
  19.  
  20. List<CharSequence> inputs = new ArrayList<> ( 4 );
  21.  
  22. inputs.add ( "Mon Jul 28 11:39:29 GMT-05:00 2014" );
  23. inputs.add ( "Mon Jul 28 13:39:29 GMT+02:00 2014" );
  24. inputs.add ( "Thu Jul 17 00:02:02 UTC 2014" );
  25. inputs.add ( "Fri Jul 18 14:47:01 UTC 2014" );
  26. inputs.add ( "garbage-in garbaage-out" );
  27. inputs.add ( "Fri Jan 23 12:34:56 UTC 2099" ); // Future.
  28.  
  29. Instant now = Instant.now (); // Capture the current moment in UTC.
  30.  
  31. DateTimeFormatter fGMT = DateTimeFormatter.ofPattern ( "EEE MMM dd HH:mm:ss OOOO uuuu" );
  32. DateTimeFormatter fUTC = DateTimeFormatter.ofPattern ( "EEE MMM dd HH:mm:ss z uuuu" );
  33.  
  34. for ( CharSequence input : inputs ) {
  35. if ( input.length () == "Mon Jul 28 11:39:29 GMT-05:00 2014".length () ) {
  36. OffsetDateTime odt = OffsetDateTime.parse ( input , fGMT );
  37. if ( odt.toInstant ().isAfter ( now ) ) {
  38. System.out.println ( "input: " + input + " is future." );
  39. } else {
  40. System.out.println ( "input: " + input + " is past or present." );
  41. }
  42. } else if ( input.length () == "Thu Jul 17 00:02:02 UTC 2014".length () ) {
  43. ZonedDateTime zdt = ZonedDateTime.parse ( input , fUTC );
  44. if ( zdt.toInstant ().isAfter ( now ) ) {
  45. System.out.println ( "input: " + input + " is future." );
  46. } else {
  47. System.out.println ( "input: " + input + " is past or present." );
  48. }
  49. } else {
  50. System.out.println ( "ERROR - Unexpected input: " + input );
  51. }
  52. }
  53.  
  54. }
  55. }
Success #stdin #stdout 0.15s 4386816KB
stdin
Standard input is empty
stdout
input: Mon Jul 28 11:39:29 GMT-05:00 2014 is past or present.
input: Mon Jul 28 13:39:29 GMT+02:00 2014 is past or present.
input: Thu Jul 17 00:02:02 UTC 2014 is past or present.
input: Fri Jul 18 14:47:01 UTC 2014 is past or present.
ERROR - Unexpected input: garbage-in garbaage-out
input: Fri Jan 23 12:34:56 UTC 2099 is future.