001: /*
002: * DateUtils.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: /*
023: * The Apache Software License, Version 1.1
024: *
025: * Copyright (c) 1999 The Apache Software Foundation. All rights
026: * reserved.
027: *
028: */
029:
030: package org.underworldlabs.util;
031:
032: import java.text.SimpleDateFormat;
033:
034: import java.util.Calendar;
035: import java.util.Locale;
036: import java.util.TimeZone;
037:
038: /* ----------------------------------------------------------
039: * CVS NOTE: Changes to the CVS repository prior to the
040: * release of version 3.0.0beta1 has meant a
041: * resetting of CVS revision numbers.
042: * ----------------------------------------------------------
043: */
044:
045: /**
046: * <p>This class was modified and included as a part of the Package
047: * <code>org.executequery.util</code> in the application Execute Query.
048: *
049: * Original authorship belongs to The Apache Software Foundation.
050: * <p>
051: * Copyright (c) 1999 The Apache Software Foundation.
052: *
053: * Takis Diakoumis 2002
054: */
055:
056: /**
057: *
058: * @author Takis Diakoumis
059: * @version $Revision: 1.4 $
060: * @date $Date: 2006/05/14 06:56:07 $
061: */
062: public class DateUtils {
063:
064: private Calendar calendar;
065: private static TimeZone timezone;
066: private static Locale locale;
067:
068: private SimpleDateFormat dateFormat;
069:
070: private static final String DASH = "-";
071: private static final String COLON = ":";
072: private static final String SPACE = " ";
073: private static final String ZERO = "0";
074:
075: public DateUtils() {
076: if (timezone == null) {
077: timezone = TimeZone.getTimeZone(System
078: .getProperty("user.country"));
079: }
080: if (locale == null) {
081: locale = new Locale(System.getProperty("user.language"),
082: System.getProperty("user.timezone"));
083: }
084: calendar = Calendar.getInstance();
085: }
086:
087: public DateUtils(String format) {
088: this ();
089: dateFormat = new SimpleDateFormat(format);
090: }
091:
092: public void reset() {
093: calendar = Calendar.getInstance(timezone, locale);
094: }
095:
096: public long getTimeInMillis() {
097: return calendar.getTimeInMillis();
098: }
099:
100: public String getFormattedDate() {
101: return dateFormat.format(calendar.getTime());
102: }
103:
104: public void resetTimeZone(String timezoneString, String language,
105: String country) {
106: timezone = TimeZone.getTimeZone(timezoneString);
107: locale = new Locale(language, country);
108: }
109:
110: public int getYear() {
111: return calendar.get(Calendar.YEAR);
112: }
113:
114: public String getMonth() {
115: int m = getMonthInt();
116: String[] months = new String[] { "Jan", "Feb", "Mar", "Apr",
117: "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
118: if (m > 12) {
119: return "Unknown to Man";
120: }
121:
122: return months[m - 1];
123: }
124:
125: public String getDay() {
126: int x = getDayOfWeek();
127: String[] days = new String[] { "Sunday", "Monday", "Tuesday",
128: "Wednesday", "Thursday", "Friday", "Saturday" };
129:
130: if (x > 7) {
131: return "Unknown to Man";
132: }
133:
134: return days[x - 1];
135: }
136:
137: public int getMonthInt() {
138: return 1 + calendar.get(Calendar.MONTH);
139: }
140:
141: public String getDate() {
142: String year = Integer.toString(getYear());
143: return getDayOfMonth() + DASH + getMonth() + DASH
144: + year.substring(2);
145: }
146:
147: public String getDate(char delimeter) {
148: String year = Integer.toString(getYear());
149: return getDayOfMonth() + delimeter + getMonth() + delimeter
150: + year.substring(2);
151: }
152:
153: public String getDateInt(String delimeter) {
154: String year = Integer.toString(getYear());
155: return getDayOfMonth() + delimeter + getMonthInt() + delimeter
156: + year.substring(2);
157: }
158:
159: public String getTime() {
160: return getHour() + COLON + getMinute();
161: }
162:
163: public String getLongTime() {
164: return getHour() + COLON + getMinute() + COLON + getSecond();
165: }
166:
167: public String getDateTime() {
168: return getDate() + SPACE + getTime();
169: }
170:
171: public String getLongDateTime() {
172: return getDayOfMonth() + DASH + getMonth() + DASH + getYear()
173: + SPACE + getLongTime();
174: }
175:
176: public int getDayOfMonth() {
177: return calendar.get(Calendar.DAY_OF_MONTH);
178: }
179:
180: public int getDayOfWeek() {
181: return calendar.get(Calendar.DAY_OF_WEEK);
182: }
183:
184: public int getWeekOfMonth() {
185: return calendar.get(Calendar.WEEK_OF_MONTH);
186: }
187:
188: public int getHour() {
189: return calendar.get(Calendar.HOUR_OF_DAY);
190: }
191:
192: public String getSecond() {
193: int tempSecond = calendar.get(Calendar.SECOND);
194: return tempSecond < 10 ? ZERO + tempSecond : Integer
195: .toString(tempSecond);
196: }
197:
198: public String getMinute() {
199: int tempMinute = calendar.get(Calendar.MINUTE);
200: return tempMinute < 10 ? ZERO + tempMinute : Integer
201: .toString(tempMinute);
202: }
203:
204: public int getMinuteForCalc() {
205: return calendar.get(Calendar.MINUTE);
206: }
207:
208: }
|