1 2 3 4

advertisement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Demo for: Custom-Made Classes
DateAssistant.class */
package wu.andy;
import
import
import
import
import
import
java.util.GregorianCalendar;
java.util.Locale;
java.util.Date;
java.util.TimeZone;
java.text.DateFormat;
java.text.SimpleDateFormat;
public class DateAssistant
{
public static GregorianCalendar convertDateString(String dateString)
{
/* This method converts an String argument that is in the "mm/dd/yyyy"
format into a GregorianCalendar object. */
// Declare the GregorianCalendar object to be returned
GregorianCalendar gc;
// To call the constructor of GregorianCalendar with month, day, and
// year arguments, three int variables are needed.
String monthString, dayString, yearString;
int month, day, year;
/* The substring method of the String class returns part of a String as
a "substring". The charaters included in the substring are determined
by their indexes in the original string, an index being the position of
the character.
For example: in the date string
character:
m | m | / | d | d | / | y | y | y | y
position:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
The substring() method is overloaded. One version takes two position
indexes as arguments: the first one marks the start position of the
substring and the scond, ending. The tricky part is that the first
index is inclusive whereas the second is exclusive. Another version
takes only one position index as argument. In that case, this index
marks the start position of the substring, and it will run all the way
to tne end of the original string.
*/
// Call the substring() method with two arguments. Since the month part
// is the first two characters of the date string, the start and ending
// indexes are 0 and 2.
E:/BCIS 3680/10e-customclasses/DateAssistant.java
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
monthString = dateString.substring(0, 2);
month = Integer.parseInt(monthString);
//month = Integer.parseInt(dateString.substring(0,2)); // better
// Get the day part.
dayString = dateString.substring(3, 5);
day = Integer.parseInt(dayString);
// Get the year part. We may use the other version with one argument.
yearString = dateString.substring(6);
year = Integer.parseInt(yearString);
//
//
//
gc
Now we have all three arguments for calling one of the constructors
of the GregorianCalendar class. Note that the GregorianCalendar class
counts months from 0 (i.e., January = Month 0; December = Month 11).
= new GregorianCalendar(year, month - 1, day);
return gc;
}
public static String formatDate(GregorianCalendar gc, int loc)
{
/* This method takes a GregorianCalendar object that represents a given
point in time and displays it in a format that is determined by one
of three locales: the United States, the United Kingdom, and Japan.
To specify which local to use, pass an integer as the second
argument:
1 - US, 2 - UK, 3 - Japan. */
// Variables
String dateDisplay;
// Convert the GregorianCalendar object to Date object. This is
// necessary because the format() method of the DateFormat class takes
// a Date object as the argument.
Date d = gc.getTime();
/* The DateFormat class formats dates and times in a locale-specific way.
It is an abstract class and thus cannot be instantiated directly. So,
Unlike the DecimalFormat class, you don't create a "DateFormat"
object by calling its constructor - DateFormat df = new DateFormat();
Instead, you call one of a number of static methods to create a
particular instance.
--------------------------------------------------------------------if you want to format...
then call this method...
---------------------------------------------------------------------
E:/BCIS 3680/10e-customclasses/DateAssistant.java
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
date only
getDateInstance()
time only
getTimeInstance()
date and time (long format)
getDateTimeInstance()
date and time (short format)
getInstance()
--------------------------------------------------------------------However, at this point, the local for date display is not set yet.
The display style of the DateFormat instances can be controlled by
the arguments you pass to the method.
For the first argument, you pass one of the few constants that are
defined in the DateFormat class; it controls the format used on the
date portion:
* DateFormat.FULL ( = 0 ):
Tuesday, January 1, 2011
* DateFormat.LONG ( = 1 ):
January 1, 2011
* DateFormat.MEDIUM ( = 2 ):
January 1, 2011
* DateFormat.SHORT ( = 3 ):
1/1/11
* DateFormat.DEFAULT (default is MEDIUM)
The second argument is again one of those constants, such as
DateFormat.FULL). However, when passed as the second argument, it
controls the format used on the time portion instead.
The last argument controls the locale, which is a java.util.Locale
object.
*/
// Control how the date will be formatted based on locale speified by
// the user.
DateFormat df = null;
// loc is the int the user enters: 1 - US, 2 - UK, 3
switch (loc)
{
case 1:
df = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.SHORT,
break;
case 2:
df = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.SHORT,
break;
case 3:
df = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.SHORT,
break;
}
// Create the display string
E:/BCIS 3680/10e-customclasses/DateAssistant.java
- Japan
Locale.US);
Locale.UK);
Locale.JAPAN);
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
dateDisplay = df.format(d);
// Return date display string
return dateDisplay;
}
public static String formatDate(GregorianCalendar gc, String zone)
{
/* This method takes a GregorianCalendar object that represents a given
point in time and displays it in a format that is determined by time
zones:
Hawaii, Alaska, Pacific, Mountain, Central, Eastern
To specify which time zone to use, pass one of the above six strings
as the second argument when calling this method.
*/
// Variables
String dateDisplay;
TimeZone tz;
// Convert the GregorianCalendar object to Date object.
Date d = gc.getTime();
// Create the DateFormat object and set date display in U.S. style
DateFormat df = null;
df = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.SHORT, Locale.US);
//
//
//
tz
Set default time zone as Central
This is a catch-all option so that if the user doesn't enter a string
that matches any of the six below, the display is set as Central
= TimeZone.getTimeZone("America/Chicago");
// Adjust the time zone for date display based on the zone names
// passed into this method
if ( zone.equalsIgnoreCase("Hawaii") )
{
tz = TimeZone.getTimeZone("Pacific/Honolulu");
}
else if ( zone.equalsIgnoreCase("Alaska") )
{
tz = TimeZone.getTimeZone("America/Anchorage");
}
else if ( zone.equalsIgnoreCase("Pacific") )
{
tz = TimeZone.getTimeZone("America/Los_Angeles");
}
else if ( zone.equalsIgnoreCase("Mountain") )
E:/BCIS 3680/10e-customclasses/DateAssistant.java
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
{
tz = TimeZone.getTimeZone("America/Denver");
}
else if ( zone.equalsIgnoreCase("Central") )
{
tz = TimeZone.getTimeZone("America/Chicago");
}
else if ( zone.equalsIgnoreCase("Eastern") )
{
tz = TimeZone.getTimeZone("America/New_York");
}
// Actaully set time zone for the DateFormat object
df.setTimeZone(tz);
// Create the display string
dateDisplay = df.format(d);
// Return date display string
return dateDisplay;
}
public static int calcInterval(String date1, String date2)
{
/* This method takes two dates in the "mm/dd/yyyy" format and
calculates the number of days between them. */
// Variables
int interval;
GregorianCalendar gc1, gc2;
long time1, time2;
// Convert date strings to GregorianCalendar objects
gc1 = convertDateString(date1);
gc2 = convertDateString(date2);
/* The getTimeInMillis() is an example of the methods the
GregorianCalendar class inherits from the Calendar class. Similar
to the Date class, it represents the number of milliseconds from the
"epoch" (midnight, GMT, Jan. 1, 1970) as a long value. This is handy
for:
1. Comparing two dates, or
2. Calculating the time interval between two dates.
We use it for both in this method.
*/
// Convert GregorianCalendar objects to longs
time1 = gc1.getTimeInMillis();
E:/BCIS 3680/10e-customclasses/DateAssistant.java
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
time2 = gc2.getTimeInMillis();
// Calculate time interval in days
// 1 day = 24 hours * 60 mins * 60 seconds * 1000 millisecs
interval = (int) ((Math.max(time1, time2) - Math.min(time1, time2))
/ (24 * 60 * 60 * 1000));
return interval;
}
public static String calcNewDate(String startDate, long interval,
boolean forward)
{
// String variable to hold calculation result
String targetDateString = "";
// Other variables needed in the calculation
GregorianCalendar gcStart;
long startTime, targetTime;
Date targetDate;
// SimpleDateFormat object to format the new date; the "MM/dd/yyyy"
// string defines the formatting style. Note that the month part is "MM"
// rather than "mm" because the latter, if included in the style, will
// display the minutes field in the Date object
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
// Convert start date into GC object
gcStart = convertDateString(startDate);
// ***** Date Arithmetics *****
// Convert start date into milliseconds since the "epoch"
startTime = gcStart.getTimeInMillis();
// Convert the desired interval into milliseconds
long intervalInMillis = interval * 24 * 60 * 60 * 1000;
// Add or substract the desired interval
// Get future date if it is forward calculation
if ( forward )
{
targetTime = startTime + intervalInMillis;
}
// Get past date if it is not forward calculation
else
{
targetTime = startTime - intervalInMillis;
}
// The result of calculation still is the number of milliseconds between
E:/BCIS 3680/10e-customclasses/DateAssistant.java
283
284
285
286
287
288
289
290
291
292 }
// the "epoch" and the new date; So convert it into a Date object
targetDate = new Date(targetTime);
// Format the Date object
targetDateString = sdf.format(targetDate);
// Return the resultant date in "mm/dd/yyyy" format
return targetDateString;
}
E:/BCIS 3680/10e-customclasses/DateAssistant.java
Download