source: josm/trunk/src/org/openstreetmap/josm/tools/FallbackDateParser.java@ 729

Last change on this file since 729 was 627, checked in by framm, 16 years ago
  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1package org.openstreetmap.josm.tools;
2
3import java.text.DateFormat;
4import java.text.ParseException;
5import java.text.SimpleDateFormat;
6import java.util.ArrayList;
7import java.util.Date;
8import java.util.List;
9
10
11/**
12 * Handles a number of different date formats encountered in OSM. This is built
13 * based on similar code in JOSM. This class is not threadsafe, a separate
14 * instance must be created per thread.
15 *
16 * @author Brett Henderson
17 */
18public class FallbackDateParser {
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}
Note: See TracBrowser for help on using the repository browser.