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

Last change on this file since 8565 was 8513, checked in by Don-vip, 9 years ago

checkstyle: blocks

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