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  
24  import java.time.LocalDateTime;
25  import java.time.ZoneId;
26  import java.time.ZonedDateTime;
27  import java.time.format.DateTimeFormatter;
28  import java.time.format.DateTimeParseException;
29  
30  /**
31   * Date related conversion methods
32   */
33  public class DateUtil {
34      public static final String GRAPH_DATE_TIME = "yyyy-MM-dd'T'HH:mm:ss";
35      public static final String CALDAV_DATE_TIME = "yyyyMMdd'T'HHmmss";
36  
37      public static String convertDateFormat(String sourceDate, String sourceFormat, String targetFormat) throws DavMailException {
38          String targetDate = null;
39          if (sourceDate != null) {
40              try {
41                  DateTimeFormatter parser = DateTimeFormatter.ofPattern(sourceFormat);
42                  DateTimeFormatter formatter = DateTimeFormatter.ofPattern(targetFormat);
43                  LocalDateTime localDateTime = LocalDateTime.parse(sourceDate, parser);
44  
45                  targetDate = localDateTime.format(formatter);
46              } catch (DateTimeParseException e) {
47                  throw new DavMailException("EXCEPTION_INVALID_DATE", sourceDate);
48              }
49          }
50          return targetDate;
51      }
52  
53      public static String convertToUTCDateFormat(String sourceDate, String sourceTimezone, String sourceFormat, String targetFormat) throws DavMailException {
54          String targetDate = null;
55          if (sourceDate != null && sourceTimezone != null) {
56              try {
57                  DateTimeFormatter parser = DateTimeFormatter.ofPattern(sourceFormat);
58                  DateTimeFormatter formatter = DateTimeFormatter.ofPattern(targetFormat);
59                  LocalDateTime localDateTime = LocalDateTime.parse(sourceDate, parser);
60  
61                  // Convert to UTC
62                  ZonedDateTime zonedStart = localDateTime.atZone(ZoneId.of(sourceTimezone));
63                  ZonedDateTime utcStartDate = zonedStart.withZoneSameInstant(ZoneId.of("UTC"));
64                  targetDate = utcStartDate.format(formatter);
65              } catch (DateTimeParseException e) {
66                  throw new DavMailException("EXCEPTION_INVALID_DATE", sourceDate);
67              }
68          }
69          return targetDate;
70      }
71  }