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

Last change on this file since 6962 was 6104, checked in by Don-vip, 11 years ago

see #8902 - Small performance enhancements / coding style (patch by shinigami):

  • while -> foreach
  • for -> for each

plus:

  • cleanup of FileDrop class to make it more integrated into JOSM core + remove warnings
  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.text.DateFormat;
5import java.text.ParseException;
6import java.text.SimpleDateFormat;
7import java.util.ArrayList;
8import java.util.Date;
9import java.util.List;
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 private List<DateFormat> dateParsers;
37 private int activeDateParser;
38
39 /**
40 * Creates a new instance.
41 */
42 public FallbackDateParser() {
43 // Build a list of candidate date parsers.
44 dateParsers = new ArrayList<DateFormat>(formats.length);
45 for (String format : formats) {
46 dateParsers.add(new SimpleDateFormat(format));
47 }
48
49 // We haven't selected a date parser yet.
50 activeDateParser = -1;
51 }
52
53 /**
54 * Attempts to parse the specified date.
55 *
56 * @param date
57 * The date to parse.
58 * @return The date.
59 * @throws ParseException
60 * Occurs if the date does not match any of the supported date
61 * formats.
62 */
63 public Date parse(String date) throws ParseException {
64 String correctedDate;
65
66 // Try to fix ruby's broken xmlschema - format
67 // Replace this:
68 // 2007-02-12T18:43:01+00:00
69 // With this:
70 // 2007-02-12T18:43:01+0000
71 if (date.length() == 25 && date.charAt(22) == ':') {
72 correctedDate = date.substring(0, 22) + date.substring(23, 25);
73 } else {
74 correctedDate = date;
75 }
76
77 // If we have previously successfully used a date parser, we'll try it
78 // first.
79 if (activeDateParser >= 0) {
80 try {
81 return dateParsers.get(activeDateParser).parse(correctedDate);
82 } catch (ParseException e) {
83 // The currently active parser didn't work, so we must clear it
84 // and find a new appropriate parser.
85 activeDateParser = -1;
86 }
87 }
88
89 // Try the date parsers one by one until a suitable format is found.
90 for (int i = 0; i < dateParsers.size(); i++) {
91 try {
92 Date result;
93
94 // Attempt to parse with the current parser, if successful we
95 // store its index for next time.
96 result = dateParsers.get(i).parse(correctedDate);
97 activeDateParser = i;
98
99 return result;
100
101 } catch (ParseException pe) {
102 // Ignore parsing errors and try the next pattern.
103 }
104 }
105
106 throw new ParseException("The date string (" + date + ") could not be parsed.", 0);
107 }
108}
Note: See TracBrowser for help on using the repository browser.