]> git.basschouten.com Git - openhab-addons.git/blob
4f1be63128cf0944903a118d1ee2822348ad688f
[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.enturno.internal.util;
14
15 import java.time.ZonedDateTime;
16 import java.time.format.DateTimeFormatter;
17 import java.time.format.DateTimeParseException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20
21 /**
22  * EnturNo date utility methods.
23  *
24  * @author Jacob Laursen - Initial contribution
25  */
26 @NonNullByDefault
27 public class DateUtil {
28     /**
29      * Converts a zoned date time string that lacks a colon in the zone to an ISO-8601 formatted string.
30      * 
31      * @param dateTimeWithoutColonInZone
32      * @return ISO-8601 formatted string
33      */
34     public static String getIsoDateTime(String dateTimeWithoutColonInZone) {
35         ZonedDateTime zonedDateTime = null;
36         try {
37             zonedDateTime = ZonedDateTime.parse(dateTimeWithoutColonInZone);
38         } catch (DateTimeParseException e) {
39             // Skip
40         }
41         try {
42             zonedDateTime = ZonedDateTime.parse(dateTimeWithoutColonInZone.replaceAll("(\\d{2})(\\d{2})$", "$1:$2"));
43         } catch (DateTimeParseException e) {
44             // Skip
45         }
46         if (zonedDateTime != null) {
47             return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(zonedDateTime);
48         }
49         return dateTimeWithoutColonInZone;
50     }
51 }