2 * Copyright (c) 2017-2020. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.snooker;
19 import java.text.DateFormat;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22 import java.util.TimeZone;
25 * Date and Time Utils for Matches and Events.
27 * @author Dmitry Samoshin aka gotty
29 public final class SnookerDateUtils {
31 private static SnookerDateUtils _instance;
33 private static final String DATE_PATTERN = SnookerScoreProperties.get("snooker.date.format");
34 private static final String TIME_PATTERN = SnookerScoreProperties.get("snooker.time.format");
35 private static final String DATETIME_PATTERN = String.format("%s %s", TIME_PATTERN , DATE_PATTERN);
37 private static final TimeZone SNOOKER_TIMEZONE = TimeZone.getTimeZone(
38 SnookerScoreProperties.get("snooker.timezone")
40 private static final TimeZone LOCAL_TIMEZONE = TimeZone.getDefault();
42 private final DateFormat dateFormat;
43 private final DateFormat snookerTimeFormat;
44 private final DateFormat snookerDateTimeFormat;
45 private final DateFormat localTimeFormat;
46 private final DateFormat localDateTimeFormat;
48 private SnookerDateUtils() {
49 dateFormat = new SimpleDateFormat(DATE_PATTERN);
51 snookerTimeFormat = new SimpleDateFormat(TIME_PATTERN);
52 snookerTimeFormat.setTimeZone(SNOOKER_TIMEZONE);
54 snookerDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
55 snookerDateTimeFormat.setTimeZone(SNOOKER_TIMEZONE);
57 localTimeFormat = new SimpleDateFormat(TIME_PATTERN);
58 localTimeFormat.setTimeZone(LOCAL_TIMEZONE);
60 localDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
61 localDateTimeFormat.setTimeZone(LOCAL_TIMEZONE);
64 public static String formatDate(Date date) {
65 return getInstance().format(date);
68 public static String formatTime(Date date) {
69 return formatTime(date, false);
72 public static String formatTime(Date date, boolean withDate) {
73 return getInstance().format(date, false, withDate);
76 public static String formatLocalTime(Date date) {
77 return formatLocalTime(date, false);
80 public static String formatLocalTime(Date date, boolean withDate) {
81 return getInstance().format(date, true, withDate);
84 private String format(Date date) {
85 return dateFormat.format(date);
88 private String format(Date date, boolean isLocal, boolean withDate) {
89 return getDateFormat(isLocal, withDate).format(date);
92 private DateFormat getDateFormat(boolean isLocal, boolean withDate) {
94 return withDate ? localDateTimeFormat : localTimeFormat;
96 return withDate ? snookerDateTimeFormat : snookerTimeFormat;
99 protected static SnookerDateUtils getInstance() {
100 if (_instance == null)
101 _instance = new SnookerDateUtils();