1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17 package org.apache.logging.log4j.core.util.datetime;
18
19 import java.text.ParseException;
20 import java.text.ParsePosition;
21 import java.util.Calendar;
22 import java.util.Date;
23 import java.util.Locale;
24 import java.util.TimeZone;
25
26 /**
27 * DateParser is the "missing" interface for the parsing methods of
28 * {@link java.text.DateFormat}. You can obtain an object implementing this
29 * interface by using one of the FastDateFormat factory methods.
30 * <p>
31 * Warning: Since binary compatible methods may be added to this interface in any
32 * release, developers are not expected to implement this interface.
33 *
34 * <p>
35 * Copied and modified from <a href="https://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a>.
36 * </p>
37 *
38 * @since Apache Commons Lang 3.2
39 */
40 public interface DateParser {
41
42 /**
43 * Equivalent to DateFormat.parse(String).
44 *
45 * See {@link java.text.DateFormat#parse(String)} for more information.
46 * @param source A <code>String</code> whose beginning should be parsed.
47 * @return A <code>Date</code> parsed from the string
48 * @throws ParseException if the beginning of the specified string cannot be parsed.
49 */
50 Date parse(String source) throws ParseException;
51
52 /**
53 * Equivalent to DateFormat.parse(String, ParsePosition).
54 *
55 * See {@link java.text.DateFormat#parse(String, ParsePosition)} for more information.
56 *
57 * @param source A <code>String</code>, part of which should be parsed.
58 * @param pos A <code>ParsePosition</code> object with index and error index information
59 * as described above.
60 * @return A <code>Date</code> parsed from the string. In case of error, returns null.
61 * @throws NullPointerException if text or pos is null.
62 */
63 Date parse(String source, ParsePosition pos);
64
65 /**
66 * Parses a formatted date string according to the format. Updates the Calendar with parsed fields.
67 * Upon success, the ParsePosition index is updated to indicate how much of the source text was consumed.
68 * Not all source text needs to be consumed. Upon parse failure, ParsePosition error index is updated to
69 * the offset of the source text which does not match the supplied format.
70 *
71 * @param source The text to parse.
72 * @param pos On input, the position in the source to start parsing, on output, updated position.
73 * @param calendar The calendar into which to set parsed fields.
74 * @return true, if source has been parsed (pos parsePosition is updated); otherwise false (and pos errorIndex is updated)
75 * @throws IllegalArgumentException when Calendar has been set to be not lenient, and a parsed field is
76 * out of range.
77 *
78 * @since 3.5
79 */
80 boolean parse(String source, ParsePosition pos, Calendar calendar);
81
82 // Accessors
83 //-----------------------------------------------------------------------
84 /**
85 * <p>Gets the pattern used by this parser.</p>
86 *
87 * @return the pattern, {@link java.text.SimpleDateFormat} compatible
88 */
89 String getPattern();
90
91 /**
92 * <p>
93 * Gets the time zone used by this parser.
94 * </p>
95 *
96 * <p>
97 * The default {@link TimeZone} used to create a {@link Date} when the {@link TimeZone} is not specified by
98 * the format pattern.
99 * </p>
100 *
101 * @return the time zone
102 */
103 TimeZone getTimeZone();
104
105 /**
106 * <p>Gets the locale used by this parser.</p>
107 *
108 * @return the locale
109 */
110 Locale getLocale();
111
112 /**
113 * Parses text from a string to produce a Date.
114 *
115 * @param source A <code>String</code> whose beginning should be parsed.
116 * @return a <code>java.util.Date</code> object
117 * @throws ParseException if the beginning of the specified string cannot be parsed.
118 * @see java.text.DateFormat#parseObject(String)
119 */
120 Object parseObject(String source) throws ParseException;
121
122 /**
123 * Parses a date/time string according to the given parse position.
124 *
125 * @param source A <code>String</code> whose beginning should be parsed.
126 * @param pos the parse position
127 * @return a <code>java.util.Date</code> object
128 * @see java.text.DateFormat#parseObject(String, ParsePosition)
129 */
130 Object parseObject(String source, ParsePosition pos);
131 }