[LIB-8] Console output, properties, date utils
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / SnookerDateUtils.java
diff --git a/src/main/java/org/hedgecode/snooker/SnookerDateUtils.java b/src/main/java/org/hedgecode/snooker/SnookerDateUtils.java
new file mode 100644 (file)
index 0000000..3fde47d
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2017-2019. 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.snooker;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+/**
+ * Date and Time Utils for Matches and Events.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+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 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 String formatDate(Date date) {
+        return DATE_FORMAT.format(date);
+    }
+
+    public static String formatTime(Date date) {
+        return formatTime(date, false);
+    }
+
+    public static String formatTime(Date date, boolean withDate) {
+        return withDate ? CET_DATETIME_FORMAT.format(date) : CET_TIME_FORMAT.format(date);
+    }
+
+    public static String formatLocalTime(Date date) {
+        return formatLocalTime(date, false);
+    }
+
+    public static String formatLocalTime(Date date, boolean withDate) {
+        return withDate ? LOCAL_DATETIME_FORMAT.format(date) : LOCAL_TIME_FORMAT.format(date);
+    }
+
+}