All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.enterprise.fmt.ADateFmtJavaUtilText Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

// @(#) $Id: ADateFmtJavaUtilText.java 2093 2007-11-28 14:23:36Z s3460 $
package at.spardat.enterprise.fmt;

import java.util.*;
import java.text.*;
import at.spardat.enterprise.util.DateUtil;

/**
 * This class wraps the default behaviour of the JDK regarding formatting and parsing of 
 * Dates. The provided styles are mapped to the corresponding styles of the class
 * java.text.DateFormat.
 */
public class ADateFmtJavaUtilText extends ADateFmt {
    
    // the instance used internally to format
    private DateFormat                  dateFormat_;

   

    /**
     * Constructs an ADateFmt providing a style and a locale.
     * 
     * @param style   one of MANDATORY, SHORT, MEDIUM, LONG or FULL.
     * @param l       the locale to use
     */
    public ADateFmtJavaUtilText (int style, Locale l) {
        style_ = style;
        int          jdkStyle = DateFormat.MEDIUM;
        if ((style & SHORT) != 0) jdkStyle = DateFormat.SHORT;
        else if ((style & LONG) != 0) jdkStyle = DateFormat.LONG;
        else if ((style & FULL) != 0) jdkStyle = DateFormat.FULL;
        dateFormat_ = DateFormat.getDateInstance (jdkStyle, l);
        dateFormat_.setLenient (false);
    }

    /**
     * Constructs an ADateFmt providing a pattern as specified in 
     * java.text.SimpleDateFormat.
     * 
     * @param pattern see java.text.SimpleDateFormat
     * @param style may be MANDATORY
     */
    public ADateFmtJavaUtilText (String pattern, int style) {
        dateFormat_ = new SimpleDateFormat (pattern);
        style_ = style;
    }
    
    /**
     * Constructs an ADateFmt providing a pattern as specified in 
     * java.text.SimpleDateFormat.
     * 
     * @param pattern see java.text.SimpleDateFormat
     * @param style may be MANDATORY
     */
    public ADateFmtJavaUtilText (String pattern, int style, Locale locale) {
        dateFormat_ = new SimpleDateFormat (pattern, locale);
        style_ = style;
    }

    /**
     * @see at.spardat.enterprise.fmt.IFmt#parse(String)
     */
    public String parse (String external) throws AParseException {
        String internal = parse2 (external);
        checkMandatory(internal);
        checkDateRange(internal);
        return internal;
    }
    
    /**
     * Does the work of parse without the mandatory-check 
     */
    private String parse2 (String external) throws AParseException {
        // trim blanks on both sides
        if (external == null) return "";
        external = external.trim();
        if (external.length() == 0) return "";
        else {
            ParsePosition           pp = new ParsePosition(0);
            Date                    date = null;
            date = dateFormat_.parse(external, pp);
            if (date == null || pp.getIndex() != external.length()) {
                throw new FmtParseException ("ADateSyntax", getSampleDate());
            }
            
            // check, if the date we got is within its valid range
            GregorianCalendar        cal = new GregorianCalendar();
            cal.setTime(date);
            DateUtil.DMY             dmy = DateUtil.getDMY(cal);
            if (!DateUtil.isValid(dmy)) {
                throw new FmtParseException ("ADateSyntax", getSampleDate());
            }
            return DateUtil.DMY2Internal(dmy);
        }
    }

    /**
     * @see at.spardat.enterprise.fmt.IFmt#format(String)
     */
    public String format (String internal) {
        if (internal == null || internal.length() == 0) return "";
        return dateFormat_.format(DateUtil.internal2Gregorian(internal).getTime());
    }

    /**
     * @see at.spardat.enterprise.fmt.IFmt#maxLenOfExternal()
     */
    public int maxLenOfExternal () {
        return 50;
    }

    /**
     * @see at.spardat.enterprise.fmt.IFmt#isLegalExternalChar(char)
     */
    public boolean isLegalExternalChar (char aChar) {
        // we don't know here either
        return true; 
    }

    /**
     * @see at.spardat.enterprise.fmt.IFmt#isOneWay()
     */
    public boolean isOneWay () {
        return false;
    }
    
    /**
     * Provides a sample date for use in error-messages.
     */
    private String getSampleDate () {
        return dateFormat_.format(new GregorianCalendar().getTime());
    }    
    
    /**
     * @see at.spardat.enterprise.fmt.IFmt#clone()
     */
    public Object clone() {
        Object toReturn = super.clone();
        ((ADateFmtJavaUtilText)toReturn).dateFormat_ = (DateFormat)dateFormat_.clone();
        return toReturn;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy