Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util;
 
 import java.util.Date;
 
 /* ------------------------------------------------------------ */
Date Format Cache. Computes String representations of Dates and caches the results so that subsequent requests within the same minute will be fast. Only format strings that contain either "ss" or "ss.SSS" are handled. The timezone of the date may be included as an ID with the "zzz" format string or as an offset with the "ZZZ" format string. If consecutive calls are frequently very different, then this may be a little slower than a normal DateFormat.
 
 
 public class DateCache  
 {
     public static String DEFAULT_FORMAT="EEE MMM dd HH:mm:ss zzz yyyy";
     private static long __hitWindow=60*60;
     
     private String _formatString;
     private String _tzFormatString;
     private SimpleDateFormat _tzFormat;
     
     private String _minFormatString;
     private SimpleDateFormat _minFormat;
 
     private String _secFormatString;
     private String _secFormatString0;
     private String _secFormatString1;
 
     private long _lastMinutes = -1;
     private long _lastSeconds = -1;
     private int _lastMs = -1;
     private String _lastResult = null;
 
     private Locale _localenull;
     private DateFormatSymbols	_dfsnull;
 
     /* ------------------------------------------------------------ */
    
Constructor. Make a DateCache that will use a default format. The default format generates the same results as Date.toString().
 
     public DateCache()
     {
         this();
         getFormat().setTimeZone(TimeZone.getDefault());
     }
     
     /* ------------------------------------------------------------ */
    
Constructor. Make a DateCache that will use the given format
 
     public DateCache(String format)
     {
         =format;
         setTimeZone(TimeZone.getDefault());
         
     }
     
     /* ------------------------------------------------------------ */
     public DateCache(String format,Locale l)
     {
         =format;
          = l;
         setTimeZone(TimeZone.getDefault());       
     }
     
    /* ------------------------------------------------------------ */
    public DateCache(String format,DateFormatSymbols s)
    {
        =format;
         = s;
        setTimeZone(TimeZone.getDefault());
    }
    /* ------------------------------------------------------------ */
    
Set the timezone.

Parameters:
tz TimeZone
    public synchronized void setTimeZone(TimeZone tz)
    {
        setTzFormatString(tz);        
        if != null ) 
        {
            =new SimpleDateFormat(,);
        }
        else if != null ) 
        {
            =new SimpleDateFormat(,);
            =new SimpleDateFormat(,);
        }
        else 
        {
            =new SimpleDateFormat();
            =new SimpleDateFormat();
        }
        .setTimeZone(tz);
        .setTimeZone(tz);
        =-1;
        =-1;        
    }
    /* ------------------------------------------------------------ */
    public TimeZone getTimeZone()
    {
        return .getTimeZone();
    }
    
    /* ------------------------------------------------------------ */
    
Set the timezone.

Parameters:
timeZoneId TimeZoneId the ID of the zone as used by TimeZone.getTimeZone(id)
    public void setTimeZoneID(String timeZoneId)
    {
        setTimeZone(TimeZone.getTimeZone(timeZoneId));
    }
    
    /* ------------------------------------------------------------ */
    private synchronized void setTzFormatString(final  TimeZone tz )
    {
        int zIndex = .indexOf"ZZZ" );
        ifzIndex >= 0 )
        {
            String ss1 = .substring( 0, zIndex );
            String ss2 = .substringzIndex+3 );
            int tzOffset = tz.getRawOffset();
            
            StringBuilder sb = new StringBuilder(.length()+10);
            sb.append(ss1);
            sb.append("'");
            iftzOffset >= 0 )
                sb.append'+' );
            else
            {
                tzOffset = -tzOffset;
                sb.append'-' );
            }
            
            int raw = tzOffset / (1000*60);		// Convert to seconds
            int hr = raw / 60;
            int min = raw % 60;
            
            ifhr < 10 )
                sb.append'0' );
            sb.appendhr );
            ifmin < 10 )
                sb.append'0' );
            sb.appendmin );
            sb.append'\'' );
            
            sb.append(ss2);
            =sb.toString();            
        }
        else
            =;
        setMinFormatString();
    }
    
    /* ------------------------------------------------------------ */
    private void setMinFormatString()
    {
        int i = .indexOf("ss.SSS");
        int l = 6;
        if (i>=0)
            throw new IllegalStateException("ms not supported");
        i = .indexOf("ss");
        l=2;
        
        // Build a formatter that formats a second format string
        String ss1=.substring(0,i);
        String ss2=.substring(i+l);
         =ss1+"'ss'"+ss2;
    }
    /* ------------------------------------------------------------ */
    
Format a date according to our stored formatter.

Parameters:
inDate
Returns:
Formatted date
    public synchronized String format(Date inDate)
    {
        return format(inDate.getTime());
    }
    
    /* ------------------------------------------------------------ */
    
Format a date according to our stored formatter.

Parameters:
inDate
Returns:
Formatted date
    public synchronized String format(long inDate)
    {
        long seconds = inDate / 1000;
        // Is it not suitable to cache?
        if (seconds< ||
            >0 && seconds>+)
        {
            // It's a cache miss
            Date d = new Date(inDate);
            return .format(d);
            
        }
                                          
        // Check if we are in the same second
        // and don't care about millis
        if (==seconds )
            return ;
        Date d = new Date(inDate);
        
        // Check if we need a new format string
        long minutes = seconds/60;
        if ( != minutes)
        {
             = minutes;
            =.format(d);
            int i=.indexOf("ss");
            int l=2;
            =.substring(0,i);
            =.substring(i+l);
        }
        // Always format if we get here
         = seconds;
        sb.append();
        int s=(int)(seconds%60);
        if (s<10)
            sb.append('0');
        sb.append(s);
        sb.append();
        =sb.toString();
                
        return ;
    }
    /* ------------------------------------------------------------ */
    
Format to string buffer.

Parameters:
inDate Date the format
buffer StringBuilder
    public void format(long inDateStringBuilder buffer)
    {
        buffer.append(format(inDate));
    }
    
    /* ------------------------------------------------------------ */
    
Get the format.
    public SimpleDateFormat getFormat()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    public String getFormatString()
    {
        return ;
    }    
    /* ------------------------------------------------------------ */
    public String now()
    {
        long now=System.currentTimeMillis();
        =(int)(now%1000);
        return format(now);
    }
    /* ------------------------------------------------------------ */
    public int lastMs()
    {
        return ;
    }
New to GrepCode? Check out our FAQ X