Source Code Cross Referenced for DateUtils.java in  » IDE » tIDE » snow » 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 » IDE » tIDE » snow.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package snow.utils;
002:
003:        import java.text.SimpleDateFormat;
004:        import java.util.GregorianCalendar;
005:        import java.util.Calendar;
006:        import java.text.DecimalFormat;
007:
008:        public final class DateUtils {
009:            static final DecimalFormat format0 = new DecimalFormat("0.0");
010:
011:            private DateUtils() {
012:            }
013:
014:            /** For differences between months until millis.
015:             *  @param diff in millis
016:             */
017:            public static final String formatTimeDifference(long diff) {
018:                return formatTimeDifference(diff, false);
019:            }
020:
021:            private static long cachedMidnight = 0;
022:
023:            public synchronized static long getMidnightTodayMorning() {
024:                // SPEEDUP 40 times
025:                long dt = System.currentTimeMillis() - cachedMidnight;
026:                if (dt >= 0 && dt < 24L * 3600 * 1000) {
027:                    // still valid
028:                    return cachedMidnight;
029:                }
030:
031:                // no more valid => recompute
032:                // speed: 170000 per second.
033:                cachedMidnight = getStartOfDayForTime(System
034:                        .currentTimeMillis());
035:                return cachedMidnight;
036:            }
037:
038:            public synchronized static long getStartOfDayForTime(long time) {
039:
040:                Calendar cal = GregorianCalendar.getInstance();
041:                cal.setTimeInMillis(time);
042:                cal.set(Calendar.HOUR_OF_DAY, 0);
043:                cal.set(Calendar.MINUTE, 0);
044:                cal.set(Calendar.SECOND, 0);
045:                cal.set(Calendar.MILLISECOND, 0);
046:
047:                return cal.getTimeInMillis();
048:            }
049:
050:            public static boolean isToday(long t) {
051:                long md = getMidnightTodayMorning();
052:                if (t - md >= 0 && t - md < 24L * 3600 * 1000)
053:                    return true;
054:                return false;
055:            }
056:
057:            /** For differences between months until millis.
058:             *  @param diff in millis
059:             */
060:            public static final String formatTimeDifference(long diff,
061:                    boolean notGreaterMultiplierThanHours) {
062:                long adiff = Math.abs(diff);
063:                if (adiff < 1000L)
064:                    return "" + diff + " ms";
065:                if (adiff < 1000L * 60) {
066:                    double ds = diff / 1000.0;
067:                    return roundInt(ds) + " s";
068:                }
069:                if (adiff < 1000L * 3600) {
070:                    double dm = diff / 1000.0 / 60.0;
071:                    if (dm < 2) {
072:                        return format0.format(dm) + " m";
073:                    } else {
074:                        return roundInt(dm) + " m";
075:                    }
076:                }
077:                if (notGreaterMultiplierThanHours || adiff < 1000L * 3600 * 24) {
078:                    return format0.format(diff / 1000.0 / 3600.0) + " h";
079:                }
080:                if (adiff < 1000L * 3600 * 24 * 30) // THE L IS MANDATORY !!! (overpass int range !!!!!!!!!!!)
081:                {
082:                    double dd = diff / 1000.0 / 3600.0 / 24.0;
083:                    if (dd < 2) {
084:                        return format0.format(dd) + " day";
085:                    } else {
086:                        int rid = roundInt(dd);
087:                        return rid + " day" + (rid != 1 ? "s" : "");
088:                    }
089:                }
090:                if (adiff < 1000L * 3600 * 24 * 30 * 13) {
091:                    double md = diff / 1000.0 / 3600.0 / 24.0 / 30;
092:                    if (md < 1.5) {
093:                        return format0.format(md) + " month";
094:                    } else {
095:                        int rim = roundInt(md);
096:                        return rim + " month" + (rim != 1 ? "s" : "");
097:                    }
098:                }
099:                double yd = diff / 1000.0 / 3600 / 24 / 30 / 12;
100:                if (yd < 1.5) {
101:                    return format0.format(yd) + " year";
102:                } else {
103:                    int riy = roundInt(yd);
104:                    return "" + riy + " year" + (riy != 1 ? "s" : "");
105:                }
106:            }
107:
108:            public static SimpleDateFormat dateFormatMDY = new SimpleDateFormat(
109:                    "MMM dd yyyy");
110:            public static SimpleDateFormat dateFormatMDYhm = new SimpleDateFormat(
111:                    "MMM dd yyyy  HH:mm");
112:            public static SimpleDateFormat dateFormatMDYhmFileName = new SimpleDateFormat(
113:                    "MMM dd yyyy  HH.mm");// without :
114:            public static SimpleDateFormat timeFormatHHMM = new SimpleDateFormat(
115:                    "HH:mm");
116:            public static final long oneDayMs = 1000L * 3600 * 24;
117:
118:            public static String formatDate(long time) {
119:                return dateFormatMDY.format(time);
120:            }
121:
122:            /** Fixed format. No special "Today" case.
123:             */
124:            public static String formatDateAndTimeFix(long time) {
125:                return dateFormatMDYhm.format(time);
126:            }
127:
128:            /** Writes today for today, ...
129:             */
130:            public static String formatDateAndTimeHuman(long time) {
131:                if (isToday(time))
132:                    return "Today " + timeFormatHHMM.format(time);
133:                if (isToday(time + oneDayMs))
134:                    return "Yesterday " + timeFormatHHMM.format(time);
135:                if (isToday(time - oneDayMs))
136:                    return "Tomorrow " + timeFormatHHMM.format(time);
137:                return dateFormatMDYhm.format(time);
138:            }
139:
140:            public static void main(String[] arguments) {
141:                formatDate(System.currentTimeMillis());
142:            }
143:
144:            private static int roundInt(double d) {
145:                return (int) Math.round(d);
146:            }
147:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.