001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import java.text.DateFormat;
024: import java.text.SimpleDateFormat;
025:
026: import java.util.Date;
027: import java.util.Locale;
028: import java.util.TimeZone;
029:
030: /**
031: * <a href="DateUtil.java.html"><b><i>View Source</i></b></a>
032: *
033: * @author Brian Wing Shun Chan
034: *
035: */
036: public class DateUtil {
037:
038: public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
039:
040: public static int compareTo(Date date1, Date date2) {
041:
042: // Workaround for bug in JDK 1.5.x. This bug is fixed in JDK 1.5.07. See
043: // http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=6207898 for
044: // more information.
045:
046: if ((date1 != null) && (date2 == null)) {
047: return -1;
048: } else if ((date1 == null) && (date2 != null)) {
049: return 1;
050: } else if ((date1 == null) && (date2 == null)) {
051: return 0;
052: }
053:
054: long time1 = date1.getTime();
055: long time2 = date2.getTime();
056:
057: if (time1 == time2) {
058: return 0;
059: } else if (time1 < time2) {
060: return -1;
061: } else {
062: return 1;
063: }
064: }
065:
066: public static String getCurrentDate(String pattern, Locale locale) {
067: return getDate(new Date(), pattern, locale);
068: }
069:
070: public static String getDate(Date date, String pattern,
071: Locale locale) {
072: DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
073:
074: return dateFormat.format(date);
075: }
076:
077: public static DateFormat getISOFormat() {
078: return getISOFormat(StringPool.BLANK);
079: }
080:
081: public static DateFormat getISOFormat(String text) {
082: String pattern = StringPool.BLANK;
083:
084: if (text.length() == 8) {
085: pattern = "yyyyMMdd";
086: } else if (text.length() == 12) {
087: pattern = "yyyyMMddHHmm";
088: } else if (text.length() == 13) {
089: pattern = "yyyyMMdd'T'HHmm";
090: } else if (text.length() == 14) {
091: pattern = "yyyyMMddHHmmss";
092: } else if (text.length() == 15) {
093: pattern = "yyyyMMdd'T'HHmmss";
094: } else if ((text.length() > 8) && (text.charAt(8) == 'T')) {
095: pattern = "yyyyMMdd'T'HHmmssz";
096: } else {
097: pattern = "yyyyMMddHHmmssz";
098: }
099:
100: return new SimpleDateFormat(pattern);
101: }
102:
103: public static DateFormat getISO8601Format() {
104: return new SimpleDateFormat(ISO_8601_PATTERN);
105: }
106:
107: public static DateFormat getUTCFormat() {
108: return getUTCFormat(StringPool.BLANK);
109: }
110:
111: public static DateFormat getUTCFormat(String text) {
112: String pattern = StringPool.BLANK;
113:
114: if (text.length() == 8) {
115: pattern = "yyyyMMdd";
116: } else if (text.length() == 12) {
117: pattern = "yyyyMMddHHmm";
118: } else if (text.length() == 13) {
119: pattern = "yyyyMMdd'T'HHmm";
120: } else if (text.length() == 14) {
121: pattern = "yyyyMMddHHmmss";
122: } else if (text.length() == 15) {
123: pattern = "yyyyMMdd'T'HHmmss";
124: } else {
125: pattern = "yyyyMMdd'T'HHmmssz";
126: }
127:
128: DateFormat dateFormat = new SimpleDateFormat(pattern);
129:
130: dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
131:
132: return dateFormat;
133: }
134:
135: }
|