Source Code Cross Referenced for CalendarUtil.java in  » Portal » liferay-portal-4.4.2 » com » liferay » util » cal » 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 » Portal » liferay portal 4.4.2 » com.liferay.util.cal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.util.cal;
022:
023:        import com.liferay.portal.kernel.util.CalendarFactoryUtil;
024:        import com.liferay.portal.kernel.util.LocaleUtil;
025:        import com.liferay.portal.kernel.util.StringMaker;
026:        import com.liferay.portal.kernel.util.TimeZoneUtil;
027:        import com.liferay.portal.kernel.util.Validator;
028:        import com.liferay.util.CollectionFactory;
029:
030:        import java.sql.Timestamp;
031:
032:        import java.text.DateFormat;
033:        import java.text.SimpleDateFormat;
034:
035:        import java.util.Calendar;
036:        import java.util.Date;
037:        import java.util.Locale;
038:        import java.util.Map;
039:        import java.util.TimeZone;
040:
041:        /**
042:         * <a href="CalendarUtil.java.html"><b><i>View Source</i></b></a>
043:         *
044:         * @author Brian Wing Shun Chan
045:         *
046:         */
047:        public class CalendarUtil {
048:
049:            public static String[] DAYS_ABBREVIATION = new String[] {
050:                    "sunday-abbreviation", "monday-abbreviation",
051:                    "tuesday-abbreviation", "wednesday-abbreviation",
052:                    "thursday-abbreviation", "friday-abbreviation",
053:                    "saturday-abbreviation" };
054:
055:            public static int[] MONTH_IDS = new int[] { Calendar.JANUARY,
056:                    Calendar.FEBRUARY, Calendar.MARCH, Calendar.APRIL,
057:                    Calendar.MAY, Calendar.JUNE, Calendar.JULY,
058:                    Calendar.AUGUST, Calendar.SEPTEMBER, Calendar.OCTOBER,
059:                    Calendar.NOVEMBER, Calendar.DECEMBER };
060:
061:            public static boolean afterByDay(Date date1, Date date2) {
062:                long millis1 = date1.getTime() / 86400000;
063:                long millis2 = date2.getTime() / 86400000;
064:
065:                if (millis1 > millis2) {
066:                    return true;
067:                } else {
068:                    return false;
069:                }
070:            }
071:
072:            public static boolean beforeByDay(Date date1, Date date2) {
073:                long millis1 = date1.getTime() / 86400000;
074:                long millis2 = date2.getTime() / 86400000;
075:
076:                if (millis1 < millis2) {
077:                    return true;
078:                } else {
079:                    return false;
080:                }
081:            }
082:
083:            public static boolean equalsByDay(Date date1, Date date2) {
084:                long millis1 = date1.getTime() / 86400000;
085:                long millis2 = date2.getTime() / 86400000;
086:
087:                if (millis1 == millis2) {
088:                    return true;
089:                } else {
090:                    return false;
091:                }
092:            }
093:
094:            public static int getAge(Date date, TimeZone tz) {
095:                return getAge(date, CalendarFactoryUtil.getCalendar(tz));
096:            }
097:
098:            public static int getAge(Date date, Calendar today) {
099:                Calendar birthday = CalendarFactoryUtil.getCalendar();
100:
101:                birthday.setTime(date);
102:
103:                int yearDiff = today.get(Calendar.YEAR)
104:                        - birthday.get(Calendar.YEAR);
105:
106:                if (today.get(Calendar.MONTH) < birthday.get(Calendar.MONTH)) {
107:                    yearDiff--;
108:                } else if (today.get(Calendar.MONTH) == birthday
109:                        .get(Calendar.MONTH)
110:                        && today.get(Calendar.DATE) < birthday
111:                                .get(Calendar.DATE)) {
112:
113:                    yearDiff--;
114:                }
115:
116:                return yearDiff;
117:            }
118:
119:            public static String[] getDays(Locale locale) {
120:                return getDays(locale, null);
121:            }
122:
123:            public static String[] getDays(Locale locale, String pattern) {
124:                if (Validator.isNull(pattern)) {
125:                    pattern = "EEEE";
126:                }
127:
128:                StringMaker sm = new StringMaker();
129:
130:                sm.append("days_");
131:                sm.append(pattern);
132:                sm.append("_");
133:                sm.append(locale.getLanguage());
134:                sm.append("_");
135:                sm.append(locale.getCountry());
136:
137:                String key = sm.toString();
138:
139:                String[] days = (String[]) _calendarPool.get(key);
140:
141:                if (days == null) {
142:                    days = new String[7];
143:
144:                    DateFormat dayFormat = new SimpleDateFormat(pattern, locale);
145:
146:                    Calendar cal = CalendarFactoryUtil.getCalendar();
147:
148:                    cal.set(Calendar.DATE, 1);
149:
150:                    for (int i = 0; i < 7; i++) {
151:                        cal.set(Calendar.DAY_OF_WEEK, i + 1);
152:
153:                        days[i] = dayFormat.format(cal.getTime());
154:                    }
155:
156:                    _calendarPool.put(key, days);
157:                }
158:
159:                return days;
160:            }
161:
162:            public static int getDaysInMonth(Calendar cal) {
163:                return getDaysInMonth(cal.get(Calendar.MONTH), cal
164:                        .get(Calendar.YEAR));
165:            }
166:
167:            public static int getDaysInMonth(int month, int year) {
168:                month++;
169:
170:                if ((month == 1) || (month == 3) || (month == 5)
171:                        || (month == 7) || (month == 8) || (month == 10)
172:                        || (month == 12)) {
173:
174:                    return 31;
175:                } else if ((month == 4) || (month == 6) || (month == 9)
176:                        || (month == 11)) {
177:
178:                    return 30;
179:                } else {
180:                    if (((year % 4) == 0) && ((year % 100) != 0)
181:                            || ((year % 400) == 0)) {
182:
183:                        return 29;
184:                    } else {
185:                        return 28;
186:                    }
187:                }
188:            }
189:
190:            public static int getGregorianDay(Calendar cal) {
191:                int year = cal.get(Calendar.YEAR) - 1600;
192:
193:                int month = cal.get(Calendar.MONTH) + 1;
194:
195:                if (month < 3) {
196:                    month += 12;
197:                }
198:
199:                int day = cal.get(Calendar.DATE);
200:
201:                int gregorianDay = (int) (6286 + (year * 365.25) - (year / 100)
202:                        + (year / 400) + (30.6 * month) + 0.2 + day);
203:
204:                return gregorianDay;
205:            }
206:
207:            public static Date getGTDate(Calendar cal) {
208:                Calendar gtCal = (Calendar) cal.clone();
209:
210:                gtCal.set(Calendar.HOUR_OF_DAY, 0);
211:                gtCal.set(Calendar.MINUTE, 0);
212:                gtCal.set(Calendar.SECOND, 0);
213:                gtCal.set(Calendar.MILLISECOND, 0);
214:
215:                return gtCal.getTime();
216:            }
217:
218:            public static int getLastDayOfWeek(Calendar cal) {
219:                int firstDayOfWeek = cal.getFirstDayOfWeek();
220:
221:                if (firstDayOfWeek == Calendar.SUNDAY) {
222:                    return Calendar.SATURDAY;
223:                } else if (firstDayOfWeek == Calendar.MONDAY) {
224:                    return Calendar.SUNDAY;
225:                } else if (firstDayOfWeek == Calendar.TUESDAY) {
226:                    return Calendar.MONDAY;
227:                } else if (firstDayOfWeek == Calendar.WEDNESDAY) {
228:                    return Calendar.TUESDAY;
229:                } else if (firstDayOfWeek == Calendar.THURSDAY) {
230:                    return Calendar.WEDNESDAY;
231:                } else if (firstDayOfWeek == Calendar.FRIDAY) {
232:                    return Calendar.THURSDAY;
233:                }
234:
235:                return Calendar.FRIDAY;
236:            }
237:
238:            public static Date getLTDate(Calendar cal) {
239:                Calendar ltCal = (Calendar) cal.clone();
240:
241:                ltCal.set(Calendar.HOUR_OF_DAY, 23);
242:                ltCal.set(Calendar.MINUTE, 59);
243:                ltCal.set(Calendar.SECOND, 59);
244:                ltCal.set(Calendar.MILLISECOND, 999);
245:
246:                return ltCal.getTime();
247:            }
248:
249:            public static int[] getMonthIds() {
250:                return MONTH_IDS;
251:            }
252:
253:            public static String[] getMonths(Locale locale) {
254:                return getMonths(locale, null);
255:            }
256:
257:            public static String[] getMonths(Locale locale, String pattern) {
258:                if (Validator.isNull(pattern)) {
259:                    pattern = "MMMM";
260:                }
261:
262:                StringMaker sm = new StringMaker();
263:
264:                sm.append("months_");
265:                sm.append(pattern);
266:                sm.append("_");
267:                sm.append(locale.getLanguage());
268:                sm.append("_");
269:                sm.append(locale.getCountry());
270:
271:                String key = sm.toString();
272:
273:                String[] months = (String[]) _calendarPool.get(key);
274:
275:                if (months == null) {
276:                    months = new String[12];
277:
278:                    DateFormat monthFormat = new SimpleDateFormat(pattern,
279:                            locale);
280:
281:                    Calendar cal = CalendarFactoryUtil.getCalendar();
282:
283:                    cal.set(Calendar.DATE, 1);
284:
285:                    for (int i = 0; i < 12; i++) {
286:                        cal.set(Calendar.MONTH, i);
287:
288:                        months[i] = monthFormat.format(cal.getTime());
289:                    }
290:
291:                    _calendarPool.put(key, months);
292:                }
293:
294:                return months;
295:            }
296:
297:            public static Timestamp getTimestamp(Date date) {
298:                if (date == null) {
299:                    return null;
300:                } else {
301:                    return new Timestamp(date.getTime());
302:                }
303:            }
304:
305:            public static boolean isAfter(int month1, int day1, int year1,
306:                    int hour1, int minute1, int amPm1, int month2, int day2,
307:                    int year2, int hour2, int minute2, int amPm2,
308:                    TimeZone timeZone, Locale locale) {
309:
310:                Calendar cal1 = CalendarFactoryUtil.getCalendar(timeZone,
311:                        locale);
312:
313:                cal1.set(Calendar.MONTH, month1);
314:                cal1.set(Calendar.DATE, day1);
315:                cal1.set(Calendar.YEAR, year1);
316:                cal1.set(Calendar.HOUR, hour1);
317:                cal1.set(Calendar.MINUTE, minute1);
318:                cal1.set(Calendar.AM_PM, amPm1);
319:
320:                Calendar cal2 = CalendarFactoryUtil.getCalendar(timeZone,
321:                        locale);
322:
323:                cal2.set(Calendar.MONTH, month2);
324:                cal2.set(Calendar.DATE, day2);
325:                cal2.set(Calendar.YEAR, year2);
326:                cal2.set(Calendar.HOUR, hour2);
327:                cal2.set(Calendar.MINUTE, minute2);
328:                cal2.set(Calendar.AM_PM, amPm2);
329:
330:                return cal1.after(cal2);
331:            }
332:
333:            public static boolean isBroadcastDate(int month, int day, int year) {
334:                if (!isDate(month, day, year)) {
335:                    return false;
336:                }
337:
338:                Calendar cal1 = CalendarFactoryUtil.getCalendar();
339:
340:                cal1.setFirstDayOfWeek(Calendar.MONDAY);
341:                cal1.set(Calendar.MONTH, month);
342:                cal1.set(Calendar.DATE, day);
343:                cal1.set(Calendar.YEAR, year);
344:
345:                Calendar cal2 = CalendarFactoryUtil.getCalendar();
346:
347:                cal2.setFirstDayOfWeek(Calendar.MONDAY);
348:                cal2.set(Calendar.MONTH, month + 1);
349:                cal2.set(Calendar.DATE, 1);
350:                cal2.set(Calendar.YEAR, year);
351:
352:                if ((cal2.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY)
353:                        && (cal2.get(Calendar.WEEK_OF_YEAR) == cal1
354:                                .get(Calendar.WEEK_OF_YEAR))) {
355:
356:                    return false;
357:                }
358:
359:                return true;
360:            }
361:
362:            public static boolean isDate(int month, int day, int year) {
363:                return Validator.isDate(month, day, year);
364:            }
365:
366:            public static boolean isFuture(int month, int year) {
367:                return isFuture(month, year, TimeZoneUtil.getDefault(),
368:                        LocaleUtil.getDefault());
369:            }
370:
371:            public static boolean isFuture(int month, int year,
372:                    TimeZone timeZone, Locale locale) {
373:
374:                Calendar curCal = CalendarFactoryUtil.getCalendar(timeZone,
375:                        locale);
376:
377:                curCal.set(Calendar.DATE, 1);
378:
379:                Calendar cal = (Calendar) curCal.clone();
380:
381:                cal.set(Calendar.MONTH, month);
382:                cal.set(Calendar.YEAR, year);
383:
384:                return cal.after(curCal);
385:            }
386:
387:            public static boolean isFuture(int month, int day, int year) {
388:                return isFuture(month, day, year, TimeZoneUtil.getDefault(),
389:                        LocaleUtil.getDefault());
390:            }
391:
392:            public static boolean isFuture(int month, int day, int year,
393:                    TimeZone timeZone, Locale locale) {
394:
395:                Calendar curCal = CalendarFactoryUtil.getCalendar(timeZone,
396:                        locale);
397:
398:                Calendar cal = (Calendar) curCal.clone();
399:
400:                cal.set(Calendar.MONTH, month);
401:                cal.set(Calendar.DATE, day);
402:                cal.set(Calendar.YEAR, year);
403:
404:                return cal.after(curCal);
405:            }
406:
407:            public static boolean isFuture(int month, int day, int year,
408:                    int hour, int minute, int amPm) {
409:
410:                return isFuture(month, day, year, hour, minute, amPm,
411:                        TimeZoneUtil.getDefault(), LocaleUtil.getDefault());
412:            }
413:
414:            public static boolean isFuture(int month, int day, int year,
415:                    int hour, int minute, int amPm, TimeZone timeZone,
416:                    Locale locale) {
417:
418:                Calendar curCal = CalendarFactoryUtil.getCalendar(timeZone,
419:                        locale);
420:
421:                Calendar cal = (Calendar) curCal.clone();
422:
423:                cal.set(Calendar.MONTH, month);
424:                cal.set(Calendar.DATE, day);
425:                cal.set(Calendar.YEAR, year);
426:                cal.set(Calendar.HOUR, hour);
427:                cal.set(Calendar.MINUTE, minute);
428:                cal.set(Calendar.AM_PM, amPm);
429:
430:                return cal.after(curCal);
431:            }
432:
433:            public static boolean isGregorianDate(int month, int day, int year) {
434:                return Validator.isGregorianDate(month, day, year);
435:            }
436:
437:            public static boolean isJulianDate(int month, int day, int year) {
438:                return Validator.isJulianDate(month, day, year);
439:            }
440:
441:            public static Calendar roundByMinutes(Calendar cal, int interval) {
442:                int minutes = cal.get(Calendar.MINUTE);
443:
444:                int delta = 0;
445:
446:                if (minutes < interval) {
447:                    delta = interval - minutes;
448:                } else {
449:                    delta = interval - (minutes % interval);
450:                }
451:
452:                if (delta == interval) {
453:                    delta = 0;
454:                }
455:
456:                cal.add(Calendar.MINUTE, delta);
457:
458:                return cal;
459:            }
460:
461:            private static Map _calendarPool = CollectionFactory
462:                    .getSyncHashMap();
463:
464:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.