01: /*
02: * $Id: DateFormatter.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.util;
22:
23: import java.text.DateFormat;
24: import java.text.ParseException;
25: import java.text.SimpleDateFormat;
26: import java.util.Date;
27:
28: /**
29: * A bean that can be used to format dates
30: *
31: */
32: public class DateFormatter {
33:
34: Date date;
35: DateFormat format;
36:
37: // Attributes ----------------------------------------------------
38: DateFormat parser;
39:
40: // Public --------------------------------------------------------
41: public DateFormatter() {
42: this .parser = new SimpleDateFormat();
43: this .format = new SimpleDateFormat();
44: this .date = new Date();
45: }
46:
47: public void setDate(String date) {
48: try {
49: this .date = parser.parse(date);
50: } catch (ParseException e) {
51: throw new IllegalArgumentException(e.getMessage());
52: }
53: }
54:
55: public void setDate(Date date) {
56: this .date = date;
57: }
58:
59: public void setDate(int date) {
60: setDate(Integer.toString(date));
61: }
62:
63: public Date getDate() {
64: return this .date;
65: }
66:
67: public void setFormat(String format) {
68: this .format = new SimpleDateFormat(format);
69: }
70:
71: public void setFormat(DateFormat format) {
72: this .format = format;
73: }
74:
75: public String getFormattedDate() {
76: return format.format(date);
77: }
78:
79: public void setParseFormat(String format) {
80: this .parser = new SimpleDateFormat(format);
81: }
82:
83: public void setParser(DateFormat parser) {
84: this .parser = parser;
85: }
86:
87: public void setTime(long time) {
88: date.setTime(time);
89: }
90: }
|