KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > core > DateBean


1 /*
2  * DateBean.java
3  *
4  * Created on 30. August 2003, 21:35
5  */

6
7 package org.contineo.core;
8
9 import java.util.Date JavaDoc;
10 import java.text.SimpleDateFormat JavaDoc;
11 import java.text.ParsePosition JavaDoc;
12 import org.contineo.core.LocaleBean;
13
14 /**
15  * This class converts a date into some formats.
16  * @author Michael Scholz
17  * @author Sebastian Stein
18  * @version 1.1
19  */

20 public class DateBean extends Date JavaDoc {
21     
22     private static final long serialVersionUID = 1L;
23
24     /**
25      * This method returns the current date in the format yyyymmdd.
26      * @return current date in format yyyyMMdd
27      */

28     public static String JavaDoc toCompactString() {
29         SimpleDateFormat JavaDoc targetFormat = new SimpleDateFormat JavaDoc();
30         targetFormat.applyPattern("yyyyMMdd");
31         String JavaDoc result = targetFormat.format(new DateBean());
32
33         return result;
34     }
35     
36     /**
37      * This method formats a string with format dd.mm.yyyy into format yyyymmdd.
38      * @param date string containing the date to be converted
39      * @param lang from which language it should be converted
40      * @return a string containing the converted date
41      */

42     public static String JavaDoc toCompactString(String JavaDoc date, String JavaDoc lang) {
43         if (date != null && !date.equals(""))
44             return convertDate(new LocaleBean().getDateFormat(lang), "yyyyMMdd", date);
45         else
46             return "";
47     }
48     
49     /**
50      * This method formats a compact date (yyyyMMdd) to a localised date.
51      * @param date string containing the date to be converted
52      * @param lang to which language it should be converted to
53      * @return a string containing the converted date
54      */

55     public static String JavaDoc toLocaleString(String JavaDoc date, String JavaDoc lang) {
56         if (date != null && !date.equals(""))
57             return convertDate("yyyyMMdd", new LocaleBean().getDateFormat(lang), date);
58         else
59             return "";
60     }
61     
62     /**
63      * Converts a string containing a date between the given formats.
64      * @param formatIn current format of the string
65      * @param formatOut format the string should be converted to
66      * @param dateIn the string containing a date in the formatIn
67      * @return returns the converted string in the formatOut
68      */

69     public static String JavaDoc convertDate(String JavaDoc formatIn, String JavaDoc formatOut, String JavaDoc dateIn)
70     {
71         try {
72             return (new SimpleDateFormat JavaDoc(formatOut)).format((new SimpleDateFormat JavaDoc(formatIn).parse(dateIn, new ParsePosition JavaDoc(0))));
73         } catch (Exception JavaDoc ex) {
74             return null;
75         }
76     }
77 }
Popular Tags