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

Last change on this file since 2610 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

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