001: /**
002: *
003: */package clime.messadmin.utils;
004:
005: import java.text.DateFormat;
006: import java.text.SimpleDateFormat;
007: import java.util.Date;
008:
009: import clime.messadmin.i18n.I18NSupport;
010:
011: /**
012: * @author Cédrik LIME
013: */
014: public abstract class DateUtils {
015: /**
016: * Default ISO 8601 datetime format: {@value}
017: * @see <a href="http://www.w3.org/TR/NOTE-datetime">ISO 8601 / European Standard EN 28601 DateTime</a>
018: * @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC 3399</a>
019: */
020: public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";//$NON-NLS-1$
021: public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";//$NON-NLS-1$
022: public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";//$NON-NLS-1$
023:
024: /**
025: *
026: */
027: private DateUtils() {
028: super ();
029: }
030:
031: public static String dateToFormattedDateTimeString(long value,
032: String format) {
033: String lc_format = format;
034: if (null == lc_format) {
035: lc_format = DEFAULT_DATE_TIME_FORMAT;
036: }
037: DateFormat formatter = new SimpleDateFormat(lc_format);
038: String dateStr = formatter.format(new Date(value));
039: if (DEFAULT_DATE_TIME_FORMAT == lc_format) {
040: // hack to be 100% W3
041: dateStr = dateStr.substring(0, dateStr.length() - 2) + ':'
042: + dateStr.substring(dateStr.length() - 2);
043: }
044: return dateStr;
045: }
046:
047: /*
048: * Can't use new Date(value) with formatter... :-(
049: */
050: public static String timeIntervalToFormattedString(
051: int in_milliseconds) {
052: StringBuffer buff = new StringBuffer(9);
053: long rest = in_milliseconds / 1000; // seconds
054: long hour = rest / 3600;
055: rest = rest % 3600;
056: long minute = rest / 60;
057: rest = rest % 60;
058: long second = rest;
059: if (hour < 10) {
060: buff.append('0');
061: }
062: buff.append(hour);
063: buff.append(':');
064: if (minute < 10) {
065: buff.append('0');
066: }
067: buff.append(minute);
068: buff.append(':');
069: if (second < 10) {
070: buff.append('0');
071: }
072: buff.append(second);
073: return buff.toString();
074: }
075:
076: public static String timeIntervalToFormattedString(
077: long in_milliseconds) {
078: StringBuffer buff = new StringBuffer(9);
079: int millis = (int) in_milliseconds % 1000;
080: long rest = in_milliseconds / 1000; // seconds
081: long hour = rest / 3600;
082: rest = rest % 3600;
083: long minute = rest / 60;
084: rest = rest % 60;
085: long second = rest;
086: if (hour >= 24) {
087: buff.append(I18NSupport.getLocalizedMessage(
088: "days", new Long[] { new Long(hour / 24) }));//$NON-NLS-1$
089: hour = hour % 24;
090: }
091: if (hour < 10) {
092: buff.append('0');
093: }
094: buff.append(hour);
095: buff.append(':');
096: if (minute < 10) {
097: buff.append('0');
098: }
099: buff.append(minute);
100: buff.append(':');
101: if (second < 10) {
102: buff.append('0');
103: }
104: buff.append(second);
105: return buff.toString();
106: }
107: }
|