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.
  * ====================================================================
  *
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Apache Software Foundation.  For more
  * information on the Apache Software Foundation, please see
  * <http://www.apache.org/>.
  *
  */
 
 /*
  * Code taken from http-utils and modified for gcm-java:
  * - external dependencies removed
  * - formatting functions removed
  */
 
 package com.phonedeck.gcm4j;
 
 import java.util.Date;
 import java.util.Map;
 
A utility class for parsing and formatting HTTP dates as used in cookies and other headers. This class handles dates as defined by RFC 2616 section 3.3.1 as well as some other common non-standard formats.
 
 public final class DateUtils {

    
Date format pattern used to parse HTTP date headers in RFC 1123 format.
 
     public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";

    
Date format pattern used to parse HTTP date headers in RFC 1036 format.
 
     public static final String PATTERN_RFC1036 = "EEE, dd-MMM-yy HH:mm:ss zzz";

    
Date format pattern used to parse HTTP date headers in ANSI C asctime() format.
 
     public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
 
     private static final String[] DEFAULT_PATTERNS = new String[] {
         ,
         ,
         
     };
 
     private static final Date DEFAULT_TWO_DIGIT_YEAR_START;
 
     public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
 
     static {
         final Calendar calendar = Calendar.getInstance();
         calendar.setTimeZone();
         calendar.set(2000, ., 1, 0, 0, 0);
         calendar.set(., 0);
          = calendar.getTime();
     }

    
Parses a date value. The formats used for parsing the date value are retrieved from the default http params.

Parameters:
dateValue the date value to parse
Returns:
the parsed date or null if input could not be parsed
 
     public static Date parseDate(final String dateValue) {
         return parseDate(dateValuenullnull);
     }

    
Parses the date value using the given date formats.

Parameters:
dateValue the date value to parse
dateFormats the date formats to use
Returns:
the parsed date or null if input could not be parsed
    public static Date parseDate(final String dateValuefinal String[] dateFormats) {
        return parseDate(dateValuedateFormatsnull);
    }

    
Parses the date value using the given date formats.

Parameters:
dateValue the date value to parse
dateFormats the date formats to use
startDate During parsing, two digit years will be placed in the range startDate to startDate + 100 years. This value may be null. When null is given as a parameter, year 2000 will be used.
Returns:
the parsed date or null if input could not be parsed
    public static Date parseDate(
            final String dateValue,
            final String[] dateFormats,
            final Date startDate) {
        Preconditions.checkNotNull(dateValue"dateValue cannot be null");
        final String[] localDateFormats = dateFormats != null ? dateFormats : ;
        final Date localStartDate = startDate != null ? startDate : ;
        String v = dateValue;
        // trim single quotes around date if present
        // see issue #5279
        if (v.length() > 1 && v.startsWith("'") && v.endsWith("'")) {
            v = v.substring (1, v.length() - 1);
        }
        for (final String dateFormat : localDateFormats) {
            final SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormat);
            dateParser.set2DigitYearStart(localStartDate);
            final ParsePosition pos = new ParsePosition(0);
            final Date result = dateParser.parse(vpos);
            if (pos.getIndex() != 0) {
                return result;
            }
        }
        return null;
    }

    
Clears thread-local variable containing java.text.DateFormat cache.

Since:
4.3
    public static void clearThreadLocal() {
        DateFormatHolder.clearThreadLocal();
    }

    
This class should not be instantiated.
    private DateUtils() {
    }

    
A factory for java.text.SimpleDateFormats. The instances are stored in a threadlocal way because SimpleDateFormat is not threadsafe as noted in its javadoc.
    final static class DateFormatHolder {
        private static final ThreadLocal<SoftReference<Map<StringSimpleDateFormat>>>
            THREADLOCAL_FORMATS = new ThreadLocal<SoftReference<Map<StringSimpleDateFormat>>>() {
            @Override
            protected SoftReference<Map<StringSimpleDateFormat>> initialValue() {
                return new SoftReference<Map<StringSimpleDateFormat>>(
                        new HashMap<StringSimpleDateFormat>());
            }
        };

        
creates a java.text.SimpleDateFormat for the requested format string.

Parameters:
pattern a non-null format String according to java.text.SimpleDateFormat. The format is not checked against null since all paths go through DateUtils.
Returns:
the requested format. This simple dateformat should not be used to apply to a different pattern.
        public static SimpleDateFormat formatFor(final String pattern) {
            final SoftReference<Map<StringSimpleDateFormat>> ref = .get();
            Map<StringSimpleDateFormatformats = ref.get();
            if (formats == null) {
                formats = new HashMap<StringSimpleDateFormat>();
                .set(
                        new SoftReference<Map<StringSimpleDateFormat>>(formats));
            }
            SimpleDateFormat format = formats.get(pattern);
            if (format == null) {
                format = new SimpleDateFormat(pattern.);
                format.setTimeZone(TimeZone.getTimeZone("GMT"));
                formats.put(patternformat);
            }
            return format;
        }
        public static void clearThreadLocal() {
            .remove();
        }
    }
New to GrepCode? Check out our FAQ X