[LIB-10] Date and Timezone format constants from snooker.properties
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / SnookerDateUtils.java
index 3fde47d..126ab5a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2019. Developed by Hedgecode.
+ * Copyright (c) 2017-2020. Developed by Hedgecode.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -28,28 +28,41 @@ import java.util.TimeZone;
  */
 public final class SnookerDateUtils {
 
-    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 SnookerDateUtils _instance;
 
-    private static final TimeZone CTZ = TimeZone.getTimeZone("CET");
-    private static final TimeZone LTZ = TimeZone.getDefault();
+    private static final String DATE_PATTERN = SnookerScoreProperties.get("snooker.date.format");
+    private static final String TIME_PATTERN = SnookerScoreProperties.get("snooker.time.format");
+    private static final String DATETIME_PATTERN = String.format("%s %s", TIME_PATTERN , DATE_PATTERN);
 
-    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);
+    private static final TimeZone SNOOKER_TIMEZONE = TimeZone.getTimeZone(
+            SnookerScoreProperties.get("snooker.timezone")
+    );
+    private static final TimeZone LOCAL_TIMEZONE = TimeZone.getDefault();
 
-    static {
-        CET_TIME_FORMAT.setTimeZone(CTZ);
-        CET_DATETIME_FORMAT.setTimeZone(CTZ);
-        LOCAL_TIME_FORMAT.setTimeZone(LTZ);
-        LOCAL_DATETIME_FORMAT.setTimeZone(LTZ);
+    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 +70,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 +78,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;
     }
 
 }