fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.Locale;
  9. import java.util.TimeZone;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. System.out.println(parseRFC3339Date("2007-05-01T15:43:26-07:00"));
  17. System.out.println(parseRFC3339Date("2007-05-01T15:43:26.3-07:00"));
  18. System.out.println(parseRFC3339Date("2007-05-01T15:43:26.3452-07:00"));
  19. System.out.println(parseRFC3339Date("2007-05-01T15:43:26+07:00"));
  20. System.out.println(parseRFC3339Date("2007-05-01T15:43:26.3+07:00"));
  21. System.out.println(parseRFC3339Date("2007-05-01T15:43:26.3452+07:00"));
  22. System.out.println(parseRFC3339Date("2007-05-01T15:43:26.3452Z"));
  23. System.out.println(parseRFC3339Date("2007-05-01T15:43:26.3Z"));
  24. System.out.println(parseRFC3339Date("2007-05-01T15:43:26Z"));
  25. }
  26.  
  27. public synchronized static Date parseRFC3339Date(String dateString) throws java.text.ParseException, IndexOutOfBoundsException {
  28. Date d;
  29.  
  30. //if there is no time zone, we don't need to do any special parsing.
  31. if (dateString.endsWith("Z")) {
  32. try {
  33. SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault());//spec for RFC3339 with a 'Z'
  34. s.setTimeZone(TimeZone.getTimeZone("UTC"));
  35. d = s.parse(dateString);
  36. } catch (java.text.ParseException pe) {//try again with optional decimals
  37. SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'", Locale.getDefault());//spec for RFC3339 with a 'Z' and fractional seconds
  38. s.setTimeZone(TimeZone.getTimeZone("UTC"));
  39. s.setLenient(true);
  40. d = s.parse(dateString);
  41. }
  42. return d;
  43. }
  44.  
  45. //step one, split off the timezone.
  46. String firstPart;
  47. String secondPart;
  48. if (dateString.lastIndexOf('+') == -1) {
  49. firstPart = dateString.substring(0, dateString.lastIndexOf('-'));
  50. secondPart = dateString.substring(dateString.lastIndexOf('-'));
  51. } else {
  52. firstPart = dateString.substring(0, dateString.lastIndexOf('+'));
  53. secondPart = dateString.substring(dateString.lastIndexOf('+'));
  54. }
  55.  
  56. //step two, remove the colon from the timezone offset
  57. secondPart = secondPart.substring(0, secondPart.indexOf(':')) + secondPart.substring(secondPart.indexOf(':') + 1);
  58. dateString = firstPart + secondPart;
  59. SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());//spec for RFC3339
  60. try {
  61. d = s.parse(dateString);
  62. } catch (java.text.ParseException pe) {//try again with optional decimals
  63. s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ", Locale.getDefault());//spec for RFC3339 (with fractional seconds)
  64. s.setLenient(true);
  65. d = s.parse(dateString);
  66. }
  67. return d;
  68. }
  69. }
Success #stdin #stdout 0.18s 321472KB
stdin
Standard input is empty
stdout
Tue May 01 22:43:26 GMT 2007
Tue May 01 22:43:26 GMT 2007
Tue May 01 22:43:29 GMT 2007
Tue May 01 08:43:26 GMT 2007
Tue May 01 08:43:26 GMT 2007
Tue May 01 08:43:29 GMT 2007
Tue May 01 15:43:29 GMT 2007
Tue May 01 15:43:26 GMT 2007
Tue May 01 15:43:26 GMT 2007