View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2010  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  
20  package davmail.util;
21  
22  import davmail.exception.DavMailException;
23  import org.apache.log4j.Logger;
24  
25  import java.time.LocalDateTime;
26  import java.time.ZoneId;
27  import java.time.ZonedDateTime;
28  import java.time.format.DateTimeFormatter;
29  import java.time.format.DateTimeParseException;
30  import java.util.ResourceBundle;
31  import java.util.TimeZone;
32  
33  /**
34   * Date conversion methods
35   */
36  public class DateUtil {
37  
38      protected static final Logger LOGGER = Logger.getLogger("davmail.util.DateUtil");
39  
40      public static final String GRAPH_DATE_TIME = "yyyy-MM-dd'T'HH:mm:ss";
41      public static final String CALDAV_DATE_TIME = "yyyyMMdd'T'HHmmss";
42  
43      /**
44       * Standard to Exchange timezone mapping
45       */
46      public static final ResourceBundle STD_TO_EXCHANGE_TZ = ResourceBundle.getBundle("exchtimezones");
47      /**
48       * Standard to Exchange timezone mapping
49       */
50      public static final ResourceBundle EXCHANGE_TO_STD_TZ = ResourceBundle.getBundle("stdtimezones");
51  
52      public static final ResourceBundle VTIMEZONES = ResourceBundle.getBundle("vtimezones");
53  
54  
55      /**
56       * Convert timezone to Exchange timezone.
57       * @param timezoneId exchange or standard timezone id
58       * @return exchange timezone or null if unknown
59       */
60      public static String getExchangeTimeZone(String timezoneId) {
61          if (EXCHANGE_TO_STD_TZ.containsKey(timezoneId)) {
62              return timezoneId;
63          } else if (STD_TO_EXCHANGE_TZ.containsKey(timezoneId)) {
64              return STD_TO_EXCHANGE_TZ.getString(timezoneId);
65          } else if ("tzone://Microsoft/Utc".equals(timezoneId)) {
66              return "UTC";
67          } else if ("tzone://Microsoft/Custom".equals(timezoneId)) {
68              return null;
69          } else {
70              LOGGER.warn("Unknown timezone: " + timezoneId);
71              return null;
72          }
73      }
74  
75      /**
76       * Convert timezone id to standard timezone id.
77       * @param timezoneId exchange or standard timezone id
78       * @return standard timezone or null if unknown
79       */
80      public static String getStandardTimeZone(String timezoneId) {
81          if (STD_TO_EXCHANGE_TZ.containsKey(timezoneId)) {
82              return timezoneId;
83          } else if (EXCHANGE_TO_STD_TZ.containsKey(timezoneId)) {
84              return EXCHANGE_TO_STD_TZ.getString(timezoneId);
85          } else {
86              LOGGER.warn("Unknown timezone: " + timezoneId);
87              return null;
88          }
89      }
90  
91      public static TimeZone getTimeZone(String timezoneId) {
92          String standardTimeZoneId = getStandardTimeZone(timezoneId);
93          if (standardTimeZoneId != null) {
94              return TimeZone.getTimeZone(standardTimeZoneId);
95          } else {
96              LOGGER.warn("Unknown timezone: " + timezoneId+", using UTC");
97              return TimeZone.getTimeZone("UTC");
98          }
99      }
100 
101     public static String getVTimeZone(String timezoneId) {
102         String exchangeTimeZone = getExchangeTimeZone(timezoneId);
103         if (exchangeTimeZone != null && VTIMEZONES.containsKey(exchangeTimeZone)) {
104             return VTIMEZONES.getString(exchangeTimeZone);
105         } else {
106             LOGGER.warn("VTimezone not available for timezone: " + timezoneId);
107             return null;
108         }
109     }
110 
111     public static String convertDateFormat(String sourceDate, String sourceFormat, String targetFormat) throws DavMailException {
112         String targetDate = null;
113         if (sourceDate != null) {
114             try {
115                 DateTimeFormatter parser = DateTimeFormatter.ofPattern(sourceFormat);
116                 DateTimeFormatter formatter = DateTimeFormatter.ofPattern(targetFormat);
117                 LocalDateTime localDateTime = LocalDateTime.parse(sourceDate, parser);
118 
119                 targetDate = localDateTime.format(formatter);
120             } catch (DateTimeParseException e) {
121                 throw new DavMailException("EXCEPTION_INVALID_DATE", sourceDate);
122             }
123         }
124         return targetDate;
125     }
126 
127     public static String convertToUTCDateFormat(String sourceDate, String sourceTimezone, String sourceFormat, String targetFormat) throws DavMailException {
128         String targetDate = null;
129         if (sourceDate != null && sourceTimezone != null) {
130             try {
131                 DateTimeFormatter parser = DateTimeFormatter.ofPattern(sourceFormat);
132                 DateTimeFormatter formatter = DateTimeFormatter.ofPattern(targetFormat);
133                 LocalDateTime localDateTime = LocalDateTime.parse(sourceDate, parser);
134 
135                 // Convert to UTC
136                 ZonedDateTime zonedStart = localDateTime.atZone(ZoneId.of(sourceTimezone));
137                 ZonedDateTime utcStartDate = zonedStart.withZoneSameInstant(ZoneId.of("UTC"));
138                 targetDate = utcStartDate.format(formatter);
139             } catch (DateTimeParseException e) {
140                 throw new DavMailException("EXCEPTION_INVALID_DATE", sourceDate);
141             }
142         }
143         return targetDate;
144     }
145 }