[LIB-10] Сorrect some site data
[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     public static final String DATE_PATTERN = "dd.MM.yyyy";
34     public static final String TIME_PATTERN = "HH:mm";
35     public static final String DATETIME_PATTERN = String.format("%s %s", TIME_PATTERN , DATE_PATTERN);
36
37     private static final TimeZone SNOOKER_TIMEZONE = TimeZone.getTimeZone("CET");
38     private static final TimeZone LOCAL_TIMEZONE = TimeZone.getDefault();
39
40     private final DateFormat dateFormat;
41     private final DateFormat snookerTimeFormat;
42     private final DateFormat snookerDateTimeFormat;
43     private final DateFormat localTimeFormat;
44     private final DateFormat localDateTimeFormat;
45
46     private SnookerDateUtils() {
47         dateFormat = new SimpleDateFormat(DATE_PATTERN);
48
49         snookerTimeFormat = new SimpleDateFormat(TIME_PATTERN);
50         snookerTimeFormat.setTimeZone(SNOOKER_TIMEZONE);
51
52         snookerDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
53         snookerDateTimeFormat.setTimeZone(SNOOKER_TIMEZONE);
54
55         localTimeFormat = new SimpleDateFormat(TIME_PATTERN);
56         localTimeFormat.setTimeZone(LOCAL_TIMEZONE);
57
58         localDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
59         localDateTimeFormat.setTimeZone(LOCAL_TIMEZONE);
60     }
61
62     public static String formatDate(Date date) {
63         return getInstance().format(date);
64     }
65
66     public static String formatTime(Date date) {
67         return formatTime(date, false);
68     }
69
70     public static String formatTime(Date date, boolean withDate) {
71         return getInstance().format(date, false, withDate);
72     }
73
74     public static String formatLocalTime(Date date) {
75         return formatLocalTime(date, false);
76     }
77
78     public static String formatLocalTime(Date date, boolean withDate) {
79         return getInstance().format(date, true, withDate);
80     }
81
82     private String format(Date date) {
83         return dateFormat.format(date);
84     }
85
86     private String format(Date date, boolean isLocal, boolean withDate) {
87         return getDateFormat(isLocal, withDate).format(date);
88     }
89
90     private DateFormat getDateFormat(boolean isLocal, boolean withDate) {
91         if (isLocal) {
92             return withDate ? localDateTimeFormat : localTimeFormat;
93         }
94         return withDate ? snookerDateTimeFormat : snookerTimeFormat;
95     }
96
97     protected static SnookerDateUtils getInstance() {
98         if (_instance == null)
99             _instance = new SnookerDateUtils();
100         return _instance;
101     }
102
103 }