001: package org.contineo.core.i18n;
002:
003: import java.text.DateFormat;
004: import java.text.ParseException;
005: import java.text.ParsePosition;
006: import java.text.SimpleDateFormat;
007: import java.util.Date;
008:
009: /**
010: * This class converts a date into some formats.
011: *
012: * @author Michael Scholz
013: * @author Sebastian Stein
014: * @version 1.1
015: */
016: public class DateBean extends Date {
017: private static final long serialVersionUID = 1L;
018:
019: /**
020: * This method returns the current date in the format yyyyMMdd HH:mm:ss.
021: *
022: * @return current date in format yyyyMMdd HH:mm:ss
023: */
024: public static String toCompactString() {
025: SimpleDateFormat targetFormat = new SimpleDateFormat();
026: targetFormat.applyPattern("yyyyMMdd HHmmss");
027:
028: String result = targetFormat.format(new DateBean());
029:
030: return result;
031: }
032:
033: /**
034: * This method creates the Date object for a compact string in the format
035: * yyyyMMdd HH:mm:ss or yyyyHHdd.
036: */
037: public static Date dateFromCompactString(String compactString) {
038: SimpleDateFormat targetFormat = new SimpleDateFormat(
039: "yyyyMMdd HHmmss");
040: if (compactString.length() < 9)
041: targetFormat = new SimpleDateFormat("yyyyMMdd");
042:
043: try {
044: return targetFormat.parse(compactString);
045: } catch (ParseException e) {
046: return null;
047: }
048: }
049:
050: /**
051: * This method formats a string with format dd.mm.yyyy into format yyyymmdd.
052: *
053: * @param date string containing the date to be converted
054: * @param lang from which language it should be converted
055: * @return a string containing the converted date
056: */
057: public static String toCompactString(String date, String lang) {
058: return toCompactString(date, LocaleInformation
059: .getDateFormat(lang), lang);
060: }
061:
062: /**
063: * This method formats a a date into format yyyymmdd.
064: *
065: * @param date the date to be converted
066: * @return a string containing the converted date
067: */
068: public static String toCompactString(Date date) {
069: DateFormat df = new SimpleDateFormat("yyyyMMdd");
070: return df.format(date);
071: }
072:
073: /**
074: * This method formats a string with format dd.mm.yyyy into format yyyymmdd.
075: *
076: * @param date string containing the date to be converted
077: * @param lang from which language it should be converted
078: * @return a string containing the converted date
079: */
080: public static String toCompactString(String date,
081: String dateFormat, String lang) {
082: if ((date != null) && !date.equals("")) {
083: return convertDate(dateFormat, "yyyyMMdd HHmmss", date);
084: } else {
085: return "";
086: }
087: }
088:
089: /**
090: * This method formats a compact date (yyyyMMdd) to a localised date.
091: *
092: * @param date string containing the date to be converted
093: * @param lang to which language it should be converted to
094: * @return a string containing the converted date
095: */
096: public static String toLocaleString(String date, String lang) {
097: if ((date != null) && !date.equals("")) {
098: return convertDate("yyyyMMdd HHmmss", LocaleInformation
099: .getDateFormat(lang), date);
100: } else {
101: return "";
102: }
103: }
104:
105: /**
106: * Converts a string containing a date between the given formats.
107: *
108: * @param formatIn current format of the string
109: * @param formatOut format the string should be converted to
110: * @param dateIn the string containing a date in the formatIn
111: * @return returns the converted string in the formatOut
112: */
113: public static String convertDate(String formatIn, String formatOut,
114: String dateIn) {
115: try {
116: if (dateIn.length() > 8) {
117: return (new SimpleDateFormat(formatOut))
118: .format((new SimpleDateFormat(formatIn).parse(
119: dateIn, new ParsePosition(0))));
120: } else {
121: return (new SimpleDateFormat(formatOut))
122: .format((new SimpleDateFormat("yyyyMMdd")
123: .parse(dateIn, new ParsePosition(0))));
124: }
125: } catch (Exception ex) {
126: return null;
127: }
128: }
129: }
|