DateTimeField.java revision 31d9acbf0623872f9d4a2b3210b5970854b654c7
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;
21
22//BEGIN android-changed: Stubbing out logging
23import com.android.emailcommon.utility.Utility;
24
25import org.apache.james.mime4j.Log;
26import org.apache.james.mime4j.LogFactory;
27//END
28import org.apache.james.mime4j.field.datetime.DateTime;
29import org.apache.james.mime4j.field.datetime.parser.ParseException;
30
31import java.util.Date;
32
33public class DateTimeField extends Field {
34    private Date date;
35    private ParseException parseException;
36
37    protected DateTimeField(String name, String body, String raw, Date date, ParseException parseException) {
38        super(name, body, raw);
39        this.date = date;
40        this.parseException = parseException;
41    }
42
43    public Date getDate() {
44        return date;
45    }
46
47    public ParseException getParseException() {
48        return parseException;
49    }
50
51    public static class Parser implements FieldParser {
52        private static Log log = LogFactory.getLog(Parser.class);
53
54        public Field parse(final String name, String body, final String raw) {
55            Date date = null;
56            ParseException parseException = null;
57            //BEGIN android-changed
58            body = Utility.cleanUpMimeDate(body);
59            //END android-changed
60            try {
61                date = DateTime.parse(body).getDate();
62            }
63            catch (ParseException e) {
64                if (log.isDebugEnabled()) {
65                    log.debug("Parsing value '" + body + "': "+ e.getMessage());
66                }
67                parseException = e;
68            }
69            return new DateTimeField(name, body, raw, date, parseException);
70        }
71    }
72}
73