View Javadoc
1   package fr.ifremer.quadrige3.core.dao.technical;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import java.sql.Timestamp;
31  import java.time.*;
32  import java.time.format.DateTimeFormatter;
33  import java.time.format.DateTimeParseException;
34  import java.util.Date;
35  import java.util.TimeZone;
36  
37  /**
38   * Utilitaire sur les heures.
39   */
40  public class Times {
41  
42      /**
43       * Constant <code>FILE_SUFFIX_DATE_FORMAT</code>
44       */
45      private static final DateTimeFormatter FILE_SUFFIX_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd-HHmmss");
46  
47      /**
48       * Logger.
49       */
50      private static final Log LOG = LogFactory.getLog(Times.class);
51  
52      /**
53       * Hour format.
54       */
55      private static final DateTimeFormatter HOUR_MINUTE_FORMAT = DateTimeFormatter.ofPattern("HH:mm");
56  
57      /**
58       * Conversion d une heure (en seconde) en String hh:mm.
59       *
60       * @param seconds L heure
61       * @return String
62       */
63      public static String secondsToString(final Integer seconds) {
64          if (seconds == null) return null; // or empty string ?
65          return LocalTime.ofSecondOfDay(seconds).format(HOUR_MINUTE_FORMAT);
66      }
67  
68      /**
69       * Convertir une heure hh:mm en secondes.
70       *
71       * @param string String
72       * @return Heure en seconde
73       */
74      public static Integer stringToSeconds(final String string) throws DateTimeParseException {
75          if (StringUtils.isBlank(string)) return null;
76          return LocalTime.parse(string, HOUR_MINUTE_FORMAT).toSecondOfDay();
77      }
78  
79      /**
80       * Convertir une heure en date.
81       *
82       * @param seconds L heure a convertir
83       * @return La date
84       */
85      public static Date secondsToDate(final Integer seconds) {
86          if (seconds == null) return null;
87          return Dates.convertToDate(LocalDate.now().atTime(LocalTime.ofSecondOfDay(seconds)), TimeZone.getTimeZone(ZoneOffset.UTC));
88      }
89  
90      /**
91       * Convert date into hour (second).
92       *
93       * @param date Date
94       * @return Hour in seconds
95       */
96      public static Integer dateToSeconds(final Date date) {
97          if (date == null) return null;
98          return Dates.convertToLocalDateTime(date, TimeZone.getTimeZone(ZoneOffset.UTC)).toLocalTime().toSecondOfDay();
99      }
100 
101     /**
102      * Human readable format of a period of time
103      *
104      * @param millis period of time in millisecond
105      * @return pretty print
106      */
107     public static String durationToString(long millis) {
108         return Duration.ofMillis(millis).toString()
109                 .substring(2)
110                 .replaceAll("(\\d[HMS])(?!$)", "$1 ")
111                 .toLowerCase();
112     }
113 
114     /**
115      * <p>
116      * getTimestampOrNull.
117      * </p>
118      *
119      * @param date a {@link java.util.Date} object.
120      * @return a {@link java.sql.Timestamp} object.
121      */
122     public static Timestamp getTimestampOrNull(Date date) {
123         return date == null ? null : new Timestamp(date.getTime());
124     }
125 
126     public static String getFileSuffix() {
127         return getFileSuffix(LocalDateTime.now());
128     }
129 
130     public static String getFileSuffix(LocalDateTime localDateTime) {
131         return localDateTime.format(FILE_SUFFIX_DATE_FORMAT);
132     }
133 }