001: /*
002: * Copyright 2001-2007 Hippo (www.hippo.nl)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package nl.hippo.util;
017:
018: import java.text.DateFormat;
019: import java.text.ParseException;
020: import java.text.SimpleDateFormat;
021: import java.util.Calendar;
022: import java.util.Date;
023: import java.util.GregorianCalendar;
024: import java.util.Locale;
025:
026: public class DateHelper {
027: public static int oneWeekAgo() {
028: GregorianCalendar date = new GregorianCalendar(); // today
029: date.add(Calendar.DATE, -7); // one week ago
030: int fromDate = dateAsYYYYMMDD(date);
031: //System.out.println("DateHelper: oneWeekAgo: " + fromDate);
032: return fromDate;
033: }
034:
035: public static String timeOffset(String format, int years,
036: int months, int days) {
037: GregorianCalendar date = new GregorianCalendar(); // today
038: if (years != 0)
039: date.add(Calendar.YEAR, years);
040: if (months != 0)
041: date.add(Calendar.MONTH, months);
042: if (days != 0)
043: date.add(Calendar.DATE, days);
044:
045: SimpleDateFormat sdf = new SimpleDateFormat(format);
046:
047: return sdf.format(date.getTime());
048: }
049:
050: public static int twoWeeksAgo() {
051: GregorianCalendar date = new GregorianCalendar(); // today
052: date.add(Calendar.DATE, -14); // two weeks ago
053: int fromDate = dateAsYYYYMMDD(date);
054: //System.out.println("DateHelper: twoWeeksAgo: " + fromDate);
055: return fromDate;
056: }
057:
058: public static int oneMonthAgo() {
059: GregorianCalendar date = new GregorianCalendar(); // today
060: date.add(Calendar.MONTH, -1); // one month ago
061: int fromDate = dateAsYYYYMMDD(date);
062: //System.out.println("DateHelper: oneMonthAgo: " + fromDate);
063: return fromDate;
064: }
065:
066: public static int twoMonthsAgo() {
067: GregorianCalendar date = new GregorianCalendar(); // today
068: date.add(Calendar.MONTH, -2); // two months ago
069: int fromDate = dateAsYYYYMMDD(date);
070: return fromDate;
071: }
072:
073: public static int sixMonthsAgo() {
074: GregorianCalendar date = new GregorianCalendar(); // today
075: date.add(Calendar.MONTH, -6);
076: int fromDate = dateAsYYYYMMDD(date);
077: return fromDate;
078: }
079:
080: public static int oneYearAgo() {
081: GregorianCalendar date = new GregorianCalendar(); // today
082: date.add(Calendar.YEAR, -1);
083: int fromDate = dateAsYYYYMMDD(date);
084: return fromDate;
085: }
086:
087: public static int dateAsYYYYMMDD(GregorianCalendar date) {
088: SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
089: String dateString = dateFormat.format(date.getTime());
090: return Integer.parseInt(dateString);
091: }
092:
093: public static int compareDates(String date1, String format1,
094: String date2, String format2) throws ParseException {
095: DateFormat firstDateFormat;
096: if (isStringNullOrEmpty(format1)) {
097: firstDateFormat = createIsoDateFormat();
098: } else {
099: firstDateFormat = new SimpleDateFormat(format1);
100: }
101:
102: DateFormat secondDateFormat;
103: if (isStringNullOrEmpty(format2)) {
104: secondDateFormat = createIsoDateFormat();
105: } else {
106: secondDateFormat = new SimpleDateFormat(format2);
107: }
108: Date firstDate = firstDateFormat.parse(date1);
109: Date secondDate = secondDateFormat.parse(date2);
110:
111: return firstDate.compareTo(secondDate);
112: }
113:
114: /**
115: * @param string
116: * @return
117: */
118: private static boolean isStringNullOrEmpty(String string) {
119: return string == null || string.equals("");
120: }
121:
122: private static DateFormat createIsoDateFormat() {
123: DateFormat result;
124: result = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z",
125: Locale.ENGLISH);
126: return result;
127: }
128:
129: public static String reformatDate(String date,
130: String sourcePattern, String pattern) throws ParseException {
131: DateFormat df = new SimpleDateFormat(sourcePattern,
132: Locale.ENGLISH);
133: DateFormat df2 = new SimpleDateFormat(pattern);
134: String reformattedDate = null;
135: Date tempDate = df.parse(date);
136: reformattedDate = df2.format(tempDate);
137: return reformattedDate;
138: }
139:
140: }
|