]> git.basschouten.com Git - openhab-addons.git/blob
8dc7b2040a2c6036392e359f312f8db193652fb3
[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 DateTimeParseException if the string cannot be parsed to a local date
46      */
47     public static LocalDate parseDate(@Nullable String str, String pattern) {
48         if (str == null) {
49             throw new DateTimeParseException("date string is null", "<null>", 0);
50         }
51         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
52         return LocalDate.parse(str, dtf);
53     }
54
55     /**
56      * Parses a local date and time contained in the given string, using the given pattern.
57      *
58      * @param str
59      *            the string to be parsed (can be {@code null})
60      * @param pattern
61      *            the pattern to be used to parse the given string
62      *
63      * @return a {@link LocalDateTime} object containing the parsed date and time
64      *
65      * @throws DateTimeParseException if the string cannot be parsed to a local date and time
66      */
67     public static LocalDateTime parseLocalTime(@Nullable String str, String pattern) {
68         if (str == null) {
69             throw new DateTimeParseException("time string is null", "<null>", 0);
70         }
71         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
72         return LocalDateTime.parse(str, dtf);
73     }
74
75     /**
76      * Parses a date and time with timezone contained in the given string, using the given pattern.
77      *
78      * @param str
79      *            the string to be parsed (can be {@code null})
80      * @param pattern
81      *            the pattern to be used to parse the given string
82      *
83      * @return a {@link ZonedDateTime} object containing the parsed date and time with timezone
84      *
85      * @throws DateTimeParseException if the string cannot be parsed to a date and time with timezone
86      */
87     public static ZonedDateTime parseZonedTime(@Nullable String str, String pattern) {
88         if (str == null) {
89             throw new DateTimeParseException("zoned string is null", "<null>", 0);
90         }
91         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
92         return ZonedDateTime.parse(str, dtf);
93     }
94
95     /**
96      * Returns a date at given days after today and at start of day in the given timezone.
97      *
98      * @param days
99      *            the number of days to be added ({@code 0} means today)
100      * @param zoneId
101      *            the identifier of the timezone to be applied
102      *
103      * @return a {@link ZonedDateTime} object containing the date and time with timezone
104      */
105     public static ZonedDateTime getZonedStartOfDay(int days, ZoneId zoneId) {
106         return LocalDate.now().plusDays(days).atStartOfDay(zoneId);
107     }
108
109     /**
110      * Returns a string representing the given local date and time object, using the given format pattern.
111      *
112      * @param date
113      *            the local date and time object to be formatted
114      * @param pattern
115      *            the format pattern to be applied
116      *
117      * @return a string representing the local date and time object
118      *
119      * @throws DateTimeException if an error occurs during printing
120      */
121     public static String format(LocalDateTime date, String pattern) {
122         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
123         return date.format(dtf);
124     }
125
126     /**
127      * Returns a string representing the given date and time with timezone object, using the given format pattern.
128      *
129      * @param date
130      *            the date and time with timezone object to be formatted
131      * @param pattern
132      *            the format pattern to be applied
133      *
134      * @return a string representing the date and time with timezone object
135      *
136      * @throws DateTimeException if an error occurs during printing
137      */
138     public static String format(ZonedDateTime date, String pattern) {
139         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
140         return date.format(dtf);
141     }
142
143     /**
144      * Returns a string representing the range between two local date and time objects, using the given format pattern.
145      * The range itself is returned as {@code <date1>/<date2>}.
146      *
147      * @param date1
148      *            the first local date and time object in range
149      * @param date2
150      *            the second local date and time object in range
151      * @param pattern
152      *            the format pattern to be applied
153      *
154      * @return a string representing the range between the two local date and time objects
155      *
156      * @throws DateTimeException if an error occurs during printing
157      */
158     public static String formatRange(LocalDateTime date1, LocalDateTime date2, String pattern) {
159         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
160         return String.format(RANGE_FORMAT, date1.format(dtf), date2.format(dtf));
161     }
162
163     /**
164      * Returns a string representing the range between two date and time with timezone objects, using the given format
165      * pattern.
166      * The range itself is returned as {@code <date1>/<date2>}.
167      *
168      * @param date1
169      *            the first date and time with timezone object in range
170      * @param date2
171      *            the second date and time with timezone object in range
172      * @param pattern
173      *            the format pattern to be applied
174      *
175      * @return a string representing the range between the two date and time with timezone objects
176      *
177      * @throws DateTimeException if an error occurs during printing
178      */
179     public static String formatRange(ZonedDateTime date1, ZonedDateTime date2, String pattern) {
180         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
181         return String.format(RANGE_FORMAT, date1.format(dtf), date2.format(dtf));
182     }
183 }