Source Code Cross Referenced for DateTools.java in  » Development » rapla » org » rapla » components » 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 » Development » rapla » org.rapla.components.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*--------------------------------------------------------------------------*
002:        | Copyright (C) 2006 Gereon Fassbender, Christopher Kohlhaas               |
003:        |                                                                          |
004:        | This program is free software; you can redistribute it and/or modify     |
005:        | it under the terms of the GNU General Public License as published by the |
006:        | Free Software Foundation. A copy of the license has been included with   |
007:        | these distribution in the COPYING file, if not go to www.fsf.org         |
008:        |                                                                          |
009:        | As a special exception, you are granted the permissions to link this     |
010:        | program with every library, which license fulfills the Open Source       |
011:        | Definition as published by the Open Source Initiative (OSI).             |
012:         *--------------------------------------------------------------------------*/
013:        package org.rapla.components.util;
014:
015:        import java.util.Calendar;
016:        import java.util.Date;
017:        import java.util.TimeZone;
018:
019:        /** Tools for manipulating dates.
020:         * At the moment of writing rapla internaly stores all appointments
021:         * in the GMT timezone.
022:         */
023:        public abstract class DateTools {
024:            public static final int DAYS_PER_WEEK = 7;
025:            public static final long MILLISECONDS_PER_MINUTE = 1000 * 60;
026:            public static final long MILLISECONDS_PER_HOUR = MILLISECONDS_PER_MINUTE * 60;
027:            public static final long MILLISECONDS_PER_DAY = 24 * MILLISECONDS_PER_HOUR;
028:            public static final long MILLISECONDS_PER_WEEK = 7 * MILLISECONDS_PER_DAY;
029:            public static TimeZone GMT = TimeZone.getTimeZone("GMT+0");
030:
031:            public static int getHourOfDay(long date) {
032:                return (int) ((date % MILLISECONDS_PER_DAY) / MILLISECONDS_PER_HOUR);
033:            }
034:
035:            public static int getMinuteOfHour(long date) {
036:                return (int) ((date % MILLISECONDS_PER_HOUR) / MILLISECONDS_PER_MINUTE);
037:            }
038:
039:            /** sets time of day to 0:00.
040:                @see #cutDate(Date)
041:             */
042:            public static long cutDate(long date) {
043:                return (date - (date % MILLISECONDS_PER_DAY));
044:            }
045:
046:            public static boolean isMidnight(long date) {
047:                return cutDate(date) == date;
048:            }
049:
050:            public static boolean isMidnight(Date date) {
051:                return isMidnight(date.getTime());
052:            }
053:
054:            /** sets time of day to 0:00.
055:                @see #cutDate(Date)
056:             */
057:            public static void cutDate(Calendar calendar) {
058:                calendar.set(Calendar.HOUR_OF_DAY, 0);
059:                calendar.set(Calendar.MINUTE, 0);
060:                calendar.set(Calendar.SECOND, 0);
061:                calendar.set(Calendar.MILLISECOND, 0);
062:            }
063:
064:            /** sets time of day to 0:00. */
065:            public static Date cutDate(Date date) {
066:                return new Date(cutDate(date.getTime()));
067:            }
068:
069:            static TimeZone timeZone = TimeZone.getTimeZone("GMT");
070:
071:            /** same as TimeZone.getTimeZone("GMT"). */
072:            public static TimeZone getTimeZone() {
073:                return timeZone;
074:            }
075:
076:            /** sets time of day to 0:00 and increases day.
077:                @see #fillDate(Date)
078:             */
079:            public static long fillDate(long date) {
080:                // cut date
081:                long cuttedDate = (date - (date % MILLISECONDS_PER_DAY));
082:                return cuttedDate + MILLISECONDS_PER_DAY;
083:            }
084:
085:            public static Date fillDate(Date date) {
086:                return new Date(fillDate(date.getTime()));
087:            }
088:
089:            /** Monday 24:00 = tuesday 0:00.
090:                But the first means end of monday and the second start of tuesday.
091:                The default DateFormat always displays tuesday.
092:                If you want to edit the first interpretation in calendar components.
093:                call addDay() to add 1 day to the given date before displaying
094:                and subDay() for mapping a day back after editing.
095:                @see #subDay
096:                @see #addDays
097:             */
098:            public static Date addDay(Date date) {
099:                return new Date(date.getTime() + MILLISECONDS_PER_DAY);
100:            }
101:
102:            /** see #addDay*/
103:            public static Date addDays(Date date, int days) {
104:                return new Date(date.getTime() + MILLISECONDS_PER_DAY * days);
105:            }
106:
107:            /**
108:                @see #addDay
109:                @see #subDays
110:             */
111:            public static Date subDay(Date date) {
112:                return new Date(date.getTime() - MILLISECONDS_PER_DAY);
113:            }
114:
115:            /**
116:                @see #addDay
117:             */
118:            public static Date subDays(Date date, int days) {
119:                return new Date(date.getTime() - MILLISECONDS_PER_DAY * days);
120:            }
121:
122:            /** returns if the two dates are one the same date.
123:             * Dates must be in GMT */
124:            static public boolean isSameDay(long d1, long d2) {
125:                return cutDate(d1) == cutDate(d2);
126:            }
127:
128:            /** uses the calendar-object for date comparison.
129:             * Use this for non GMT Dates*/
130:            static public boolean isSameDay(Calendar calendar, Date d1, Date d2) {
131:                calendar.setTime(d1);
132:                int era1 = calendar.get(Calendar.ERA);
133:                int year1 = calendar.get(Calendar.YEAR);
134:                int day_of_year1 = calendar.get(Calendar.DAY_OF_YEAR);
135:                calendar.setTime(d2);
136:                int era2 = calendar.get(Calendar.ERA);
137:                int year2 = calendar.get(Calendar.YEAR);
138:                int day_of_year2 = calendar.get(Calendar.DAY_OF_YEAR);
139:                return (era1 == era2 && year1 == year2 && day_of_year1 == day_of_year2);
140:            }
141:
142:            static public long countDays(Date start, Date end) {
143:                return (cutDate(end.getTime()) - cutDate(start.getTime()))
144:                        / MILLISECONDS_PER_DAY;
145:            }
146:
147:            static public Calendar createGMTCalendar() {
148:                return Calendar.getInstance(GMT);
149:            }
150:
151:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.