]> git.basschouten.com Git - openhab-addons.git/blob
f366941686aab53aede9a733f9515071cb118119
[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.luftdateninfo.internal.utils;
14
15 import java.time.LocalDateTime;
16 import java.time.format.DateTimeFormatter;
17 import java.time.format.DateTimeParseException;
18 import java.util.Locale;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The {@link DateTimeUtils} class provides helpers for converting Dates and Times.
27  *
28  * @author Bernd Weymann - Initial contribution
29  */
30 @NonNullByDefault
31 public class DateTimeUtils {
32     public static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
33     private static final Logger LOGGER = LoggerFactory.getLogger(DateTimeUtils.class);
34
35     public static synchronized @Nullable LocalDateTime toDate(String dateTime) {
36         try {
37             return LocalDateTime.from(DTF.parse(dateTime));
38
39         } catch (DateTimeParseException e) {
40             LOGGER.debug("Unable to parse date {}", dateTime);
41             return null;
42         }
43     }
44 }