no.abmu.questionnaire.propertyeditors.DateEditor.java Source code

Java tutorial

Introduction

Here is the source code for no.abmu.questionnaire.propertyeditors.DateEditor.java

Source

/*$Id: DateEditor.java 15543 2010-04-20 15:53:14Z jens $*/
/*
 ****************************************************************************
 *                                                                          *
 *                   (c) Copyright 2010 ABM-utvikling                       *
 *                                                                          *
 * This program is free software; you can redistribute it and/or modify it  *
 * under the terms of the GNU General Public License as published by the    *
 * Free Software Foundation; either version 2 of the License, or (at your   *
 * option) any later version.                                               *
 *                                                                          *
 * This program is distributed in the hope that it will be useful, but      *
 * WITHOUT ANY WARRANTY; without even the implied warranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
 * Public License for more details. http://www.gnu.org/licenses/gpl.html    *
 *                                                                          *
 ****************************************************************************
 */

package no.abmu.questionnaire.propertyeditors;

import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * DateEditor.
 *
 * @author Thomas Oldervoll
 * @author $Author: jens $
 * @version $Rev: 15543 $
 * $Date: 2010-04-20 17:53:14 +0200 (Tue, 20 Apr 2010) $
 * copyright ABM-Utvikling
 * @since 2009-02-01
 */
public class DateEditor extends PropertyEditorSupport {

    private static final Log logger = (Log) LogFactory.getLog(DateEditor.class);

    private final String fieldCode;

    private DateFormat outputFormat;
    private List<SimpleDateFormat> dateFormats;

    public DateEditor(String fieldCode) {
        super();
        this.fieldCode = fieldCode;
        outputFormat = new SimpleDateFormat("dd/MM-yyyy");
        dateFormats = new ArrayList<SimpleDateFormat>();
        dateFormats.add(new SimpleDateFormat("yyyy-MM-dd"));
        dateFormats.add(new SimpleDateFormat("dd/MM-yyyy"));
        dateFormats.add(new SimpleDateFormat("dd/MM-yy"));
        dateFormats.add(new SimpleDateFormat("dd.MM.yyyy"));
        dateFormats.add(new SimpleDateFormat("dd-MM-yyyy"));
        dateFormats.add(new SimpleDateFormat("dd.MM.yy"));
    }

    public String getAsText() {
        if (logger.isDebugEnabled()) {
            logger.debug("Execute getAsText field=[" + fieldCode + "], getValue()=[" + getValue() + "]");
        }

        if (getValue() != null) {
            return outputFormat.format(getValue());
        } else {
            return "";
        }
    }

    public void setAsText(String string) throws IllegalArgumentException {
        if (logger.isDebugEnabled()) {
            logger.debug("Execute setAsText field=[" + fieldCode + "], with string=[" + string + "]");
        }

        if ((string == null) || (string.trim().length() == 0)) {
            setValue(null);
        } else {
            string = string.trim();
            Date date = null;
            for (DateFormat dateFormat : dateFormats) {
                try {
                    date = dateFormat.parse(string);
                } catch (ParseException e) {
                    // ignore, try next format
                }
            }
            if (date == null) {
                throw new IllegalArgumentException(string + " is not a valid date");
            }
            setValue(date);
        }
    }
}