Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2012 the project-owners 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 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 inti.util;
 
A fast and dedicated DateFormatter, that only applies to the date formats for HTTP related stuff:

Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
 
 public class DateFormatter {
    
    
Weekdays for the RFC 850 format.
 
     private static final String[] WEEKDAY_850 = new String[] { "Sunday""Monday""Tuesday""Wednesday""Thursday",
             "Friday""Saturday" };
    
Weekdays for the RFC 1123 format.
 
     private static final String[] WEEKDAY_1123 = new String[] { "Sun""Mon""Tue""Wed""Thu""Fri""Sat" };
    
Month name formats.
 
     private static final String[] MONTH_NAME = new String[] { "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug",
             "Sep""Oct""Nov""Dec" };
    
Thread-safe calendar instances.
 
     private static final ThreadLocal<CalendarCALENDARS = new ThreadLocal<Calendar>() {
         
         @Override
         protected Calendar initialValue() {
             return Calendar.getInstance(TimeZone.getTimeZone("GMT"), .);
         }
         
     };
    
Index correction number for fast array-element access.
 
     private static final int MONTH_CORRECTION = 'A' + 'a' + 'a';
    
Array that addresses months by the first three letters.
 
     private static final int[] MONTH_INDEX = new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, .,
             ., -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, ., -1, -1, -1,
             ., -1, -1, ., -1, -1, ., -1, -1, ..,
             ., -1, -1, ., -1, ., -1, -1, -1, -1, -1, . };
    
    
Enlisting of valid date formats.
 
     public enum Format {
         RFC850, RFC1123, ANSI_C
     }
    
    
Calls parseDate(java.lang.String,java.util.Calendar).

Parameters:
date The date string to parse.
Returns:
The date as milliseconds.
 
     public long parseDate(String date) {
         return parseDate(date.get());
     }
    
    
Parses the date string using their respective first unique sign to determine the format used.

Parameters:
date The date string to parse.
cal The calendar instance to use.
Returns:
The date as milliseconds.
 
     public long parseDate(String dateCalendar cal) {
         int index = date.indexOf(','), daymonthyearminutehoursecond;
         char[] chars = date.toCharArray();
         
         day = month = year = minute = hour = second = 0;
         
         if (index == 3) {
             // RFC822 / RFC1123
            day = (chars[5] - '0') * 10 + chars[6] - '0';
            month = [chars[8] + chars[9] + chars[10] - ];
            year = (chars[12] - '0') * 1000 + (chars[13] - '0') * 100 + (chars[14] - '0') * 10 + chars[15] - '0';
            hour = (chars[17] - '0') * 10 + chars[18] - '0';
            minute = (chars[20] - '0') * 10 + chars[21] - '0';
            second = (chars[23] - '0') * 10 + chars[24] - '0';
        } else if (index == -1) {
            // ASCI_C
            day = (chars[8] >= '0' && chars[8] <= '9' ? chars[8] - '0' : 0) * 10 + chars[9] - '0';
            month = [chars[4] + chars[5] + chars[6] - ];
            year = (chars[20] - '0') * 1000 + (chars[21] - '0') * 100 + (chars[22] - '0') * 10 + chars[23] - '0';
            hour = (chars[11] - '0') * 10 + chars[12] - '0';
            minute = (chars[14] - '0') * 10 + chars[15] - '0';
            second = (chars[17] - '0') * 10 + chars[18] - '0';
        } else {
            // RFC850
            day = (chars[index + 2] - '0') * 10 + chars[index + 3] - '0';
            month = [chars[index + 5] + chars[index + 6] + chars[index + 7] - ];
            year = (chars[index + 9] - '0') * 10 + chars[index + 10] - '0';
            year += year > 50 ? 1900 : 2000;
            hour = (chars[index + 12] - '0') * 10 + chars[index + 13] - '0';
            minute = (chars[index + 15] - '0') * 10 + chars[index + 16] - '0';
            second = (chars[index + 18] - '0') * 10 + chars[index + 19] - '0';
        }
        
        cal.setTimeInMillis(0);
        cal.set(.year);
        cal.set(.month);
        cal.set(.day);
        cal.set(.hour - 1);
        cal.set(.minute);
        cal.set(.second);
        
        return cal.getTimeInMillis();
    }
    
    
Formats the specified date in DateFormatter.Format.RFC1123 - creates a new string builder and a new string.

Parameters:
date The date in milliseconds.
Returns:
The formated date.
    public String formatDate(long date) {
        StringBuilder builder = new StringBuilder();
        format1123(datebuilder.get());
        return builder.toString();
    }
    
    
Formats the specified date in DateFormatter.Format.RFC1123 - uses the string builder and creates a new string.

Parameters:
date The date in milliseconds.
builder The string builder used as buffer.
Returns:
The formated date.
    public void formatDate(long dateStringBuilder builder) {
        format1123(datebuilder.get());
    }
    
    
Formats the specified date in given format - uses the string builder and creates a new string.

Parameters:
date The date in milliseconds.
builder The string builder used as buffer.
format The format to use.
Returns:
The formated date.
    public void formatDate(long dateFormat formatStringBuilder builder) {
        formatDate(dateformatbuilder.get());
    }
    
    
Formats the specified date in given format - uses the string builder and creates a new string.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1

Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format

Parameters:
date The date in milliseconds.
builder The string builder used as buffer.
format The format to use.
cal The calendar instance.
Returns:
The formated date.
    public void formatDate(long dateFormat formatStringBuilder builderCalendar cal) {
        switch (format) {
            case :
                asci_c(datebuildercal);
                break;
            case :
                format850(datebuildercal);
                break;
            default:
            case :
                format1123(datebuildercal);
                break;
        }
    }
    
    
Formats the given date in asci_c format.
    protected void asci_c(long dateStringBuilder builderCalendar cal) {
        int tmp;
        
        cal.setTimeInMillis(date);
        
        builder.append([cal.get(.) - 1]);
        builder.append(' ');
        builder.append([cal.get(.)]);
        builder.append(' ');
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        
        builder.append(' ');
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        builder.append(':');
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        builder.append(':');
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        builder.append(' ');
        builder.append(cal.get(.));
    }
    
    
Formats the given date in rfc1123 format.
    protected void format1123(long dateStringBuilder builderCalendar cal) {
        int tmp;
        
        cal.setTimeInMillis(date);
        
        builder.append([cal.get(.) - 1]);
        builder.append(", ");
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        builder.append(' ');
        builder.append([cal.get(.)]);
        builder.append(' ');
        builder.append(cal.get(.));
        
        builder.append(' ');
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        builder.append(':');
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        builder.append(':');
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        builder.append(" GMT");
    }
    
    
Formats the given date in rfc850 format.
    protected void format850(long dateStringBuilder builderCalendar cal) {
        int tmp;
        
        cal.setTimeInMillis(date);
        
        builder.append([cal.get(.) - 1]);
        builder.append(", ");
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        builder.append('-');
        builder.append([cal.get(.)]);
        builder.append('-');
        tmp = cal.get(.) % 100;
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        
        builder.append(' ');
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        builder.append(':');
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        builder.append(':');
        tmp = cal.get(.);
        if (tmp < 10) {
            builder.append('0');
        }
        builder.append(tmp);
        builder.append(" GMT");
    }
    
New to GrepCode? Check out our FAQ X