/* * Copyright (c) 2017-2020. 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 { private static SnookerDateUtils _instance; private static final String DATE_PATTERN = SnookerScoreProperties.get("snooker.date.format"); private static final String TIME_PATTERN = SnookerScoreProperties.get("snooker.time.format"); private static final String DATETIME_PATTERN = String.format("%s %s", TIME_PATTERN , DATE_PATTERN); private static final TimeZone SNOOKER_TIMEZONE = TimeZone.getTimeZone( SnookerScoreProperties.get("snooker.timezone") ); private static final TimeZone LOCAL_TIMEZONE = TimeZone.getDefault(); private final DateFormat dateFormat; private final DateFormat snookerTimeFormat; private final DateFormat snookerDateTimeFormat; private final DateFormat localTimeFormat; private final DateFormat localDateTimeFormat; private SnookerDateUtils() { dateFormat = new SimpleDateFormat(DATE_PATTERN); snookerTimeFormat = new SimpleDateFormat(TIME_PATTERN); snookerTimeFormat.setTimeZone(SNOOKER_TIMEZONE); snookerDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN); snookerDateTimeFormat.setTimeZone(SNOOKER_TIMEZONE); localTimeFormat = new SimpleDateFormat(TIME_PATTERN); localTimeFormat.setTimeZone(LOCAL_TIMEZONE); localDateTimeFormat = new SimpleDateFormat(DATETIME_PATTERN); localDateTimeFormat.setTimeZone(LOCAL_TIMEZONE); } public static String formatDate(Date date) { return getInstance().format(date); } public static String formatTime(Date date) { return formatTime(date, false); } public static String formatTime(Date date, boolean withDate) { return getInstance().format(date, false, withDate); } public static String formatLocalTime(Date date) { return formatLocalTime(date, false); } public static String formatLocalTime(Date date, boolean withDate) { return getInstance().format(date, true, withDate); } private String format(Date date) { return dateFormat.format(date); } private String format(Date date, boolean isLocal, boolean withDate) { return getDateFormat(isLocal, withDate).format(date); } private DateFormat getDateFormat(boolean isLocal, boolean withDate) { if (isLocal) { return withDate ? localDateTimeFormat : localTimeFormat; } return withDate ? snookerDateTimeFormat : snookerTimeFormat; } protected static SnookerDateUtils getInstance() { if (_instance == null) _instance = new SnookerDateUtils(); return _instance; } }