View Javadoc
1   package fr.ifremer.quadrige2.core.dao.technical;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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  import org.joda.time.DateTimeConstants;
30  import org.joda.time.LocalTime;
31  import org.joda.time.chrono.ISOChronology;
32  import org.joda.time.format.DateTimeFormat;
33  import org.joda.time.format.DateTimeFormatter;
34  
35  import java.util.Date;
36  
37  /**
38   * Utilitaire sur les heures.
39   */
40  public class Times {
41  
42      /**
43       * Constant <code>FILE_SUFFIX_DATE_FORMAT</code>
44       */
45      public static final DateTimeFormatter FILE_SUFFIX_DATE_FORMAT = DateTimeFormat.forPattern("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 = DateTimeFormat.forPattern("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 Double seconds) {
64          if (seconds == null) {
65              return null; // or empty string ?
66          }
67  
68          return toLocalTime(seconds).toString(HOUR_MINUTE_FORMAT);
69      }
70  
71      private static LocalTime toLocalTime(Double seconds) {
72          return new LocalTime(seconds.intValue() * DateTimeConstants.MILLIS_PER_SECOND, ISOChronology.getInstanceUTC());
73      }
74  
75      /**
76       * Convertir une heure hh:mm en secondes.
77       *
78       * @param string String
79       * @return Heure en seconde
80       */
81      public static Double stringToSeconds(final String string) {
82          if (StringUtils.isBlank(string)) {
83              return null;
84          }
85          try {
86              return toSeconds(LocalTime.parse(string, HOUR_MINUTE_FORMAT));
87          } catch (final UnsupportedOperationException e) {
88              LOG.error("Error parsing hour", e);
89          }
90          return null;
91      }
92  
93      private static Double toSeconds(LocalTime localTime) {
94          Integer seconds = localTime.getHourOfDay() * DateTimeConstants.SECONDS_PER_HOUR + localTime.getMinuteOfHour() * DateTimeConstants.SECONDS_PER_MINUTE;
95          return seconds.doubleValue();
96      }
97  
98      /**
99       * Convertir une heure en date.
100      *
101      * @param seconds L heure a convertir
102      * @return La date
103      */
104     public static Date secondsToDate(final Double seconds) {
105         if (seconds != null) {
106             return toLocalTime(seconds).toDateTimeToday().toDate();
107         }
108         return null;
109     }
110 
111     /**
112      * Convert date into hour (second).
113      *
114      * @param date Date
115      * @return Hour in seconds
116      */
117     public static Double dateToSeconds(final Date date) {
118         if (date != null) {
119             return toSeconds(LocalTime.fromDateFields(date));
120         }
121         return null;
122     }
123 }