From 7b333043e036f81970bd2a33589cab2eea2fb4e1 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Fri, 11 Mar 2011 15:33:09 +0100 Subject: [PATCH] API: improve DateUtils methods, but are not thread-safe --- .../java/org/sonar/api/utils/DateUtils.java | 30 +++++++++++-------- 1 file 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 + "'"); } } } -- 2.39.5