2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bticinosmarther.internal.util;
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;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
26 * The {@code DateUtil} class defines common date utility functions used across the whole binding.
28 * @author Fabio Possieri - Initial contribution
31 public final class DateUtil {
33 private static final String RANGE_FORMAT = "%s/%s";
36 * Parses a local date contained in the given string, using the given pattern.
39 * the string to be parsed (can be {@code null})
41 * the pattern to be used to parse the given string
43 * @return a {@link LocalDate} object containing the parsed date
45 * @throws {@link DateTimeParseException}
46 * if the string cannot be parsed to a local date
48 public static LocalDate parseDate(@Nullable String str, String pattern) {
50 throw new DateTimeParseException("date string is null", "<null>", 0);
52 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
53 return LocalDate.parse(str, dtf);
57 * Parses a local date and time contained in the given string, using the given pattern.
60 * the string to be parsed (can be {@code null})
62 * the pattern to be used to parse the given string
64 * @return a {@link LocalDateTime} object containing the parsed date and time
66 * @throws {@link DateTimeParseException}
67 * if the string cannot be parsed to a local date and time
69 public static LocalDateTime parseLocalTime(@Nullable String str, String pattern) {
71 throw new DateTimeParseException("time string is null", "<null>", 0);
73 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
74 return LocalDateTime.parse(str, dtf);
78 * Parses a date and time with timezone contained in the given string, using the given pattern.
81 * the string to be parsed (can be {@code null})
83 * the pattern to be used to parse the given string
85 * @return a {@link ZonedDateTime} object containing the parsed date and time with timezone
87 * @throws {@link DateTimeParseException}
88 * if the string cannot be parsed to a date and time with timezone
90 public static ZonedDateTime parseZonedTime(@Nullable String str, String pattern) {
92 throw new DateTimeParseException("zoned string is null", "<null>", 0);
94 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
95 return ZonedDateTime.parse(str, dtf);
99 * Returns a date at given days after today and at start of day in the given timezone.
102 * the number of days to be added ({@code 0} means today)
104 * the identifier of the timezone to be applied
106 * @return a {@link ZonedDateTime} object containing the date and time with timezone
108 public static ZonedDateTime getZonedStartOfDay(int days, ZoneId zoneId) {
109 return LocalDate.now().plusDays(days).atStartOfDay(zoneId);
113 * Returns a string representing the given local date and time object, using the given format pattern.
116 * the local date and time object to be formatted
118 * the format pattern to be applied
120 * @return a string representing the local date and time object
122 * @throws {@link DateTimeException}
123 * if an error occurs during printing
125 public static String format(LocalDateTime date, String pattern) {
126 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
127 return date.format(dtf);
131 * Returns a string representing the given date and time with timezone object, using the given format pattern.
134 * the date and time with timezone object to be formatted
136 * the format pattern to be applied
138 * @return a string representing the date and time with timezone object
140 * @throws {@link DateTimeException}
141 * if an error occurs during printing
143 public static String format(ZonedDateTime date, String pattern) {
144 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
145 return date.format(dtf);
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>}.
153 * the first local date and time object in range
155 * the second local date and time object in range
157 * the format pattern to be applied
159 * @return a string representing the range between the two local date and time objects
161 * @throws {@link DateTimeException}
162 * if an error occurs during printing
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));
170 * Returns a string representing the range between two date and time with timezone objects, using the given format
172 * The range itself is returned as {@code <date1>/<date2>}.
175 * the first date and time with timezone object in range
177 * the second date and time with timezone object in range
179 * the format pattern to be applied
181 * @return a string representing the range between the two date and time with timezone objects
183 * @throws {@link DateTimeException}
184 * if an error occurs during printing
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));