[LIB-10] Date and Timezone format constants from snooker.properties
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / SnookerDateUtils.java
1 /*
2  * Copyright (c) 2017-2020. Developed by Hedgecode.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.snooker;
18
19 import java.text.DateFormat;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22 import java.util.TimeZone;
23
24 /**
25  * Date and Time Utils for Matches and Events.
26  *
27  * @author Dmitry Samoshin aka gotty
28  */
29 public final class SnookerDateUtils {
30
31     private static SnookerDateUtils _instance;
32
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);
36
37     private static final TimeZone SNOOKER_TIMEZONE = TimeZone.getTimeZone(
38             SnookerScoreProperties.get("snooker.timezone")
39     );
40     private static final TimeZone LOCAL_TIMEZONE = TimeZone.getDefault();
41
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;
47
48     private SnookerDateUtils() {
49         dateFormat = new SimpleDateFormat(DATE_PATTERN);
50
51         snookerTimeFormat = new SimpleDateFormat(TIME_PATTERN);
52         snookerTimeFormat.setTimeZone(SNOOKER_TIMEZONE);
53
54         snookerDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
55         snookerDateTimeFormat.setTimeZone(SNOOKER_TIMEZONE);
56
57         localTimeFormat = new SimpleDateFormat(TIME_PATTERN);
58         localTimeFormat.setTimeZone(LOCAL_TIMEZONE);
59
60         localDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
61         localDateTimeFormat.setTimeZone(LOCAL_TIMEZONE);
62     }
63
64     public static String formatDate(Date date) {
65         return getInstance().format(date);
66     }
67
68     public static String formatTime(Date date) {
69         return formatTime(date, false);
70     }
71
72     public static String formatTime(Date date, boolean withDate) {
73         return getInstance().format(date, false, withDate);
74     }
75
76     public static String formatLocalTime(Date date) {
77         return formatLocalTime(date, false);
78     }
79
80     public static String formatLocalTime(Date date, boolean withDate) {
81         return getInstance().format(date, true, withDate);
82     }
83
84     private String format(Date date) {
85         return dateFormat.format(date);
86     }
87
88     private String format(Date date, boolean isLocal, boolean withDate) {
89         return getDateFormat(isLocal, withDate).format(date);
90     }
91
92     private DateFormat getDateFormat(boolean isLocal, boolean withDate) {
93         if (isLocal) {
94             return withDate ? localDateTimeFormat : localTimeFormat;
95         }
96         return withDate ? snookerDateTimeFormat : snookerTimeFormat;
97     }
98
99     protected static SnookerDateUtils getInstance() {
100         if (_instance == null)
101             _instance = new SnookerDateUtils();
102         return _instance;
103     }
104
105 }