2 * Copyright (c) 2010-2020 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;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
25 * The {@code DateUtil} class defines common date utility functions used across the whole binding.
27 * @author Fabio Possieri - Initial contribution
30 public final class DateUtil {
32 private static final String RANGE_FORMAT = "%s/%s";
35 * Parses a local date contained in the given string, using the given pattern.
38 * the string to be parsed (can be {@code null})
40 * the pattern to be used to parse the given string
42 * @return a {@link LocalDate} object containing the parsed date
44 * @throws {@link DateTimeParseException}
45 * if the string cannot be parsed to a local date
47 public static LocalDate parseDate(@Nullable String str, String pattern) {
48 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
49 return LocalDate.parse(str, dtf);
53 * Parses a local date and time contained in the given string, using the given pattern.
56 * the string to be parsed (can be {@code null})
58 * the pattern to be used to parse the given string
60 * @return a {@link LocalDateTime} object containing the parsed date and time
62 * @throws {@link DateTimeParseException}
63 * if the string cannot be parsed to a local date and time
65 public static LocalDateTime parseLocalTime(@Nullable String str, String pattern) {
66 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
67 return LocalDateTime.parse(str, dtf);
71 * Parses a date and time with timezone contained in the given string, using the given pattern.
74 * the string to be parsed (can be {@code null})
76 * the pattern to be used to parse the given string
78 * @return a {@link ZonedDateTime} object containing the parsed date and time with timezone
80 * @throws {@link DateTimeParseException}
81 * if the string cannot be parsed to a date and time with timezone
83 public static ZonedDateTime parseZonedTime(@Nullable String str, String pattern) {
84 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
85 return ZonedDateTime.parse(str, dtf);
89 * Returns a date at given days after today and at start of day in the given timezone.
92 * the number of days to be added ({@code 0} means today)
94 * the identifier of the timezone to be applied
96 * @return a {@link ZonedDateTime} object containing the date and time with timezone
98 public static ZonedDateTime getZonedStartOfDay(int days, ZoneId zoneId) {
99 return LocalDate.now().plusDays(days).atStartOfDay(zoneId);
103 * Returns a string representing the given local date and time object, using the given format pattern.
106 * the local date and time object to be formatted
108 * the format pattern to be applied
110 * @return a string representing the local date and time object
112 * @throws {@link DateTimeException}
113 * if an error occurs during printing
115 public static String format(LocalDateTime date, String pattern) {
116 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
117 return date.format(dtf);
121 * Returns a string representing the given date and time with timezone object, using the given format pattern.
124 * the date and time with timezone object to be formatted
126 * the format pattern to be applied
128 * @return a string representing the date and time with timezone object
130 * @throws {@link DateTimeException}
131 * if an error occurs during printing
133 public static String format(ZonedDateTime date, String pattern) {
134 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
135 return date.format(dtf);
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>}.
143 * the first local date and time object in range
145 * the second local date and time object in range
147 * the format pattern to be applied
149 * @return a string representing the range between the two local date and time objects
151 * @throws {@link DateTimeException}
152 * if an error occurs during printing
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));
160 * Returns a string representing the range between two date and time with timezone objects, using the given format
162 * The range itself is returned as {@code <date1>/<date2>}.
165 * the first date and time with timezone object in range
167 * the second date and time with timezone object in range
169 * the format pattern to be applied
171 * @return a string representing the range between the two date and time with timezone objects
173 * @throws {@link DateTimeException}
174 * if an error occurs during printing
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));