[LIB-10] Сorrect some site data
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / SnookerDateUtils.java
index aceb36a..69da93b 100644 (file)
@@ -28,28 +28,39 @@ import java.util.TimeZone;
  */
 public final class SnookerDateUtils {
 
+    private static SnookerDateUtils _instance;
+
     public static final String DATE_PATTERN = "dd.MM.yyyy";
     public static final String TIME_PATTERN = "HH:mm";
-    public static final String DATETIME_PATTERN = TIME_PATTERN + " " + DATE_PATTERN;
-
-    private static final TimeZone CTZ = TimeZone.getTimeZone("CET");
-    private static final TimeZone LTZ = TimeZone.getDefault();
-
-    private static final DateFormat DATE_FORMAT = new SimpleDateFormat(DATE_PATTERN);
-    private static final DateFormat CET_TIME_FORMAT = new SimpleDateFormat(TIME_PATTERN);
-    private static final DateFormat CET_DATETIME_FORMAT = new SimpleDateFormat(DATETIME_PATTERN);
-    private static final DateFormat LOCAL_TIME_FORMAT = new SimpleDateFormat(TIME_PATTERN);
-    private static final DateFormat LOCAL_DATETIME_FORMAT = new SimpleDateFormat(DATETIME_PATTERN);
-
-    static {
-        CET_TIME_FORMAT.setTimeZone(CTZ);
-        CET_DATETIME_FORMAT.setTimeZone(CTZ);
-        LOCAL_TIME_FORMAT.setTimeZone(LTZ);
-        LOCAL_DATETIME_FORMAT.setTimeZone(LTZ);
+    public static final String DATETIME_PATTERN = String.format("%s %s", TIME_PATTERN , DATE_PATTERN);
+
+    private static final TimeZone SNOOKER_TIMEZONE = TimeZone.getTimeZone("CET");
+    private static final TimeZone LOCAL_TIMEZONE = TimeZone.getDefault();
+
+    private final DateFormat dateFormat;
+    private final DateFormat snookerTimeFormat;
+    private final DateFormat snookerDateTimeFormat;
+    private final DateFormat localTimeFormat;
+    private final DateFormat localDateTimeFormat;
+
+    private SnookerDateUtils() {
+        dateFormat = new SimpleDateFormat(DATE_PATTERN);
+
+        snookerTimeFormat = new SimpleDateFormat(TIME_PATTERN);
+        snookerTimeFormat.setTimeZone(SNOOKER_TIMEZONE);
+
+        snookerDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
+        snookerDateTimeFormat.setTimeZone(SNOOKER_TIMEZONE);
+
+        localTimeFormat = new SimpleDateFormat(TIME_PATTERN);
+        localTimeFormat.setTimeZone(LOCAL_TIMEZONE);
+
+        localDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
+        localDateTimeFormat.setTimeZone(LOCAL_TIMEZONE);
     }
 
     public static String formatDate(Date date) {
-        return DATE_FORMAT.format(date);
+        return getInstance().format(date);
     }
 
     public static String formatTime(Date date) {
@@ -57,7 +68,7 @@ public final class SnookerDateUtils {
     }
 
     public static String formatTime(Date date, boolean withDate) {
-        return withDate ? CET_DATETIME_FORMAT.format(date) : CET_TIME_FORMAT.format(date);
+        return getInstance().format(date, false, withDate);
     }
 
     public static String formatLocalTime(Date date) {
@@ -65,7 +76,28 @@ public final class SnookerDateUtils {
     }
 
     public static String formatLocalTime(Date date, boolean withDate) {
-        return withDate ? LOCAL_DATETIME_FORMAT.format(date) : LOCAL_TIME_FORMAT.format(date);
+        return getInstance().format(date, true, withDate);
+    }
+
+    private String format(Date date) {
+        return dateFormat.format(date);
+    }
+
+    private String format(Date date, boolean isLocal, boolean withDate) {
+        return getDateFormat(isLocal, withDate).format(date);
+    }
+
+    private DateFormat getDateFormat(boolean isLocal, boolean withDate) {
+        if (isLocal) {
+            return withDate ? localDateTimeFormat : localTimeFormat;
+        }
+        return withDate ? snookerDateTimeFormat : snookerTimeFormat;
+    }
+
+    protected static SnookerDateUtils getInstance() {
+        if (_instance == null)
+            _instance = new SnookerDateUtils();
+        return _instance;
     }
 
 }