1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.mime4j.field.datetime;
21
22 import java.util.Calendar;
23 import java.util.Date;
24 import java.util.GregorianCalendar;
25 import java.util.TimeZone;
26
27 public class DateTime {
28 private final Date date;
29 private final int year;
30 private final int month;
31 private final int day;
32 private final int hour;
33 private final int minute;
34 private final int second;
35 private final int timeZone;
36
37 public DateTime(String yearString, int month, int day, int hour, int minute, int second, int timeZone) {
38 this.year = convertToYear(yearString);
39 this.date = convertToDate(year, month, day, hour, minute, second, timeZone);
40 this.month = month;
41 this.day = day;
42 this.hour = hour;
43 this.minute = minute;
44 this.second = second;
45 this.timeZone = timeZone;
46 }
47
48 private int convertToYear(String yearString) {
49 int year = Integer.parseInt(yearString);
50 switch (yearString.length()) {
51 case 1:
52 case 2:
53 if (year >= 0 && year < 50)
54 return 2000 + year;
55 else
56 return 1900 + year;
57 case 3:
58 return 1900 + year;
59 default:
60 return year;
61 }
62 }
63
64 public static Date convertToDate(int year, int month, int day, int hour, int minute, int second, int timeZone) {
65 Calendar c = new GregorianCalendar(TimeZone.getTimeZone("GMT+0"));
66 c.set(year, month - 1, day, hour, minute, second);
67 c.set(Calendar.MILLISECOND, 0);
68
69 if (timeZone != Integer.MIN_VALUE) {
70 int minutes = ((timeZone / 100) * 60) + timeZone % 100;
71 c.add(Calendar.MINUTE, -1 * minutes);
72 }
73
74 return c.getTime();
75 }
76
77 public Date getDate() {
78 return date;
79 }
80
81 public int getYear() {
82 return year;
83 }
84
85 public int getMonth() {
86 return month;
87 }
88
89 public int getDay() {
90 return day;
91 }
92
93 public int getHour() {
94 return hour;
95 }
96
97 public int getMinute() {
98 return minute;
99 }
100
101 public int getSecond() {
102 return second;
103 }
104
105 public int getTimeZone() {
106 return timeZone;
107 }
108
109 public void print() {
110 System.out.println(toString());
111 }
112
113 @Override
114 public String toString() {
115 return getYear() + " " + getMonth() + " " + getDay() + "; " + getHour() + " " + getMinute() + " " + getSecond() + " " + getTimeZone();
116 }
117
118 @Override
119 public int hashCode() {
120 final int PRIME = 31;
121 int result = 1;
122 result = PRIME * result + ((date == null) ? 0 : date.hashCode());
123 result = PRIME * result + day;
124 result = PRIME * result + hour;
125 result = PRIME * result + minute;
126 result = PRIME * result + month;
127 result = PRIME * result + second;
128 result = PRIME * result + timeZone;
129 result = PRIME * result + year;
130 return result;
131 }
132
133 @Override
134 public boolean equals(Object obj) {
135 if (this == obj)
136 return true;
137 if (obj == null)
138 return false;
139 if (getClass() != obj.getClass())
140 return false;
141 final DateTime other = (DateTime) obj;
142 if (date == null) {
143 if (other.date != null)
144 return false;
145 } else if (!date.equals(other.date))
146 return false;
147 if (day != other.day)
148 return false;
149 if (hour != other.hour)
150 return false;
151 if (minute != other.minute)
152 return false;
153 if (month != other.month)
154 return false;
155 if (second != other.second)
156 return false;
157 if (timeZone != other.timeZone)
158 return false;
159 if (year != other.year)
160 return false;
161 return true;
162 }
163
164
165 }