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 DateTimeParseException if the string cannot be parsed to a local date
47 public static LocalDate parseDate(@Nullable String str, String pattern) {
49 throw new DateTimeParseException("date string is null", "<null>", 0);
51 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
52 return LocalDate.parse(str, dtf);
56 * Parses a local date and time contained in the given string, using the given pattern.
59 * the string to be parsed (can be {@code null})
61 * the pattern to be used to parse the given string
63 * @return a {@link LocalDateTime} object containing the parsed date and time
65 * @throws DateTimeParseException if the string cannot be parsed to a local date and time
67 public static LocalDateTime parseLocalTime(@Nullable String str, String pattern) {
69 throw new DateTimeParseException("time string is null", "<null>", 0);
71 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
72 return LocalDateTime.parse(str, dtf);
76 * Parses a date and time with timezone contained in the given string, using the given pattern.
79 * the string to be parsed (can be {@code null})
81 * the pattern to be used to parse the given string
83 * @return a {@link ZonedDateTime} object containing the parsed date and time with timezone
85 * @throws DateTimeParseException if the string cannot be parsed to a date and time with timezone
87 public static ZonedDateTime parseZonedTime(@Nullable String str, String pattern) {
89 throw new DateTimeParseException("zoned string is null", "<null>", 0);
91 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
92 return ZonedDateTime.parse(str, dtf);
96 * Returns a date at given days after today and at start of day in the given timezone.
99 * the number of days to be added ({@code 0} means today)
101 * the identifier of the timezone to be applied
103 * @return a {@link ZonedDateTime} object containing the date and time with timezone
105 public static ZonedDateTime getZonedStartOfDay(int days, ZoneId zoneId) {
106 return LocalDate.now().plusDays(days).atStartOfDay(zoneId);
110 * Returns a string representing the given local date and time object, using the given format pattern.
113 * the local date and time object to be formatted
115 * the format pattern to be applied
117 * @return a string representing the local date and time object
119 * @throws DateTimeException if an error occurs during printing
121 public static String format(LocalDateTime date, String pattern) {
122 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
123 return date.format(dtf);
127 * Returns a string representing the given date and time with timezone object, using the given format pattern.
130 * the date and time with timezone object to be formatted
132 * the format pattern to be applied
134 * @return a string representing the date and time with timezone object
136 * @throws DateTimeException if an error occurs during printing
138 public static String format(ZonedDateTime date, String pattern) {
139 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
140 return date.format(dtf);
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>}.
148 * the first local date and time object in range
150 * the second local date and time object in range
152 * the format pattern to be applied
154 * @return a string representing the range between the two local date and time objects
156 * @throws DateTimeException if an error occurs during printing
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));
164 * Returns a string representing the range between two date and time with timezone objects, using the given format
166 * The range itself is returned as {@code <date1>/<date2>}.
169 * the first date and time with timezone object in range
171 * the second date and time with timezone object in range
173 * the format pattern to be applied
175 * @return a string representing the range between the two date and time with timezone objects
177 * @throws DateTimeException if an error occurs during printing
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));