View Javadoc
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    *      https://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.commons.lang3.time;
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   * </p>
34   *
35   * @since 3.2
36   */
37  public interface DateParser {
38  
39      /**
40       * Gets the locale used by this parser.
41       *
42       * @return the locale
43       */
44      Locale getLocale();
45  
46      /**
47       * Gets the pattern used by this parser.
48       *
49       * @return the pattern, {@link java.text.SimpleDateFormat} compatible.
50       */
51      String getPattern();
52  
53      /**
54       * Gets the time zone used by this parser.
55       *
56       * <p>
57       * The default {@link TimeZone} used to create a {@link Date} when the {@link TimeZone} is not specified by
58       * the format pattern.
59       * </p>
60       *
61       * @return the time zone
62       */
63      TimeZone getTimeZone();
64  
65      /**
66       * Equivalent to DateFormat.parse(String).
67       *
68       * See {@link java.text.DateFormat#parse(String)} for more information.
69       *
70       * @param source A {@link String} whose beginning should be parsed.
71       * @return A {@link Date} parsed from the string.
72       * @throws ParseException if the beginning of the specified string cannot be parsed.
73       */
74      Date parse(String source) throws ParseException;
75  
76      /**
77       * Equivalent to DateFormat.parse(String, ParsePosition).
78       *
79       * See {@link java.text.DateFormat#parse(String, ParsePosition)} for more information.
80       *
81       * @param source A {@link String}, part of which should be parsed.
82       * @param pos A {@link ParsePosition} object with index and error index information
83       * as described above.
84       * @return A {@link Date} parsed from the string. In case of error, returns null.
85       * @throws NullPointerException if text or pos is null.
86       */
87      Date parse(String source, ParsePosition pos);
88  
89      /**
90       * Parses a formatted date string according to the format.  Updates the Calendar with parsed fields.
91       * Upon success, the ParsePosition index is updated to indicate how much of the source text was consumed.
92       * Not all source text needs to be consumed.  Upon parse failure, ParsePosition error index is updated to
93       * the offset of the source text which does not match the supplied format.
94       *
95       * @param source The text to parse.
96       * @param pos On input, the position in the source to start parsing, on output, updated position.
97       * @param calendar The calendar into which to set parsed fields.
98       * @return true, if source has been parsed (pos parsePosition is updated); otherwise false (and pos errorIndex is updated)
99       * @throws IllegalArgumentException when Calendar has been set to be not lenient, and a parsed field is
100      * out of range.
101      *
102      * @since 3.5
103      */
104     boolean parse(String source, ParsePosition pos, Calendar calendar);
105 
106     /**
107      * Parses text from a string to produce a Date.
108      *
109      * @param source A {@link String} whose beginning should be parsed.
110      * @return a {@link java.util.Date} object.
111      * @throws ParseException if the beginning of the specified string cannot be parsed.
112      * @see java.text.DateFormat#parseObject(String)
113      */
114     Object parseObject(String source) throws ParseException;
115 
116     /**
117      * Parses a date/time string according to the given parse position.
118      *
119      * @param source A {@link String} whose beginning should be parsed.
120      * @param pos the parse position.
121      * @return a {@link java.util.Date} object.
122      * @see java.text.DateFormat#parseObject(String, ParsePosition)
123      */
124     Object parseObject(String source, ParsePosition pos);
125 }