    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.lang3.time;
    018
    019import java.text.ParseException;
    020import java.text.ParsePosition;
    021import java.util.Calendar;
    022import java.util.Date;
    023import java.util.Locale;
    024import java.util.TimeZone;
    025
    026/**
    027 * DateParser is the "missing" interface for the parsing methods of
    028 * {@link java.text.DateFormat}. You can obtain an object implementing this
    029 * interface by using one of the FastDateFormat factory methods.
    030 * <p>
    031 * Warning: Since binary compatible methods may be added to this interface in any
    032 * release, developers are not expected to implement this interface.
    033 *
    034 * @since 3.2
    035 */
    036public interface DateParser {
    037
    038    /**
    039     * Equivalent to DateFormat.parse(String).
    040     *
    041     * See {@link java.text.DateFormat#parse(String)} for more information.
    042     * @param source A {@link String} whose beginning should be parsed.
    043     * @return A {@link Date} parsed from the string
    044     * @throws ParseException if the beginning of the specified string cannot be parsed.
    045     */
    046    Date parse(String source) throws ParseException;
    047
    048    /**
    049     * Equivalent to DateFormat.parse(String, ParsePosition).
    050     *
    051     * See {@link java.text.DateFormat#parse(String, ParsePosition)} for more information.
    052     *
    053     * @param source A {@link String}, part of which should be parsed.
    054     * @param pos A {@link ParsePosition} object with index and error index information
    055     * as described above.
    056     * @return A {@link Date} parsed from the string. In case of error, returns null.
    057     * @throws NullPointerException if text or pos is null.
    058     */
    059    Date parse(String source, ParsePosition pos);
    060
    061    /**
    062     * Parses a formatted date string according to the format.  Updates the Calendar with parsed fields.
    063     * Upon success, the ParsePosition index is updated to indicate how much of the source text was consumed.
    064     * Not all source text needs to be consumed.  Upon parse failure, ParsePosition error index is updated to
    065     * the offset of the source text which does not match the supplied format.
    066     *
    067     * @param source The text to parse.
    068     * @param pos On input, the position in the source to start parsing, on output, updated position.
    069     * @param calendar The calendar into which to set parsed fields.
    070     * @return true, if source has been parsed (pos parsePosition is updated); otherwise false (and pos errorIndex is updated)
    071     * @throws IllegalArgumentException when Calendar has been set to be not lenient, and a parsed field is
    072     * out of range.
    073     *
    074     * @since 3.5
    075     */
    076    boolean parse(String source, ParsePosition pos, Calendar calendar);
    077
    078    // Accessors
    079    /**
    080     * Gets the pattern used by this parser.
    081     *
    082     * @return the pattern, {@link java.text.SimpleDateFormat} compatible
    083     */
    084    String getPattern();
    085
    086    /**
    087     * Gets the time zone used by this parser.
    088     *
    089     * <p>
    090     * The default {@link TimeZone} used to create a {@link Date} when the {@link TimeZone} is not specified by
    091     * the format pattern.
    092     * </p>
    093     *
    094     * @return the time zone
    095     */
    096    TimeZone getTimeZone();
    097
    098    /**
    099     * Gets the locale used by this parser.
    100     *
    101     * @return the locale
    102     */
    103    Locale getLocale();
    104
    105    /**
    106     * Parses text from a string to produce a Date.
    107     *
    108     * @param source A {@link String} whose beginning should be parsed.
    109     * @return a {@code java.util.Date} object
    110     * @throws ParseException if the beginning of the specified string cannot be parsed.
    111     * @see java.text.DateFormat#parseObject(String)
    112     */
    113    Object parseObject(String source) throws ParseException;
    114
    115    /**
    116     * Parses a date/time string according to the given parse position.
    117     *
    118     * @param source A {@link String} whose beginning should be parsed.
    119     * @param pos the parse position
    120     * @return a {@code java.util.Date} object
    121     * @see java.text.DateFormat#parseObject(String, ParsePosition)
    122     */
    123    Object parseObject(String source, ParsePosition pos);
    124}