01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.util;
06:
07: import java.text.DateFormat;
08: import java.text.ParseException;
09: import java.text.SimpleDateFormat;
10: import java.util.Date;
11:
12: /**
13: * A bean that can be used to format dates
14: *
15: * @author Rickard Öberg (rickard@middleware-company.com)
16: * @version $Revision: 1282 $
17: */
18: public class DateFormatter {
19:
20: Date date;
21: DateFormat format;
22:
23: // Attributes ----------------------------------------------------
24: DateFormat parser;
25:
26: // Public --------------------------------------------------------
27: public DateFormatter() {
28: this .parser = new SimpleDateFormat();
29: this .format = new SimpleDateFormat();
30: this .date = new Date();
31: }
32:
33: public void setDate(String date) {
34: try {
35: this .date = parser.parse(date);
36: } catch (ParseException e) {
37: throw new IllegalArgumentException(e.getMessage());
38: }
39: }
40:
41: public void setDate(Date date) {
42: this .date = date;
43: }
44:
45: public void setDate(int date) {
46: setDate(Integer.toString(date));
47: }
48:
49: public Date getDate() {
50: return this .date;
51: }
52:
53: public void setFormat(String format) {
54: this .format = new SimpleDateFormat(format);
55: }
56:
57: public void setFormat(DateFormat format) {
58: this .format = format;
59: }
60:
61: public String getFormattedDate() {
62: return format.format(date);
63: }
64:
65: public void setParseFormat(String format) {
66: this .parser = new SimpleDateFormat(format);
67: }
68:
69: public void setParser(DateFormat parser) {
70: this .parser = parser;
71: }
72:
73: public void setTime(long time) {
74: date.setTime(time);
75: }
76: }
|