Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Abstract implementation of a DateFormatConverter.
 public abstract class AbstractDateFormatConverter implements DateFormatConverter {
     public String convert(String javaDateFormat) {
         StringBuilder str = new StringBuilder(javaDateFormat.length() * 2);
         for (String token : DateFormatTokenizer.tokenize(javaDateFormat)) {
             if (!hasMappedToken(token)) {
             } else {
                 String mapped = getMappedToken(token);
                 if (mapped == null) {
                 } else {
         if (isConvertingEscapes()) {
         return str.toString();

Called by convert(java.lang.String) when hasMappedToken(java.lang.String) returns true, but the return value of getMappedToken(java.lang.String) is null, meaning that the given token can not be mapped. Throws an UnsupportedOperationException by default.

token the token for which no mapping exists
     protected void onNullMappedToken(String token) {
         throw new UnsupportedOperationException("Can not map token " + token);

Returns true if this DateFormatConverter has special escape characters that should be converted also. In that case, the convertEscapes(java.lang.StringBuilder) method is called. Returns false by default, can be overridden by sub-classes.

true if this DateFormatConverter should convert escape characters
     protected boolean isConvertingEscapes() {
         return false;

SimpleDateFormat uses the single quote ' character to escape character sequences, where as other formats may use different quote block characters. This method converts these different escape characters.

str the StringBuilder containing the string to convert
     protected void convertEscapes(StringBuilder str) {
         int o = -1; // index where the current quote block opened
         for (int i = 0; i < str.length(); i++) {
             if (str.charAt(i) == '\'') {
                 if (o == -1) {
                     o = i;
                 } else {
                     // found a closing quote
                     o = -1;

Hook to convert an escape block.

o the index of the quote beginning a quoted block
c the index of the quote closing a quoted block
str the string builder containing the pattern
     protected void convertEscape(int oint cStringBuilder str) {
         // hook

Returns the pattern token the given pattern token is converted to.

token the SimpleDateFormat token, e.g. 'YYYY'
the mapped token
    public abstract String getMappedToken(String token);

Checks whether the given token is a mapped token.

token the token returned from DateFormatTokenizer (e.g. 'YYYY' or '-' or '.')
true if a mapping exists
    public abstract boolean hasMappedToken(String token);
New to GrepCode? Check out our FAQ X