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.logging.log4j.core.util.datetime; 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 * <p> 035 * Copied and modified from <a href="https://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a>. 036 * </p> 037 * 038 * @since Apache Commons Lang 3.2 039 */ 040public interface DateParser { 041 042 /** 043 * Equivalent to DateFormat.parse(String). 044 * 045 * See {@link java.text.DateFormat#parse(String)} for more information. 046 * @param source A <code>String</code> whose beginning should be parsed. 047 * @return A <code>Date</code> parsed from the string 048 * @throws ParseException if the beginning of the specified string cannot be parsed. 049 */ 050 Date parse(String source) throws ParseException; 051 052 /** 053 * Equivalent to DateFormat.parse(String, ParsePosition). 054 * 055 * See {@link java.text.DateFormat#parse(String, ParsePosition)} for more information. 056 * 057 * @param source A <code>String</code>, part of which should be parsed. 058 * @param pos A <code>ParsePosition</code> object with index and error index information 059 * as described above. 060 * @return A <code>Date</code> parsed from the string. In case of error, returns null. 061 * @throws NullPointerException if text or pos is null. 062 */ 063 Date parse(String source, ParsePosition pos); 064 065 /** 066 * Parses a formatted date string according to the format. Updates the Calendar with parsed fields. 067 * Upon success, the ParsePosition index is updated to indicate how much of the source text was consumed. 068 * Not all source text needs to be consumed. Upon parse failure, ParsePosition error index is updated to 069 * the offset of the source text which does not match the supplied format. 070 * 071 * @param source The text to parse. 072 * @param pos On input, the position in the source to start parsing, on output, updated position. 073 * @param calendar The calendar into which to set parsed fields. 074 * @return true, if source has been parsed (pos parsePosition is updated); otherwise false (and pos errorIndex is updated) 075 * @throws IllegalArgumentException when Calendar has been set to be not lenient, and a parsed field is 076 * out of range. 077 * 078 * @since 3.5 079 */ 080 boolean parse(String source, ParsePosition pos, Calendar calendar); 081 082 // Accessors 083 //----------------------------------------------------------------------- 084 /** 085 * <p>Gets the pattern used by this parser.</p> 086 * 087 * @return the pattern, {@link java.text.SimpleDateFormat} compatible 088 */ 089 String getPattern(); 090 091 /** 092 * <p> 093 * Gets the time zone used by this parser. 094 * </p> 095 * 096 * <p> 097 * The default {@link TimeZone} used to create a {@link Date} when the {@link TimeZone} is not specified by 098 * the format pattern. 099 * </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}