Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You 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
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * 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.
  */
 package org.apache.sling.jackrabbit.usermanager.impl.helper;
 
 import java.util.Date;
 import java.util.List;
 
 
Takes a string representation of a time-date string and tries for parse it using different formats.
 
 public class DateParser {

    
default log
 
     private static final Logger log = LoggerFactory.getLogger(DateParser.class);

    
lits of formats
 
     private final List<DateFormatformats = new LinkedList<DateFormat>();

    
Registers a format string to the list of internally checked ones. Uses the java.text.SimpleDateFormat.

Parameters:
format format as in java.text.SimpleDateFormat
Throws:
java.lang.IllegalArgumentException if the format is not valid.
 
     public void register(String format) {
         register(new SimpleDateFormat(format.));
     }

    
Registers a date format to the list of internally checked ones.

Parameters:
format date format
 
     public void register(DateFormat format) {
         .add(format);
     }

    
Parses the given source string and returns the respective calendar instance. If no format matches returns null.

Note: method is synchronized because SimpleDateFormat is not.

Parameters:
source date time source string
Returns:
calendar representation of the source or null
 
     public synchronized Calendar parse(String source) {
         for (DateFormat fmt : ) {
             try {
                 Date d = fmt.parse(source);
                 if (.isDebugEnabled()) {
                     .debug("Parsed " + source + " using " + fmt + " into "
                         + d);
                 }
                 Calendar c = Calendar.getInstance();
                 c.setTime(d);
                 return c;
             } catch (ParseException e) {
                 if (.isDebugEnabled()) {
                     .debug("Failed parsing " + source + " using " + fmt);
                 }
             }
         }
         return null;
     }

    
Parses the given source strings and returns the respective calendar instances. If no format matches for any of the sources returns null.

Note: method is synchronized because SimpleDateFormat is not.

Parameters:
sources date time source strings
Returns:
calendar representations of the source or null
    public synchronized Calendar[] parse(String sources[]) {
        Calendar ret[] = new Calendar[sources.length];
        for (int i = 0; i < sources.lengthi++) {
            if ((ret[i] = parse(sources[i])) == null) {
                return null;
            }
        }
        return ret;
    }

    
Parses the given source strings and returns the respective jcr date value instances. If no format matches for any of the sources returns null.

Note: method is synchronized because SimpleDateFormat is not.

Parameters:
sources date time source strings
factory the value factory
Returns:
jcr date value representations of the source or null
    public synchronized Value[] parse(String sources[], ValueFactory factory) {
        Value ret[] = new Value[sources.length];
        for (int i = 0; i < sources.lengthi++) {
            Calendar c = parse(sources[i]);
            if (c == null) {
                return null;
            }
            ret[i] = factory.createValue(c);
        }
        return ret;
    }
New to GrepCode? Check out our FAQ X