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 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);
37 private static final TimeZone SNOOKER_TIMEZONE = TimeZone.getTimeZone("CET");
38 private static final TimeZone LOCAL_TIMEZONE = TimeZone.getDefault();
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;
46 private SnookerDateUtils() {
47 dateFormat = new SimpleDateFormat(DATE_PATTERN);
49 snookerTimeFormat = new SimpleDateFormat(TIME_PATTERN);
50 snookerTimeFormat.setTimeZone(SNOOKER_TIMEZONE);
52 snookerDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
53 snookerDateTimeFormat.setTimeZone(SNOOKER_TIMEZONE);
55 localTimeFormat = new SimpleDateFormat(TIME_PATTERN);
56 localTimeFormat.setTimeZone(LOCAL_TIMEZONE);
58 localDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN);
59 localDateTimeFormat.setTimeZone(LOCAL_TIMEZONE);
62 public static String formatDate(Date date) {
63 return getInstance().format(date);
66 public static String formatTime(Date date) {
67 return formatTime(date, false);
70 public static String formatTime(Date date, boolean withDate) {
71 return getInstance().format(date, false, withDate);
74 public static String formatLocalTime(Date date) {
75 return formatLocalTime(date, false);
78 public static String formatLocalTime(Date date, boolean withDate) {
79 return getInstance().format(date, true, withDate);
82 private String format(Date date) {
83 return dateFormat.format(date);
86 private String format(Date date, boolean isLocal, boolean withDate) {
87 return getDateFormat(isLocal, withDate).format(date);
90 private DateFormat getDateFormat(boolean isLocal, boolean withDate) {
92 return withDate ? localDateTimeFormat : localTimeFormat;
94 return withDate ? snookerDateTimeFormat : snookerTimeFormat;
97 protected static SnookerDateUtils getInstance() {
98 if (_instance == null)
99 _instance = new SnookerDateUtils();