KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > DateFormatter


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.util;
6
7 import java.text.DateFormat JavaDoc;
8 import java.text.ParseException JavaDoc;
9 import java.text.SimpleDateFormat JavaDoc;
10 import java.util.Date JavaDoc;
11
12
13 /**
14  * A bean that can be used to format dates
15  *
16  * @author Rickard Öberg (rickard@middleware-company.com)
17  * @version $Revision: 1.5 $
18  */

19 public class DateFormatter {
20     //~ Instance fields ////////////////////////////////////////////////////////
21

22     Date JavaDoc date;
23     DateFormat JavaDoc format;
24
25     // Attributes ----------------------------------------------------
26
DateFormat JavaDoc parser;
27
28     //~ Constructors ///////////////////////////////////////////////////////////
29

30     // Public --------------------------------------------------------
31
public DateFormatter() {
32         this.parser = new SimpleDateFormat JavaDoc();
33         this.format = new SimpleDateFormat JavaDoc();
34         this.date = new Date JavaDoc();
35     }
36
37     //~ Methods ////////////////////////////////////////////////////////////////
38

39     public void setDate(String JavaDoc date) {
40         try {
41             this.date = parser.parse(date);
42         } catch (ParseException JavaDoc e) {
43             throw new IllegalArgumentException JavaDoc(e.getMessage());
44         }
45     }
46
47     public void setDate(Date JavaDoc date) {
48         this.date = date;
49     }
50
51     public void setDate(int date) {
52         setDate(Integer.toString(date));
53     }
54
55     public Date JavaDoc getDate() {
56         return this.date;
57     }
58
59     public void setFormat(String JavaDoc format) {
60         this.format = new SimpleDateFormat JavaDoc(format);
61     }
62
63     public void setFormat(DateFormat JavaDoc format) {
64         this.format = format;
65     }
66
67     public String JavaDoc getFormattedDate() {
68         return format.format(date);
69     }
70
71     public void setParseFormat(String JavaDoc format) {
72         this.parser = new SimpleDateFormat JavaDoc(format);
73     }
74
75     public void setParser(DateFormat JavaDoc parser) {
76         this.parser = parser;
77     }
78
79     public void setTime(long time) {
80         date.setTime(time);
81     }
82 }
83
Popular Tags