1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
45
46 public static final ResourceBundle STD_TO_EXCHANGE_TZ = ResourceBundle.getBundle("exchtimezones");
47
48
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
57
58
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
77
78
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
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 }