Changeset 1169 in josm for trunk/src/org/openstreetmap/josm/tools/FallbackDateParser.java
- Timestamp:
- 2008-12-23T15:07:05+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/FallbackDateParser.java
r627 r1169 13 13 * based on similar code in JOSM. This class is not threadsafe, a separate 14 14 * instance must be created per thread. 15 * 15 * 16 16 * @author Brett Henderson 17 17 */ 18 18 public class FallbackDateParser { 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 * 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 19 20 private static final String[] formats = { 21 "yyyy-MM-dd'T'HH:mm:ss'Z'", 22 "yyyy-MM-dd'T'HH:mm:ssZ", 23 "yyyy-MM-dd'T'HH:mm:ss", 24 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", 25 "yyyy-MM-dd'T'HH:mm:ss.SSSZ", 26 "yyyy-MM-dd HH:mm:ss", 27 "MM/dd/yyyy HH:mm:ss", 28 "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'", 29 "MM/dd/yyyy'T'HH:mm:ss.SSSZ", 30 "MM/dd/yyyy'T'HH:mm:ss.SSS", 31 "MM/dd/yyyy'T'HH:mm:ssZ", 32 "MM/dd/yyyy'T'HH:mm:ss", 33 "yyyy:MM:dd HH:mm:ss" 34 }; 35 36 37 private List<DateFormat> dateParsers; 38 private int activeDateParser; 39 40 41 /** 42 * Creates a new instance. 43 */ 44 public FallbackDateParser() { 45 // Build a list of candidate date parsers. 46 dateParsers = new ArrayList<DateFormat>(formats.length); 47 for (int i = 0; i < formats.length; i++) { 48 dateParsers.add(new SimpleDateFormat(formats[i])); 49 } 50 51 // We haven't selected a date parser yet. 52 activeDateParser = -1; 53 } 54 55 56 /** 57 * Attempts to parse the specified date. 58 * 59 * @param date 60 * The date to parse. 61 * @return The date. 62 * @throws ParseException 63 * Occurs if the date does not match any of the supported date 64 * formats. 65 */ 66 public Date parse(String date) throws ParseException { 67 String correctedDate; 68 69 // Try to fix ruby's broken xmlschema - format 70 // Replace this: 71 // 2007-02-12T18:43:01+00:00 72 // With this: 73 // 2007-02-12T18:43:01+0000 74 if (date.length() == 25 && date.charAt(22) == ':') { 75 correctedDate = date.substring(0, 22) + date.substring(23, 25); 76 } else { 77 correctedDate = date; 78 } 79 80 // If we have previously successfully used a date parser, we'll try it 81 // first. 82 if (activeDateParser >= 0) { 83 try { 84 return dateParsers.get(activeDateParser).parse(correctedDate); 85 } catch (ParseException e) { 86 // The currently active parser didn't work, so we must clear it 87 // and find a new appropriate parser. 88 activeDateParser = -1; 89 } 90 } 91 92 // Try the date parsers one by one until a suitable format is found. 93 for (int i = 0; i < dateParsers.size(); i++) { 94 try { 95 Date result; 96 97 // Attempt to parse with the current parser, if successful we 98 // store its index for next time. 99 result = dateParsers.get(i).parse(correctedDate); 100 activeDateParser = i; 101 102 return result; 103 104 } catch (ParseException pe) { 105 // Ignore parsing errors and try the next pattern. 106 } 107 } 108 109 throw new ParseException("The date string (" + date + ") could not be parsed.", 0); 110 } 111 111 }
Note:
See TracChangeset
for help on using the changeset viewer.