1/****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one   *
3 * or more contributor license agreements.  See the NOTICE file *
4 * distributed with this work for additional information        *
5 * regarding copyright ownership.  The ASF licenses this file   *
6 * to you under the Apache License, Version 2.0 (the            *
7 * "License"); you may not use this file except in compliance   *
8 * with the License.  You may obtain a copy of the License at   *
9 *                                                              *
10 *   http://www.apache.org/licenses/LICENSE-2.0                 *
11 *                                                              *
12 * Unless required by applicable law or agreed to in writing,   *
13 * software distributed under the License is distributed on an  *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 * KIND, either express or implied.  See the License for the    *
16 * specific language governing permissions and limitations      *
17 * under the License.                                           *
18 ****************************************************************/
19
20package org.apache.james.mime4j.field.datetime;
21
22import org.apache.james.mime4j.field.datetime.parser.DateTimeParser;
23import org.apache.james.mime4j.field.datetime.parser.ParseException;
24import org.apache.james.mime4j.field.datetime.parser.TokenMgrError;
25
26import java.util.Date;
27import java.util.Calendar;
28import java.util.TimeZone;
29import java.util.GregorianCalendar;
30import java.io.StringReader;
31
32public class DateTime {
33    private final Date date;
34    private final int year;
35    private final int month;
36    private final int day;
37    private final int hour;
38    private final int minute;
39    private final int second;
40    private final int timeZone;
41
42    public DateTime(String yearString, int month, int day, int hour, int minute, int second, int timeZone) {
43        this.year = convertToYear(yearString);
44        this.date = convertToDate(year, month, day, hour, minute, second, timeZone);
45        this.month = month;
46        this.day = day;
47        this.hour = hour;
48        this.minute = minute;
49        this.second = second;
50        this.timeZone = timeZone;
51    }
52
53    private int convertToYear(String yearString) {
54        int year = Integer.parseInt(yearString);
55        switch (yearString.length()) {
56            case 1:
57            case 2:
58                if (year >= 0 && year < 50)
59                    return 2000 + year;
60                else
61                    return 1900 + year;
62            case 3:
63                return 1900 + year;
64            default:
65                return year;
66        }
67    }
68
69    public static Date convertToDate(int year, int month, int day, int hour, int minute, int second, int timeZone) {
70        Calendar c = new GregorianCalendar(TimeZone.getTimeZone("GMT+0"));
71        c.set(year, month - 1, day, hour, minute, second);
72        c.set(Calendar.MILLISECOND, 0);
73
74        if (timeZone != Integer.MIN_VALUE) {
75            int minutes = ((timeZone / 100) * 60) + timeZone % 100;
76            c.add(Calendar.MINUTE, -1 * minutes);
77        }
78
79        return c.getTime();
80    }
81
82    public Date getDate() {
83        return date;
84    }
85
86    public int getYear() {
87        return year;
88    }
89
90    public int getMonth() {
91        return month;
92    }
93
94    public int getDay() {
95        return day;
96    }
97
98    public int getHour() {
99        return hour;
100    }
101
102    public int getMinute() {
103        return minute;
104    }
105
106    public int getSecond() {
107        return second;
108    }
109
110    public int getTimeZone() {
111        return timeZone;
112    }
113
114    public void print() {
115        System.out.println(getYear() + " " + getMonth() + " " + getDay() + "; " + getHour() + " " + getMinute() + " " + getSecond() + " " + getTimeZone());
116    }
117
118
119    public static DateTime parse(String dateString) throws ParseException {
120        try {
121            return new DateTimeParser(new StringReader(dateString)).parseAll();
122        }
123        catch (TokenMgrError err) {
124            throw new ParseException(err.getMessage());
125        }
126    }
127}
128