001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019
020package org.apache.james.mime4j.dom.datetime;
021
022import java.util.Calendar;
023import java.util.Date;
024import java.util.GregorianCalendar;
025import java.util.TimeZone;
026
027public class DateTime {
028    private final Date date;
029    private final int year;
030    private final int month;
031    private final int day;
032    private final int hour;
033    private final int minute;
034    private final int second;
035    private final int timeZone;
036
037    public DateTime(String yearString, int month, int day, int hour, int minute, int second, int timeZone) {
038        this.year = convertToYear(yearString);
039        this.date = convertToDate(year, month, day, hour, minute, second, timeZone);
040        this.month = month;
041        this.day = day;
042        this.hour = hour;
043        this.minute = minute;
044        this.second = second;
045        this.timeZone = timeZone;
046    }
047
048    private int convertToYear(String yearString) {
049        int year = Integer.parseInt(yearString);
050        switch (yearString.length()) {
051            case 1:
052            case 2:
053                if (year >= 0 && year < 50)
054                    return 2000 + year;
055                else
056                    return 1900 + year;
057            case 3:
058                return 1900 + year;
059            default:
060                return year;
061        }
062    }
063
064    public static Date convertToDate(int year, int month, int day, int hour, int minute, int second, int timeZone) {
065        Calendar c = new GregorianCalendar(TimeZone.getTimeZone("GMT+0"));
066        c.set(year, month - 1, day, hour, minute, second);
067        c.set(Calendar.MILLISECOND, 0);
068
069        if (timeZone != Integer.MIN_VALUE) {
070            int minutes = ((timeZone / 100) * 60) + timeZone % 100;
071            c.add(Calendar.MINUTE, -1 * minutes);
072        }
073
074        return c.getTime();
075    }
076
077    public Date getDate() {
078        return date;
079    }
080
081    public int getYear() {
082        return year;
083    }
084
085    public int getMonth() {
086        return month;
087    }
088
089    public int getDay() {
090        return day;
091    }
092
093    public int getHour() {
094        return hour;
095    }
096
097    public int getMinute() {
098        return minute;
099    }
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}