KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ebank > util > DateHelper


1 /*
2  * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2004 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27
28
29 package com.sun.ebank.util;
30
31 import java.util.*;
32 import java.text.SimpleDateFormat JavaDoc;
33
34
35 /**
36  * This class contains helper methods for dealing with
37  * Date objects.
38  */

39 public final class DateHelper {
40     public static final Date getDate(int year, int month, int day, int hour,
41         int minute) {
42         // returns a Date with the specified time elements
43
Calendar cal =
44             new GregorianCalendar(year, intToCalendarMonth(month), day, hour,
45                 minute);
46
47         return cal.getTime();
48     }
49      // getDate
50

51     public static final Date getDate(int year, int month, int day) {
52         // returns a Date with the specified time elements,
53
// with the hour and minutes both set to 0 (midnight)
54
Calendar cal =
55             new GregorianCalendar(year, intToCalendarMonth(month), day);
56
57         return cal.getTime();
58     }
59      // getDate
60

61     static public final Date addDays(Date target, int days) {
62         // returns a Date that is the sum of the target Date
63
// and the specified number of days;
64
// to subtract days from the target Date, the days
65
// argument should be negative
66
long msPerDay = 1000 * 60 * 60 * 24;
67         long msTarget = target.getTime();
68         long msSum = msTarget + (msPerDay * days);
69         Date result = new Date();
70         result.setTime(msSum);
71
72         return result;
73     }
74      // addDays
75

76     static public int dayDiff(Date first, Date second) {
77         // returns the difference, in days, between the first
78
// and second Date arguments
79
long msPerDay = 1000 * 60 * 60 * 24;
80         long diff =
81             (first.getTime() / msPerDay) - (second.getTime() / msPerDay);
82         Long JavaDoc convertLong = new Long JavaDoc(diff);
83
84         return convertLong.intValue();
85     }
86      // dayDiff
87

88     static public int getYear(Date date) {
89         Calendar cal = new GregorianCalendar();
90         cal.setTime(date);
91
92         return cal.get(Calendar.YEAR);
93     }
94      // getYear
95

96     static public int getMonth(Date date) {
97         Calendar cal = new GregorianCalendar();
98         cal.setTime(date);
99
100         int calendarMonth = cal.get(Calendar.MONTH);
101
102         return calendarMonthToInt(calendarMonth);
103     }
104      // getMonth
105

106     static public int getDay(Date date) {
107         Calendar cal = new GregorianCalendar();
108         cal.setTime(date);
109
110         return cal.get(Calendar.DAY_OF_MONTH);
111     }
112      // getDay
113

114     static public int getHour(Date date) {
115         Calendar cal = new GregorianCalendar();
116         cal.setTime(date);
117
118         return cal.get(Calendar.HOUR_OF_DAY);
119     }
120      // geHour
121

122     static public int getMinute(Date date) {
123         Calendar cal = new GregorianCalendar();
124         cal.setTime(date);
125
126         return cal.get(Calendar.MINUTE);
127     }
128      // geMinute
129

130     private static int calendarMonthToInt(int calendarMonth) {
131         if (calendarMonth == Calendar.JANUARY) {
132             return 1;
133         } else if (calendarMonth == Calendar.FEBRUARY) {
134             return 2;
135         } else if (calendarMonth == Calendar.MARCH) {
136             return 3;
137         } else if (calendarMonth == Calendar.APRIL) {
138             return 4;
139         } else if (calendarMonth == Calendar.MAY) {
140             return 5;
141         } else if (calendarMonth == Calendar.JUNE) {
142             return 6;
143         } else if (calendarMonth == Calendar.JULY) {
144             return 7;
145         } else if (calendarMonth == Calendar.AUGUST) {
146             return 8;
147         } else if (calendarMonth == Calendar.SEPTEMBER) {
148             return 9;
149         } else if (calendarMonth == Calendar.OCTOBER) {
150             return 10;
151         } else if (calendarMonth == Calendar.NOVEMBER) {
152             return 11;
153         } else if (calendarMonth == Calendar.DECEMBER) {
154             return 12;
155         } else {
156             return 1;
157         }
158     }
159      // calendarMonthToInt
160

161     public static String JavaDoc format(Date date, String JavaDoc pattern) {
162         // returns a String representation of the date argument,
163
// formatted according to the pattern argument, which
164
// has the same syntax as the argument of the SimpleDateFormat
165
// class1E
166
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(pattern);
167
168         return formatter.format(date);
169     }
170      // format
171

172     private static int intToCalendarMonth(int month) {
173         if (month == 1) {
174             return Calendar.JANUARY;
175         } else if (month == 2) {
176             return Calendar.FEBRUARY;
177         } else if (month == 3) {
178             return Calendar.MARCH;
179         } else if (month == 4) {
180             return Calendar.APRIL;
181         } else if (month == 5) {
182             return Calendar.MAY;
183         } else if (month == 6) {
184             return Calendar.JUNE;
185         } else if (month == 7) {
186             return Calendar.JULY;
187         } else if (month == 8) {
188             return Calendar.AUGUST;
189         } else if (month == 9) {
190             return Calendar.SEPTEMBER;
191         } else if (month == 10) {
192             return Calendar.OCTOBER;
193         } else if (month == 11) {
194             return Calendar.NOVEMBER;
195         } else if (month == 12) {
196             return Calendar.DECEMBER;
197         } else {
198             return Calendar.JANUARY;
199         }
200     }
201      // intToCalendarMonth
202
}
203  // class
204
Popular Tags