68
|
1 /*
|
|
2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com>
|
|
3
|
|
4 Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 of this software and associated documentation files (the "Software"), to deal
|
|
6 in the Software without restriction, including without limitation the rights
|
|
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 copies of the Software, and to permit persons to whom the Software is
|
|
9 furnished to do so, subject to the following conditions:
|
|
10
|
|
11 The above copyright notice and this permission notice shall be included in
|
|
12 all copies or substantial portions of the Software.
|
|
13
|
|
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 THE SOFTWARE.
|
|
21 */
|
|
22
|
|
23 package fschmidt.util.java;
|
|
24
|
|
25 import java.util.Calendar;
|
|
26 import java.util.Date;
|
|
27
|
|
28
|
|
29 public class DateUtils {
|
|
30 private DateUtils() {} // never
|
|
31
|
|
32 public static Date roundToDay(Date date) {
|
|
33 Calendar cal = Calendar.getInstance();
|
|
34 cal.setLenient(false);
|
|
35 cal.setTime(date);
|
|
36 Calendar cal2 = Calendar.getInstance();
|
|
37 cal2.setLenient(false);
|
|
38 cal2.clear();
|
|
39 cal2.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DATE));
|
|
40 return cal2.getTime();
|
|
41 }
|
|
42
|
|
43 public static Date add(Date date,int days,int unit) {
|
|
44 Calendar cal = Calendar.getInstance();
|
|
45 cal.setLenient(false);
|
|
46 cal.setTime(date);
|
|
47 cal.add(unit,days);
|
|
48 return cal.getTime();
|
|
49 }
|
|
50
|
|
51 public static Date addDays(Date date,int days) {
|
|
52 return add(date,days,Calendar.DATE);
|
|
53 }
|
|
54
|
|
55 public static Date addHours(Date date,int hours) {
|
|
56 return add(date,hours,Calendar.HOUR);
|
|
57 }
|
|
58
|
|
59 private static final long millisPerHour = 1000L*60L*60L;
|
|
60 private static final long millisPerDay = millisPerHour*24L;
|
|
61
|
|
62 public static int daysBetween(Date dateFrom,Date dateTo) {
|
|
63 long from = dateFrom.getTime();
|
|
64 long to = dateTo.getTime();
|
|
65 long diff = (to - from)/millisPerDay;
|
|
66 return (int)diff;
|
|
67 }
|
|
68
|
|
69 public static int datesBetween(Date dateFrom,Date dateTo) {
|
|
70 long from = roundToDay(dateFrom).getTime();
|
|
71 long to = roundToDay(dateTo).getTime();
|
|
72 long diff = (to - from + millisPerHour)/millisPerDay;
|
|
73 return (int)diff;
|
|
74 }
|
|
75
|
|
76 public static Date max(Date date1,Date date2) {
|
|
77 return date1.after(date2) ? date1 : date2;
|
|
78 }
|
|
79
|
|
80 public static Date min(Date date1,Date date2) {
|
|
81 return date1.before(date2) ? date1 : date2;
|
|
82 }
|
|
83
|
|
84 public static boolean equalToTheSecond(Date date1,Date date2) {
|
|
85 return date1!=null && date2!=null && date1.getTime()/1000 == date2.getTime()/1000;
|
|
86 }
|
|
87
|
|
88
|
|
89 public static void main(String[] args) {
|
|
90 Date today = roundToDay(new Date());
|
|
91 Date date = today;
|
|
92 for( int i=0; i<10; i++ ) {
|
|
93 System.out.println(daysBetween(today,date));
|
|
94 date = addDays(date,1);
|
|
95 }
|
|
96 }
|
|
97 }
|