]> git.basschouten.com Git - openhab-addons.git/blob
4993edc693b350a9d1b0e68a2fe3e2a94f095f03
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.bticinosmarther.internal.util;
14
15 import java.time.LocalDate;
16 import java.time.LocalDateTime;
17 import java.time.ZoneId;
18 import java.time.ZonedDateTime;
19 import java.time.format.DateTimeFormatter;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 /**
25  * The {@code DateUtil} class defines common date utility functions used across the whole binding.
26  *
27  * @author Fabio Possieri - Initial contribution
28  */
29 @NonNullByDefault
30 public final class DateUtil {
31
32     private static final String RANGE_FORMAT = "%s/%s";
33
34     /**
35      * Parses a local date contained in the given string, using the given pattern.
36      *
37      * @param str
38      *            the string to be parsed (can be {@code null})
39      * @param pattern
40      *            the pattern to be used to parse the given string
41      *
42      * @return a {@link LocalDate} object containing the parsed date
43      *
44      * @throws {@link DateTimeParseException}
45      *             if the string cannot be parsed to a local date
46      */
47     public static LocalDate parseDate(@Nullable String str, String pattern) {
48         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
49         return LocalDate.parse(str, dtf);
50     }
51
52     /**
53      * Parses a local date and time contained in the given string, using the given pattern.
54      *
55      * @param str
56      *            the string to be parsed (can be {@code null})
57      * @param pattern
58      *            the pattern to be used to parse the given string
59      *
60      * @return a {@link LocalDateTime} object containing the parsed date and time
61      *
62      * @throws {@link DateTimeParseException}
63      *             if the string cannot be parsed to a local date and time
64      */
65     public static LocalDateTime parseLocalTime(@Nullable String str, String pattern) {
66         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
67         return LocalDateTime.parse(str, dtf);
68     }
69
70     /**
71      * Parses a date and time with timezone contained in the given string, using the given pattern.
72      *
73      * @param str
74      *            the string to be parsed (can be {@code null})
75      * @param pattern
76      *            the pattern to be used to parse the given string
77      *
78      * @return a {@link ZonedDateTime} object containing the parsed date and time with timezone
79      *
80      * @throws {@link DateTimeParseException}
81      *             if the string cannot be parsed to a date and time with timezone
82      */
83     public static ZonedDateTime parseZonedTime(@Nullable String str, String pattern) {
84         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
85         return ZonedDateTime.parse(str, dtf);
86     }
87
88     /**
89      * Returns a date at given days after today and at start of day in the given timezone.
90      *
91      * @param days
92      *            the number of days to be added ({@code 0} means today)
93      * @param zoneId
94      *            the identifier of the timezone to be applied
95      *
96      * @return a {@link ZonedDateTime} object containing the date and time with timezone
97      */
98     public static ZonedDateTime getZonedStartOfDay(int days, ZoneId zoneId) {
99         return LocalDate.now().plusDays(days).atStartOfDay(zoneId);
100     }
101
102     /**
103      * Returns a string representing the given local date and time object, using the given format pattern.
104      *
105      * @param date
106      *            the local date and time object to be formatted
107      * @param pattern
108      *            the format pattern to be applied
109      *
110      * @return a string representing the local date and time object
111      *
112      * @throws {@link DateTimeException}
113      *             if an error occurs during printing
114      */
115     public static String format(LocalDateTime date, String pattern) {
116         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
117         return date.format(dtf);
118     }
119
120     /**
121      * Returns a string representing the given date and time with timezone object, using the given format pattern.
122      *
123      * @param date
124      *            the date and time with timezone object to be formatted
125      * @param pattern
126      *            the format pattern to be applied
127      *
128      * @return a string representing the date and time with timezone object
129      *
130      * @throws {@link DateTimeException}
131      *             if an error occurs during printing
132      */
133     public static String format(ZonedDateTime date, String pattern) {
134         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
135         return date.format(dtf);
136     }
137
138     /**
139      * Returns a string representing the range between two local date and time objects, using the given format pattern.
140      * The range itself is returned as {@code <date1>/<date2>}.
141      *
142      * @param date1
143      *            the first local date and time object in range
144      * @param date2
145      *            the second local date and time object in range
146      * @param pattern
147      *            the format pattern to be applied
148      *
149      * @return a string representing the range between the two local date and time objects
150      *
151      * @throws {@link DateTimeException}
152      *             if an error occurs during printing
153      */
154     public static String formatRange(LocalDateTime date1, LocalDateTime date2, String pattern) {
155         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
156         return String.format(RANGE_FORMAT, date1.format(dtf), date2.format(dtf));
157     }
158
159     /**
160      * Returns a string representing the range between two date and time with timezone objects, using the given format
161      * pattern.
162      * The range itself is returned as {@code <date1>/<date2>}.
163      *
164      * @param date1
165      *            the first date and time with timezone object in range
166      * @param date2
167      *            the second date and time with timezone object in range
168      * @param pattern
169      *            the format pattern to be applied
170      *
171      * @return a string representing the range between the two date and time with timezone objects
172      *
173      * @throws {@link DateTimeException}
174      *             if an error occurs during printing
175      */
176     public static String formatRange(ZonedDateTime date1, ZonedDateTime date2, String pattern) {
177         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
178         return String.format(RANGE_FORMAT, date1.format(dtf), date2.format(dtf));
179     }
180 }