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.commons.net.ftp.parser;
 
 import java.util.Date;
 
Default implementation of the FTPTimestampParser interface also implements the org.apache.commons.net.ftp.Configurable interface to allow the parsing to be configured from the outside.

 
 public class FTPTimestampParserImpl implements
 {
 
 
     private boolean lenientFutureDates = false;


    
The only constructor for this class.
 
     public FTPTimestampParserImpl() {
     }

    
Implements the one FTPTimestampParser.parseTimestamp(java.lang.String) in the FTPTimestampParser interface according to this algorithm: If the recentDateFormat member has been defined, try to parse the supplied string with that. If that parse fails, or if the recentDateFormat member has not been defined, attempt to parse with the defaultDateFormat member. If that fails, throw a ParseException. This method assumes that the server time is the same as the local time.

Parameters:
timestampStr The timestamp to be parsed
See also:
parseTimestamp(java.lang.String,java.util.Calendar)
 
 //    @Override
     public Calendar parseTimestamp(String timestampStrthrows ParseException {
         Calendar now = Calendar.getInstance();
         return parseTimestamp(timestampStrnow);
     }

    
If the recentDateFormat member has been defined, try to parse the supplied string with that. If that parse fails, or if the recentDateFormat member has not been defined, attempt to parse with the defaultDateFormat member. If that fails, throw a ParseException. This method allows a java.util.Calendar instance to be passed in which represents the current (system) time.

Parameters:
timestampStr The timestamp to be parsed
serverTime The current time for the server
Since:
1.5
See also:
FTPTimestampParser.parseTimestamp(java.lang.String)
 
     public Calendar parseTimestamp(String timestampStrCalendar serverTimethrows ParseException {
         Calendar working = (CalendarserverTime.clone();
         working.setTimeZone(getServerTimeZone()); // is this needed?
 
         Date parsed = null;
 
         if ( != null) {
            Calendar now = (CalendarserverTime.clone();// Copy this, because we may change it
            now.setTimeZone(this.getServerTimeZone());
            if () {
                // add a day to "now" so that "slop" doesn't cause a date
                // slightly in the future to roll back a full year.  (Bug 35181 => NET-83)
                now.add(., 1);
            }
            // The Java SimpleDateFormat class uses the epoch year 1970 if not present in the input
            // As 1970 was not a leap year, it cannot parse "Feb 29" correctly.
            // Java 1.5+ returns Mar 1 1970
            // Temporarily add the current year to the short date time
            // to cope with short-date leap year strings.
            // Since Feb 29 is more that 6 months from the end of the year, this should be OK for
            // all instances of short dates which are +- 6 months from current date.
            // TODO this won't always work for systems that use short dates +0/-12months
            // e.g. if today is Jan 1 2001 and the short date is Feb 29
            String year = Integer.toString(now.get(.));
            String timeStampStrPlusYear = timestampStr + " " + year;
            SimpleDateFormat hackFormatter = new SimpleDateFormat(.toPattern() + " yyyy",
                    .getDateFormatSymbols());
            hackFormatter.setLenient(false);
            hackFormatter.setTimeZone(.getTimeZone());
            ParsePosition pp = new ParsePosition(0);
            parsed = hackFormatter.parse(timeStampStrPlusYearpp);
            // Check if we parsed the full string, if so it must have been a short date originally
            if (parsed != null && pp.getIndex() == timeStampStrPlusYear.length()) {
                working.setTime(parsed);
                if (working.after(now)) { // must have been last year instead
                    working.add(., -1);
                }
                return working;
            }
        }
        ParsePosition pp = new ParsePosition(0);
        parsed = .parse(timestampStrpp);
        // note, length checks are mandatory for us since
        // SimpleDateFormat methods will succeed if less than
        // full string is matched.  They will also accept,
        // despite "leniency" setting, a two-digit number as
        // a valid year (e.g. 22:04 will parse as 22 A.D.)
        // so could mistakenly confuse an hour with a year,
        // if we don't insist on full length parsing.
        if (parsed != null && pp.getIndex() == timestampStr.length()) {
            working.setTime(parsed);
        } else {
            throw new ParseException(
                    "Timestamp '"+timestampStr+"' could not be parsed using a server time of "
                        +serverTime.getTime().toString(),
                    pp.getErrorIndex());
        }
        return working;
    }

    

Returns:
Returns the defaultDateFormat.
        return ;
    }
    

Returns:
Returns the defaultDateFormat pattern string.
        return .toPattern();
    }
    

Parameters:
defaultDateFormat The defaultDateFormat to be set.
    private void setDefaultDateFormat(String format) {
        if (format != null) {
            this. = new SimpleDateFormat(format);
            this..setLenient(false);
        }
    }
    

Returns:
Returns the recentDateFormat.
        return ;
    }
    

Returns:
Returns the recentDateFormat.
    public String getRecentDateFormatString() {
        return .toPattern();
    }
    

Parameters:
recentDateFormat The recentDateFormat to set.
    private void setRecentDateFormat(String format) {
        if (format != null) {
            this. = new SimpleDateFormat(format);
            this..setLenient(false);
        }
    }

    

Returns:
returns an array of 12 strings representing the short month names used by this parse.
    public String[] getShortMonths() {
    }


    

Returns:
Returns the serverTimeZone used by this parser.
    public TimeZone getServerTimeZone() {
        return this..getTimeZone();
    }
    
sets a TimeZone represented by the supplied ID string into all of the parsers used by this server.

Parameters:
serverTimeZone Time Id java.util.TimeZone id used by the ftp server. If null the client's local time zone is assumed.
    private void setServerTimeZone(String serverTimeZoneId) {
        TimeZone serverTimeZone = TimeZone.getDefault();
        if (serverTimeZoneId != null) {
            serverTimeZone = TimeZone.getTimeZone(serverTimeZoneId);
        }
        this..setTimeZone(serverTimeZone);
        if (this. != null) {
            this..setTimeZone(serverTimeZone);
        }
    }

    
Implementation of the org.apache.commons.net.ftp.Configurable interface. Configures this FTPTimestampParser according to the following logic:

Set up the org.apache.commons.net.ftp.FTPClientConfig.setDefaultDateFormatStr(java.lang.String) and optionally the org.apache.commons.net.ftp.FTPClientConfig.setRecentDateFormatStr(java.lang.String) to values supplied in the config based on month names configured as follows:

Finally if a org.apache.commons.net.ftp.FTPClientConfig.setServerTimeZoneId(java.lang.String) has been supplied via the config, set that into all date formats that have been configured.

//    @Override
    public void configure(FTPClientConfig config) {
        DateFormatSymbols dfs = null;
        String languageCode = config.getServerLanguageCode();
        String shortmonths = config.getShortMonthNames();
        if (shortmonths != null) {
            dfs = FTPClientConfig.getDateFormatSymbols(shortmonths);
        } else if (languageCode != null) {
            dfs = FTPClientConfig.lookupDateFormatSymbols(languageCode);
        } else {
            dfs = FTPClientConfig.lookupDateFormatSymbols("en");
        }
        String recentFormatString = config.getRecentDateFormatStr();
        if (recentFormatString == null) {
            this. = null;
        } else {
            this. = new SimpleDateFormat(recentFormatStringdfs);
            this..setLenient(false);
        }
        String defaultFormatString = config.getDefaultDateFormatStr();
        if (defaultFormatString == null) {
            throw new IllegalArgumentException("defaultFormatString cannot be null");
        }
        this. = new SimpleDateFormat(defaultFormatStringdfs);
        this..setLenient(false);
        setServerTimeZone(config.getServerTimeZoneId());
        this. = config.isLenientFutureDates();
    }
    

Returns:
Returns the lenientFutureDates.
    boolean isLenientFutureDates() {
        return ;
    }
    

Parameters:
lenientFutureDates The lenientFutureDates to set.
    void setLenientFutureDates(boolean lenientFutureDates) {
        this. = lenientFutureDates;
    }
New to GrepCode? Check out our FAQ X