X-Git-Url: https://git.hedgecode.org/?p=snooker-score-api.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fsnooker%2FSnookerDateUtils.java;h=69da93bc0922863af208f04e26dba038d8a71084;hp=aceb36a693ec7fc6b3e4d1e8d45bb76d99655d14;hb=70af784f3d0752e97b68f77b34ab9bd4dcd1fb23;hpb=3ca0166cfef8d8b0bb9d57e471a2f6e994d6160c diff --git a/src/main/java/org/hedgecode/snooker/SnookerDateUtils.java b/src/main/java/org/hedgecode/snooker/SnookerDateUtils.java index aceb36a..69da93b 100644 --- a/src/main/java/org/hedgecode/snooker/SnookerDateUtils.java +++ b/src/main/java/org/hedgecode/snooker/SnookerDateUtils.java @@ -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; } }