View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.integration.beans;
    21
    22  import java.beans.PropertyEditor;
    23  import java.text.DateFormat;
    24  import java.text.ParseException;
    25  import java.text.SimpleDateFormat;
    26  import java.util.Date;
    27  import java.util.Locale;
    28  import java.util.regex.Pattern;
    29
    30  /**
    31   * A {@link PropertyEditor} which converts a {@link String} into
    32   * a {@link Date} and vice versa.
    33   *
    34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    35   */
    36  public class DateEditor extends AbstractPropertyEditor {
    37      private static final Pattern MILLIS = Pattern.compile("[0-9][0-9]*");
    38
    39      private final DateFormat[] formats = new DateFormat[] {
    40              new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH),
    41              new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.ENGLISH),
    42              new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH),
    43              new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH), new SimpleDateFormat("yyyy-MM", Locale.ENGLISH),
    44              new SimpleDateFormat("yyyy", Locale.ENGLISH), };
    45
    46      public DateEditor() {
    47          for (DateFormat f : formats) {
    48              f.setLenient(true);
    49          }
    50      }
    51
    52      @Override
    53      protected String toText(Object value) {
    54          if (value instanceof Number) {
    55              long time = ((Number) value).longValue();
    56              if (time <= 0) {
    57                  return null;
    58              }
    59              value = new Date(time);
    60          }
    61          return formats[0].format((Date) value);
    62      }
    63
    64      @Override
    65      protected Object toValue(String text) throws IllegalArgumentException {
    66          if (MILLIS.matcher(text).matches()) {
    67              long time = Long.parseLong(text);
    68              if (time <= 0) {
    69                  return null;
    70              }
    71              return new Date(time);
    72          }
    73
    74          for (DateFormat f : formats) {
    75              try {
    76                  return f.parse(text);
    77              } catch (ParseException e) {
    78              }
    79          }
    80
    81          throw new IllegalArgumentException("Wrong date: " + text);
    82      }
    83  }