[LIB-8] Console output, properties, date utils
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / SnookerDateUtils.java
1 /*
2  * Copyright (c) 2017-2019. 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     public static final String DATE_PATTERN = "dd.MM.yyyy";
32     public static final String TIME_PATTERN = "HH:mm";
33     public static final String DATETIME_PATTERN = TIME_PATTERN + " " + DATE_PATTERN;
34
35     private static final TimeZone CTZ = TimeZone.getTimeZone("CET");
36     private static final TimeZone LTZ = TimeZone.getDefault();
37
38     private static final DateFormat DATE_FORMAT = new SimpleDateFormat(DATE_PATTERN);
39     private static final DateFormat CET_TIME_FORMAT = new SimpleDateFormat(TIME_PATTERN);
40     private static final DateFormat CET_DATETIME_FORMAT = new SimpleDateFormat(DATETIME_PATTERN);
41     private static final DateFormat LOCAL_TIME_FORMAT = new SimpleDateFormat(TIME_PATTERN);
42     private static final DateFormat LOCAL_DATETIME_FORMAT = new SimpleDateFormat(DATETIME_PATTERN);
43
44     static {
45         CET_TIME_FORMAT.setTimeZone(CTZ);
46         CET_DATETIME_FORMAT.setTimeZone(CTZ);
47         LOCAL_TIME_FORMAT.setTimeZone(LTZ);
48         LOCAL_DATETIME_FORMAT.setTimeZone(LTZ);
49     }
50
51     public static String formatDate(Date date) {
52         return DATE_FORMAT.format(date);
53     }
54
55     public static String formatTime(Date date) {
56         return formatTime(date, false);
57     }
58
59     public static String formatTime(Date date, boolean withDate) {
60         return withDate ? CET_DATETIME_FORMAT.format(date) : CET_TIME_FORMAT.format(date);
61     }
62
63     public static String formatLocalTime(Date date) {
64         return formatLocalTime(date, false);
65     }
66
67     public static String formatLocalTime(Date date, boolean withDate) {
68         return withDate ? LOCAL_DATETIME_FORMAT.format(date) : LOCAL_TIME_FORMAT.format(date);
69     }
70
71 }