Source Code Cross Referenced for DateUtil.java in  » Forum » mvnforum-1.1 » net » myvietnam » mvncore » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Forum » mvnforum 1.1 » net.myvietnam.mvncore.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/DateUtil.java,v 1.45 2008/01/24 02:35:41 minhnn Exp $
003:         * $Author: minhnn $
004:         * $Revision: 1.45 $
005:         * $Date: 2008/01/24 02:35:41 $
006:         *
007:         * ====================================================================
008:         *
009:         * Copyright (C) 2002-2007 by MyVietnam.net
010:         *
011:         * All copyright notices regarding MyVietnam and MyVietnam CoreLib
012:         * MUST remain intact in the scripts and source code.
013:         *
014:         * This library is free software; you can redistribute it and/or
015:         * modify it under the terms of the GNU Lesser General Public
016:         * License as published by the Free Software Foundation; either
017:         * version 2.1 of the License, or (at your option) any later version.
018:         *
019:         * This library is distributed in the hope that it will be useful,
020:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
021:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
022:         * Lesser General Public License for more details.
023:         *
024:         * You should have received a copy of the GNU Lesser General Public
025:         * License along with this library; if not, write to the Free Software
026:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
027:         *
028:         * Correspondence and Marketing Questions can be sent to:
029:         * info at MyVietnam net
030:         *
031:         * @author: Minh Nguyen  
032:         * @author: Mai  Nguyen  
033:         */
034:        package net.myvietnam.mvncore.util;
035:
036:        import java.sql.Timestamp;
037:        import java.text.DateFormat;
038:        import java.text.SimpleDateFormat;
039:        import java.util.*;
040:
041:        import net.myvietnam.mvncore.MVNCoreConfig;
042:
043:        /**
044:         * @todo: add config option to this class
045:         */
046:        public final class DateUtil {
047:
048:            public static final long SECOND = 1000;
049:            public static final long MINUTE = SECOND * 60;
050:            public static final long HOUR = MINUTE * 60;
051:            public static final long DAY = HOUR * 24;
052:            public static final long WEEK = DAY * 7;
053:            public static final long YEAR = DAY * 365; // or 366 ???
054:
055:            /**
056:             * This is the time difference between GMT time and Vietnamese time
057:             */
058:            public static final long GMT_VIETNAM_TIME_OFFSET = HOUR * 7;
059:
060:            /**
061:             * RFC 822 date format, for RSS 2.0
062:             * Sat, 07 Sep 2002 00:00:01 GMT
063:             */
064:            public static final String RFC_822_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
065:
066:            /**
067:             * ISO 8601 [W3CDTF] date format
068:             * 2005-06-05T14:52:57EDT
069:             * Note: not sure Z or z is correct
070:             */
071:            public static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
072:
073:            /**
074:             * UTC style date format
075:             */
076:            public static final String UTC_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
077:
078:            /**
079:             * This is the time difference between GMT time and SERVER time
080:             */
081:            //private static long SERVER_TIME_OFFSET = HOUR * (new DateOptions()).serverHourOffset;
082:            private static DateFormat ddMMyyyyFormat = new SimpleDateFormat(
083:                    "dd/MM/yyyy");
084:            private static DateFormat yyyyMMddFormat = new SimpleDateFormat(
085:                    "yyyy-MM-dd");
086:            private static DateFormat rfc822Format = new SimpleDateFormat(
087:                    RFC_822_DATE_FORMAT, Locale.US);
088:            private static DateFormat iso8601Format = new SimpleDateFormat(
089:                    ISO_8601_DATE_FORMAT, Locale.US);
090:            private static DateFormat dateFormat = DateFormat
091:                    .getDateInstance(DateFormat.DEFAULT);
092:            private static DateFormat datetimeFormat = DateFormat
093:                    .getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT);
094:            private static DateFormat viDateFormat = DateFormat
095:                    .getDateInstance(DateFormat.DEFAULT, new Locale("vi", "VN"));
096:            private static DateFormat viDatetimeFormat = DateFormat
097:                    .getDateTimeInstance(DateFormat.DEFAULT,
098:                            DateFormat.DEFAULT, new Locale("vi", "VN"));
099:            private static DateFormat headerTimeFormat = new SimpleDateFormat(
100:                    "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
101:            //private static DateFormat utcFormat = new SimpleDateFormat(UTC_DATE_FORMAT);
102:
103:            static {
104:                TimeZone gmt = TimeZone.getTimeZone("GMT");
105:                headerTimeFormat.setTimeZone(gmt);
106:            }
107:
108:            /**
109:             * private constructor
110:             */
111:            private DateUtil() {// prevent instantiation
112:            }
113:
114:            public static synchronized String getDateDDMMYYYY(Date date) {
115:                return ddMMyyyyFormat.format(date);
116:            }
117:
118:            public static synchronized String getDateYYYYMMDD(Date date) {
119:                return yyyyMMddFormat.format(date);
120:            }
121:
122:            public static synchronized String getDateRFC822(Date date) {
123:                return rfc822Format.format(date);
124:            }
125:
126:            public static synchronized String getDateISO8601(Date date) {
127:                String formattedDate = iso8601Format.format(date);
128:                int length = formattedDate.length();
129:                if (length > 2) {
130:                    return new StringBuffer(64).append(
131:                            formattedDate.substring(0, length - 2)).append(":")
132:                            .append(formattedDate.substring(length - 2))
133:                            .toString();
134:                }
135:                return formattedDate;
136:            }
137:
138:            public static synchronized String getHTTPHeaderTime(Date date) {
139:                return headerTimeFormat.format(date);
140:            }
141:
142:            public static synchronized String formatDate(Date date) {
143:                return dateFormat.format(date);
144:            }
145:
146:            public static synchronized String formatDateTime(Date date) {
147:                return datetimeFormat.format(date);
148:            }
149:
150:            public static synchronized String formatViDate(Date date) {
151:                return viDateFormat.format(date);
152:            }
153:
154:            public static synchronized String formatViDateTime(Date date) {
155:                return viDatetimeFormat.format(date);
156:            }
157:
158:            public static String getViDateTimeDesc() {
159:                return formatViDateTime(getVietnamDateFromGMTDate(getCurrentGMTTimestamp()));
160:            }
161:
162:            public static String getViDateDesc() {
163:                return formatViDate(getVietnamDateFromGMTDate(getCurrentGMTTimestamp()));
164:            }
165:
166:            public static Timestamp getCurrentGMTTimestamp() {
167:                return new Timestamp(System.currentTimeMillis()
168:                        - (HOUR * MVNCoreConfig.getServerHourOffset()));
169:            }
170:
171:            public static void updateCurrentGMTTimestamp(Timestamp timeToUpdate) {
172:                timeToUpdate.setTime(System.currentTimeMillis()
173:                        - (HOUR * MVNCoreConfig.getServerHourOffset()));
174:            }
175:
176:            public static Date getVietnamDateFromGMTDate(Date date) {
177:                return new Date(date.getTime() + GMT_VIETNAM_TIME_OFFSET);
178:            }
179:
180:            /* Note that Timestamp is extended from Date
181:            public static Date getVietnamTimestampFromGMTTimestamp(Timestamp date) {
182:                return new Timestamp(date.getTime() + GMT_VIETNAM_TIME_OFFSET);
183:            }*/
184:
185:            public static Date convertGMTDate(Date gmtDate, double hourOffset) {
186:                return new Date(gmtDate.getTime() + (long) (hourOffset * HOUR));
187:            }
188:
189:            public static Timestamp convertGMTTimestamp(Timestamp gmtTimestamp,
190:                    double hourOffset) {
191:                return new Timestamp(gmtTimestamp.getTime()
192:                        + (long) (hourOffset * HOUR));
193:            }
194:
195:            public static Timestamp getCurrentGMTTimestampExpiredYear(
196:                    int offsetYear) {
197:                Timestamp currentTimestamp = getCurrentGMTTimestamp();
198:                Timestamp expiredYear = new Timestamp(currentTimestamp
199:                        .getTime()
200:                        + YEAR * offsetYear);
201:                return expiredYear;
202:            }
203:
204:            public static Timestamp getCurrentGMTTimestampExpiredMonth(
205:                    int offsetMonth) {
206:                Timestamp currentTimestamp = getCurrentGMTTimestamp();
207:                Timestamp expiredYear = new Timestamp(currentTimestamp
208:                        .getTime()
209:                        + DAY * 30 * offsetMonth);
210:                return expiredYear;
211:            }
212:
213:            public static Timestamp getCurrentGMTTimestampExpiredDay(
214:                    int offsetDay) {
215:                Timestamp currentTimestamp = getCurrentGMTTimestamp();
216:                Timestamp expiredYear = new Timestamp(currentTimestamp
217:                        .getTime()
218:                        + DAY * offsetDay);
219:                return expiredYear;
220:                /*
221:                Calendar now = Calendar.getInstance();
222:                now.add(Calendar.DATE,offsetDay);
223:                return new Timestamp(now.getTime().getTime());
224:                 */
225:            }
226:
227:            public static String format(Date date, String pattern) {
228:                DateFormat formatter = new SimpleDateFormat(pattern, Locale.US);
229:                return formatter.format(date);
230:            }
231:
232:            public static String formatDuration(long duration, String pattern) {
233:                DurationFormater time = new DurationFormater(duration, pattern);
234:                return time.toString();
235:            }
236:
237:            public static String formatDuration(long duration) {
238:                DurationFormater time = new DurationFormater(duration, null);
239:                return time.toString();
240:            }
241:
242:            public static void main(String[] agrs) {
243:                long duration = (long) 1000 * 60 * 60 * 24 * 130 + (long) 1000
244:                        * 60 * 80;
245:                System.out.println(duration);
246:                System.out.println("Duration of " + duration + " duration = "
247:                        + formatDuration(duration));
248:                Date d = new Date();
249:                System.out.println(" Date is " + DateUtil.getDateISO8601(d));
250:            }
251:
252:            public static Timestamp getStartDate(Timestamp time) {
253:                Calendar calendar = Calendar.getInstance();
254:                calendar.setTimeInMillis(time.getTime());
255:                calendar.set(Calendar.HOUR_OF_DAY, 0);
256:                calendar.set(Calendar.MINUTE, 0);
257:                calendar.set(Calendar.SECOND, 0);
258:                return new Timestamp(calendar.getTimeInMillis());
259:            }
260:
261:            public static Timestamp getEndDate(Timestamp time) {
262:                Calendar calendar = Calendar.getInstance();
263:                calendar.setTimeInMillis(time.getTime());
264:                calendar.set(Calendar.HOUR_OF_DAY, 23);
265:                calendar.set(Calendar.MINUTE, 59);
266:                calendar.set(Calendar.SECOND, 59);
267:                return new Timestamp(calendar.getTimeInMillis());
268:            }
269:
270:            public static Timestamp getStartHour(Timestamp time) {
271:                Calendar calendar = Calendar.getInstance();
272:                calendar.setTimeInMillis(time.getTime());
273:                calendar.set(Calendar.MINUTE, 0);
274:                calendar.set(Calendar.SECOND, 0);
275:                return new Timestamp(calendar.getTimeInMillis());
276:            }
277:
278:            public static Timestamp getEndHour(Timestamp time) {
279:                Calendar calendar = Calendar.getInstance();
280:                calendar.setTimeInMillis(time.getTime());
281:                calendar.set(Calendar.MINUTE, 59);
282:                calendar.set(Calendar.SECOND, 59);
283:                return new Timestamp(calendar.getTimeInMillis());
284:            }
285:        }
286:
287:        class DurationFormater {
288:            public static final long MILLISECONDS_PER_SECOND = 1000;
289:            public static final long SECONDS_PER_MINUTE = 60;
290:            public static final long MINUTES_PER_HOUR = 60;
291:            public static final long HOURS_PER_DAY = 24;
292:
293:            public static final int MILLISECOND = 0;
294:            public static final int SECOND = 1;
295:            public static final int MINUTE = 2;
296:            public static final int HOUR = 3;
297:            public static final int DAY = 4;
298:
299:            public static final String PATTERNS[] = { "@ms", "@s", "@m", "@h",
300:                    "@d" };
301:            private static final long[] AMOUNTS = { MILLISECONDS_PER_SECOND,
302:                    SECONDS_PER_MINUTE, MINUTES_PER_HOUR, HOURS_PER_DAY };
303:            private static long[] times = new long[5];
304:            private long time;
305:            private String pattern;
306:            private boolean detail = false;
307:
308:            public DurationFormater() {
309:            }
310:
311:            public DurationFormater(long time, String pattern) {
312:                this .time = time;
313:                this .pattern = pattern;
314:                update();
315:            }
316:
317:            public DurationFormater(long time) {
318:                this .time = time;
319:                update();
320:            }
321:
322:            private void update() {
323:                long remain = time;
324:                for (int i = 0; i < AMOUNTS.length; i++) {
325:                    times[i] = remain % AMOUNTS[i];
326:                    remain = remain / AMOUNTS[i];
327:                }
328:                times[DAY] = (int) remain;
329:            }
330:
331:            /*  @h
332:             *  @M  --> Month
333:             *  @m  --> minute
334:             *  @ms --> millisecond
335:             *  @s  --> second
336:             */
337:            public void setPattern(String pattern) {
338:                this .pattern = pattern;
339:            }
340:
341:            public long getTime() {
342:                return time;
343:            }
344:
345:            public void setTime(long duration) {
346:                time = duration;
347:                update();
348:            }
349:
350:            public long getMilliseconds() {
351:                return times[MILLISECOND];
352:            }
353:
354:            public long getSeconds() {
355:                return times[SECOND];
356:            }
357:
358:            public long getMinutes() {
359:                return times[MINUTE];
360:            }
361:
362:            public long getHours() {
363:                return times[HOUR];
364:            }
365:
366:            public long getDays() {
367:                return times[DAY];
368:            }
369:
370:            public void setDetail(boolean detail) {
371:                this .detail = detail;
372:            }
373:
374:            public String getString() {
375:                StringBuffer buffer = new StringBuffer(1024);
376:                buffer.append(pattern);
377:                for (int i = 0; i < PATTERNS.length; i++) {
378:                    int start = -1;
379:                    int end = -1;
380:                    // Note, in JDK 1.3, StringBuffer does not have method indexOf
381:                    while ((start = buffer.toString().indexOf(PATTERNS[i])) > -1) {
382:                        end = start + PATTERNS[i].length();
383:                        buffer.replace(start, end, String.valueOf(times[i]));
384:                    }
385:                }
386:                return buffer.toString();
387:            }
388:
389:            public String toString() {
390:                if (pattern != null) {
391:                    return getString();
392:                }
393:
394:                StringBuffer desc = new StringBuffer(256);
395:                if (times[DAY] > 0) {
396:                    desc.append(checkPlural(times[DAY], "day"));
397:                }
398:                if (times[HOUR] > 0) {
399:                    desc.append(checkPlural(times[HOUR], "hour"));
400:                }
401:                if ((times[MINUTE] > 0)
402:                        || (times[DAY] == 0 && times[MINUTE] == 0)) {
403:                    desc.append(checkPlural(times[MINUTE], "minute"));
404:                }
405:                if (detail) {
406:                    desc.append(checkPlural(times[SECOND], "second"));
407:                    desc.append(checkPlural(times[MILLISECOND], "millisecond"));
408:                }
409:                return desc.toString();
410:            }
411:
412:            private static String checkPlural(long amount, String unit) {
413:                StringBuffer desc = new StringBuffer(20);
414:                desc.append(amount).append(" ").append(unit);
415:                if (amount > 1) {
416:                    desc.append("s");
417:                }
418:                return desc.append(" ").toString();
419:            }
420:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.