1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.util;
20
21 import java.text.DateFormat;
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25 import java.util.Locale;
26
27 import org.codehaus.plexus.util.cli.StreamConsumer;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32
33
34
35 public abstract class AbstractConsumer implements StreamConsumer {
36 protected final Logger logger = LoggerFactory.getLogger(getClass());
37
38
39
40
41
42
43
44
45
46 protected Date parseDate(String date, String userPattern, String defaultPattern) {
47 return parseDate(date, userPattern, defaultPattern, null);
48 }
49
50
51
52
53
54
55
56
57
58
59 protected Date parseDate(String date, String userPattern, String defaultPattern, Locale locale) {
60 DateFormat format;
61
62 String patternUsed = null;
63 Locale localeUsed = null;
64
65 if (userPattern != null && !userPattern.isEmpty()) {
66 if (locale != null) {
67 format = new SimpleDateFormat(userPattern, locale);
68 localeUsed = locale;
69 } else {
70 format = new SimpleDateFormat(userPattern);
71 localeUsed = Locale.getDefault();
72 }
73 patternUsed = userPattern;
74 } else {
75 if (defaultPattern != null && !defaultPattern.isEmpty()) {
76 if (locale != null) {
77 format = new SimpleDateFormat(defaultPattern, locale);
78 localeUsed = locale;
79 } else {
80 format = new SimpleDateFormat(defaultPattern);
81 localeUsed = Locale.getDefault();
82 }
83 patternUsed = defaultPattern;
84 } else {
85
86 format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
87 patternUsed = "DateFormat.SHORT";
88 localeUsed = Locale.ENGLISH;
89 }
90 }
91
92 try {
93 return format.parse(date);
94 } catch (ParseException e) {
95 if (logger.isWarnEnabled()) {
96 logger.warn(
97 "skip ParseException: " + e.getMessage() + " during parsing date '" + date
98 + "' with pattern '" + patternUsed + "' and locale '"
99 + localeUsed + "'",
100 e);
101 }
102
103 return null;
104 }
105 }
106 }