aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-03-11 15:33:09 +0100
committersimonbrandhof <simon.brandhof@gmail.com>2011-03-11 16:03:30 +0100
commit7b333043e036f81970bd2a33589cab2eea2fb4e1 (patch)
tree0b97671acad4238b51a3164e8d88277c63a40dd1
parent8a8757f094b6cf9ad497839224241d289c2a2ab5 (diff)
downloadsonarqube-7b333043e036f81970bd2a33589cab2eea2fb4e1.tar.gz
sonarqube-7b333043e036f81970bd2a33589cab2eea2fb4e1.zip
API: improve DateUtils methods, but are not thread-safe
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java30
1 files changed, 17 insertions, 13 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java
index a77865c2dbf..abf8e2bfa0e 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java
@@ -30,40 +30,44 @@ public final class DateUtils {
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
+ private static final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
+ private static final SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATETIME_FORMAT);
+
/**
- * This method is not optimized. The DateFormat instance is created each time. Please use it sporadically.
+ * This method is not thread-safe.
*/
public static String formatDate(Date d) {
- return new SimpleDateFormat(DATE_FORMAT).format(d);
+ return dateFormat.format(d);
}
/**
- * This method is not optimized. The DateFormat instance is created each time. Please use it sporadically.
+ * This method is not thread-safe.
*/
public static String formatDateTime(Date d) {
- return new SimpleDateFormat(DATETIME_FORMAT).format(d);
+ return dateTimeFormat.format(d);
}
/**
- * This method is not optimized. The DateFormat instance is created each time. Please use it sporadically.
+ * This method is not thread-safe.
*/
public static Date parseDate(String s) {
- return parse(s, DATE_FORMAT);
+ try {
+ return dateFormat.parse(s);
+
+ } catch (ParseException e) {
+ throw new SonarException("The date '" + s + "' does not respect format '" + DATE_FORMAT + "'");
+ }
}
/**
- * This method is not optimized. The DateFormat instance is created each time. Please use it sporadically.
+ * This method is not thread-safe.
*/
public static Date parseDateTime(String s) {
- return parse(s, DATETIME_FORMAT);
- }
-
- private static Date parse(String s, String format) {
try {
- return new SimpleDateFormat(format).parse(s);
+ return dateTimeFormat.parse(s);
} catch (ParseException e) {
- throw new SonarException("The date '" + s + "' does not respect format '" + format + "'");
+ throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'");
}
}
}