01: /*
02: * $Id: DateConverter.java 471756 2006-11-06 15:01:43Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21: package org.apache.struts2.showcase.chat;
22:
23: import java.text.ParseException;
24: import java.text.SimpleDateFormat;
25: import java.util.Date;
26: import java.util.Map;
27:
28: import org.apache.commons.logging.Log;
29: import org.apache.commons.logging.LogFactory;
30: import org.apache.struts2.util.StrutsTypeConverter;
31:
32: public class DateConverter extends StrutsTypeConverter {
33:
34: private static final Log _log = LogFactory
35: .getLog(DateConverter.class);
36:
37: public Object convertFromString(Map context, String[] values,
38: Class toClass) {
39:
40: if (values.length > 0 && values[0] != null
41: && values[0].trim().length() > 0) {
42: SimpleDateFormat sdf = new SimpleDateFormat();
43: try {
44: return sdf.parse(values[0]);
45: } catch (ParseException e) {
46: _log.error("error converting value [" + values[0]
47: + "] to Date ", e);
48: }
49: }
50: return null;
51: }
52:
53: public String convertToString(Map context, Object o) {
54:
55: if (o instanceof Date) {
56: SimpleDateFormat sdf = new SimpleDateFormat(
57: "dd/MM/yyyy hh:mm:ss");
58: return sdf.format((Date) o);
59: }
60: return "";
61: }
62: }
|