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