Source Code Cross Referenced for DateMathComparator.java in  » Profiler » JAMon » com » jamonapi » utils » 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 » Profiler » JAMon » com.jamonapi.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


01:        package com.jamonapi.utils;
02:
03:        import java.util.*;
04:
05:        /** Comparator that allows you to pass Calendar fields and a negative number for the number 
06:         * of this filed (i.e. hours/days) that a Date should not exceed.  Use fields like Calendar.DATE, HOUR_OF_DAY, DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_YEAR ETC.
07:         * .  Values to b used for dateToAdd in the constructor could be -7 for 7 days ago, or -24 for 24 hours ago depending on what 
08:         * was passed in the dateField.
09:         * 
10:         * @author steve souza
11:         *
12:         */
13:        public class DateMathComparator extends JAMonComparator {
14:            private int dateField;
15:            private int dateToAdd;
16:            /**Use fields like Calendar.DATE, HOUR_OF_DAY, DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_YEAR ETC.*/
17:            private Calendar calendar = new GregorianCalendar();
18:
19:            private static final boolean NATURAL_ORDER = true;
20:
21:            public DateMathComparator(int dateField, int dateToAdd) {
22:                super (NATURAL_ORDER);
23:                this .dateField = dateField;
24:                this .dateToAdd = dateToAdd;
25:            }
26:
27:            protected int compareThis(Object o1, Object o2) {
28:                int retVal = 0;
29:                if (o1 instanceof  Date && o2 instanceof  Date) {
30:                    Date d1 = (Date) o1;
31:                    Date d2 = (Date) o2;
32:
33:                    calendar.setTime(new Date());
34:                    calendar.add(dateField, dateToAdd);// i.e. todays date -7 days
35:                    Date dateAddValue = calendar.getTime();
36:
37:                    // 2 dates are equal if they are both above or below the Calendar field threshold
38:                    // else date1 is greater if it is greater than threshold and date2 isn't
39:                    // and date1 is less than the threshold than it is greater than date2.
40:                    int d1CompNum = d1.compareTo(dateAddValue);
41:                    int d2CompNum = d2.compareTo(dateAddValue);
42:
43:                    if (d1CompNum <= -1 && d2CompNum <= -1)
44:                        return 0;
45:                    else if (d1CompNum >= 1 && d2CompNum >= 1)
46:                        return 0;
47:                    else if (d1CompNum == 1)
48:                        return 1;
49:                    else if (d1CompNum == -1)
50:                        return -1;
51:                }
52:
53:                return retVal;
54:
55:            }
56:
57:            /** Test code */
58:            public static void main(String[] arg) {
59:                //7 DAYS
60:                //24 HOURS
61:                Date today = new Date();
62:
63:                Calendar calendar = new GregorianCalendar();
64:                calendar.setTime(today);
65:                calendar.add(Calendar.DAY_OF_YEAR, -8);
66:                Date minus8Days = calendar.getTime();
67:                System.out.println(new DateMathComparator(Calendar.DAY_OF_YEAR,
68:                        -7).compare(today, minus8Days));
69:
70:                calendar.setTime(today);
71:                calendar.add(Calendar.DAY_OF_YEAR, -6);
72:                Date minus6Days = calendar.getTime();
73:                System.out.println(new DateMathComparator(Calendar.DAY_OF_YEAR,
74:                        -7).compare(today, minus6Days));
75:
76:                calendar.setTime(today);
77:                calendar.add(Calendar.HOUR_OF_DAY, -6);
78:                Date minus6Hours = calendar.getTime();
79:                System.out.println(new DateMathComparator(Calendar.HOUR_OF_DAY,
80:                        -7).compare(today, minus6Hours));
81:
82:                calendar.setTime(today);
83:                calendar.add(Calendar.HOUR_OF_DAY, -8);
84:                Date minus8Hours = calendar.getTime();
85:                System.out.println(new DateMathComparator(Calendar.HOUR_OF_DAY,
86:                        -7).compare(today, minus8Hours));
87:
88:            }
89:
90:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.