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
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
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
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 }