26 |
|
|
27 |
What is needed for Calendar? |
What is needed for Calendar? |
28 |
|
|
29 |
-Coordinating system: |
-Coordinating system:: |
30 |
|
|
31 |
....... |
....... |
32 |
---------- . ----.------- |
---------- . ----.------- |
33 |
| Screen | <---- mapping <--- . | Ja.uary | |
| Screen | <---- mapping <--- . | Ja.uary | |
50 |
-Use cpu wisely and draw only needed parts. |
-Use cpu wisely and draw only needed parts. |
51 |
|
|
52 |
|
|
53 |
-Format/layout? |
-Format/layout? :: |
54 |
|
|
55 |
|---| |
|---| |
56 |
| | |
| | |
57 |
|---| -----------...------------- |
|---| -----------...------------- |
58 |
| | | | | | | | | |
| | | | | | | | | |
59 |
|---| -----------...------------- |
|---| -----------...------------- |
60 |
| | horizontal |
| | horizontal |
61 |
|---| |
|---| |
62 |
. . |
. . |
63 |
. . |
. . |
64 |
. . ----------------- |
. . ----------------- |
65 |
|---| | | | | | |
|---| | | | | | |
66 |
| | ----------------- |
| | ----------------- |
67 |
|---| | | | | | |
|---| | | | | | |
68 |
----------------- |
----------------- |
69 |
vertical | | | | | |
vertical | | | | | |
70 |
----------------- |
----------------- |
71 |
matrix |
matrix |
72 |
|
|
73 |
|
|
74 |
New Object |
New Object |
80 |
-Weekdays: string[7] |
-Weekdays: string[7] |
81 |
|
|
82 |
-these must be countable: |
-these must be countable: |
83 |
-first weekday of the Month |
-first weekday of the Month :: |
84 |
_______________ |
|
85 |
|___________|_| /|\ |
_______________ |
86 |
|_|_|_|_|_|_|_| | |
|___________|_| /|\ |
87 |
|_|_|_|_|_|_|_| -height (4...6) |
|_|_|_|_|_|_|_| | |
88 |
|_|_|_|_|_|_|_| | |
|_|_|_|_|_|_|_| -height (4...6) |
89 |
|_|_|_|_|_|_|_| | |
|_|_|_|_|_|_|_| | |
90 |
|_|_|_________| \|/ |
|_|_|_|_|_|_|_| | |
91 |
|
|_|_|_________| \|/ |
92 |
|
|
93 |
-also days per month is needed |
-also days per month is needed |
94 |
|
|
102 |
TextPrototype |
TextPrototype |
103 |
------------- |
------------- |
104 |
|
|
105 |
|
Code:: |
106 |
|
|
107 |
import java.util.*; |
import java.util.*; |
|
|
|
|
public class TextCalendar { |
|
|
static void p(String s) { System.out.print(s); } |
|
|
static void pln(String s) { System.out.println(s); } |
|
|
|
|
|
// notice common factor, 12 |
|
|
private int weeks_in_month[] = new int[12]; |
|
|
private int empty_before_first_day[] = new int[12]; |
|
|
private int days_in_month[] = new int[12]; |
|
|
|
|
|
|
|
|
public TextCalendar() { |
|
|
|
|
|
// get the supported ids for GMT-08:00 (Pacific Standard Time) |
|
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); |
|
|
|
|
|
// if no ids were returned, something is wrong. get out. |
|
|
if (ids.length == 0) |
|
|
System.exit(0); |
|
108 |
|
|
109 |
// create a Pacific Standard Time time zone |
public class TextCalendar { |
110 |
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]); |
static void p(String s) { System.out.print(s); } |
111 |
|
static void pln(String s) { System.out.println(s); } |
112 |
|
|
113 |
|
// notice common factor, 12 |
114 |
|
private int weeks_in_month[] = new int[12]; |
115 |
|
private int empty_before_first_day[] = new int[12]; |
116 |
|
private int days_in_month[] = new int[12]; |
117 |
|
|
118 |
// set up rules for daylight savings time |
|
119 |
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); |
public TextCalendar() { |
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); |
|
|
Calendar calendar = new GregorianCalendar(pdt); |
|
|
calendar.setFirstDayOfWeek(Calendar.MONDAY); |
|
120 |
|
|
121 |
Date date = new Date(); |
// get the supported ids for GMT-08:00 (Pacific Standard Time) |
122 |
date.setYear(calendar.YEAR); |
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); |
123 |
|
|
124 |
pln(" * Year: " + calendar.get(calendar.YEAR)); |
// if no ids were returned, something is wrong. get out. |
125 |
|
if (ids.length == 0) |
126 |
|
System.exit(0); |
127 |
|
|
128 |
for (int month = 0; month < 12; month++) { |
// create a Pacific Standard Time time zone |
129 |
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]); |
130 |
|
|
131 |
// set first day of month |
// set up rules for daylight savings time |
132 |
date.setDate(1); |
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); |
133 |
date.setMonth(month); |
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); |
134 |
calendar.setTime(date); |
Calendar calendar = new GregorianCalendar(pdt); |
135 |
|
calendar.setFirstDayOfWeek(Calendar.MONDAY); |
136 |
|
|
137 |
// print month |
Date date = new Date(); |
138 |
p("\n" + |
date.setYear(calendar.YEAR); |
|
monthStr(calendar.get(Calendar.MONTH)) + |
|
|
" " + (calendar.get(Calendar.MONTH)+1) + |
|
|
"\n "); |
|
|
|
|
|
// count empty day at the beginning of the month. |
|
|
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { |
|
|
empty_before_first_day[month] = 6; |
|
|
} else { |
|
|
empty_before_first_day[month] = |
|
|
calendar.get(Calendar.DAY_OF_WEEK) - 2; |
|
|
} |
|
139 |
|
|
140 |
// print empty spaces at the beginning of month if needed.. |
pln(" * Year: " + calendar.get(calendar.YEAR)); |
|
for (int i=0; i< empty_before_first_day[month]; i++) { |
|
|
p(" "); |
|
|
} |
|
141 |
|
|
142 |
|
for (int month = 0; month < 12; month++) { |
143 |
|
|
144 |
// generate one month a day by day |
// set first day of month |
145 |
for (int day=1; day<33; day++) { |
date.setDate(1); |
146 |
date.setDate(day); |
date.setMonth(month); |
147 |
calendar.setTime(date); |
calendar.setTime(date); |
148 |
|
|
149 |
// over-month checking |
// print month |
150 |
if (date.getDate() < day) { |
p("\n" + |
151 |
days_in_month[month] = day - 1; |
monthStr(calendar.get(Calendar.MONTH)) + |
152 |
break; |
" " + (calendar.get(Calendar.MONTH)+1) + |
153 |
|
"\n "); |
154 |
|
|
155 |
|
// count empty day at the beginning of the month. |
156 |
|
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { |
157 |
|
empty_before_first_day[month] = 6; |
158 |
|
} else { |
159 |
|
empty_before_first_day[month] = |
160 |
|
calendar.get(Calendar.DAY_OF_WEEK) - 2; |
161 |
} |
} |
162 |
|
|
163 |
// print weekday and date |
// print empty spaces at the beginning of month if needed.. |
164 |
p(dayStr(calendar.get(Calendar.DAY_OF_WEEK))+ |
for (int i=0; i< empty_before_first_day[month]; i++) { |
165 |
" " + |
p(" "); |
166 |
calendar.get(Calendar.DATE) + ", " |
} |
|
); |
|
167 |
|
|
168 |
// if sunday, print "\n" and increment weeks |
|
169 |
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { |
// generate one month a day by day |
170 |
p("\n "); |
for (int day=1; day<33; day++) { |
171 |
weeks_in_month[month] += 1; |
date.setDate(day); |
172 |
|
calendar.setTime(date); |
173 |
|
|
174 |
|
// over-month checking |
175 |
|
if (date.getDate() < day) { |
176 |
|
days_in_month[month] = day - 1; |
177 |
|
break; |
178 |
|
} |
179 |
|
|
180 |
|
// print weekday and date |
181 |
|
p(dayStr(calendar.get(Calendar.DAY_OF_WEEK))+ |
182 |
|
" " + |
183 |
|
calendar.get(Calendar.DATE) + ", " |
184 |
|
); |
185 |
|
|
186 |
|
// if sunday, print "\n" and increment weeks |
187 |
|
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { |
188 |
|
p("\n "); |
189 |
|
weeks_in_month[month] += 1; |
190 |
|
} |
191 |
} |
} |
192 |
|
pln("\n ** Weeks: " + weeks_in_month[month] + |
193 |
|
", " + empty_before_first_day[month] + |
194 |
|
" - empty days at the beginning of the month, " + |
195 |
|
days_in_month[month] +" days in month"); |
196 |
} |
} |
|
pln("\n ** Weeks: " + weeks_in_month[month] + |
|
|
", " + empty_before_first_day[month] + |
|
|
" - empty days at the beginning of the month, " + |
|
|
days_in_month[month] +" days in month"); |
|
|
} |
|
197 |
|
|
198 |
} |
} |
199 |
|
|
200 |
|
|
201 |
private String dayStr(int day) { |
private String dayStr(int day) { |
202 |
|
|
203 |
if ( day < Calendar.SUNDAY || day > Calendar.SATURDAY ) return "ERROR"; |
if ( day < Calendar.SUNDAY || day > Calendar.SATURDAY ) return "ERROR"; |
204 |
|
|
205 |
switch(day) { |
switch(day) { |
206 |
case Calendar.MONDAY: return "Ma"; |
case Calendar.MONDAY: return "Ma"; |
207 |
case Calendar.TUESDAY: return "Ti"; |
case Calendar.TUESDAY: return "Ti"; |
208 |
case Calendar.WEDNESDAY: return "Ke"; |
case Calendar.WEDNESDAY: return "Ke"; |
209 |
case Calendar.THURSDAY: return "To"; |
case Calendar.THURSDAY: return "To"; |
210 |
case Calendar.FRIDAY: return "Pe"; |
case Calendar.FRIDAY: return "Pe"; |
211 |
case Calendar.SATURDAY: return "La"; |
case Calendar.SATURDAY: return "La"; |
212 |
case Calendar.SUNDAY: return "Su"; |
case Calendar.SUNDAY: return "Su"; |
213 |
} |
} |
214 |
return "Error"; |
return "Error"; |
215 |
} |
} |
216 |
|
|
217 |
private String monthStr(int month) { |
private String monthStr(int month) { |
218 |
if ( month < 0 || month > 11) return "ERROR"; |
if ( month < 0 || month > 11) return "ERROR"; |
219 |
|
|
220 |
String ret =""; |
String ret =""; |
221 |
|
|
222 |
switch(month) { |
switch(month) { |
223 |
case 0: ret = "Tammi"; break; |
case 0: ret = "Tammi"; break; |
224 |
case 1: ret = "Helmi"; break; |
case 1: ret = "Helmi"; break; |
225 |
case 2: ret = "Maalis"; break; |
case 2: ret = "Maalis"; break; |
226 |
case 3: ret = "Huhti"; break; |
case 3: ret = "Huhti"; break; |
227 |
case 4: ret = "Touko"; break; |
case 4: ret = "Touko"; break; |
228 |
case 5: ret = "Kesä"; break; |
case 5: ret = "Kesä"; break; |
229 |
case 6: ret = "Heinä"; break; |
case 6: ret = "Heinä"; break; |
230 |
case 7: ret = "Elo"; break; |
case 7: ret = "Elo"; break; |
231 |
case 8: ret = "Syys"; break; |
case 8: ret = "Syys"; break; |
232 |
case 9: ret = "Loka"; break; |
case 9: ret = "Loka"; break; |
233 |
case 10: ret = "Marras"; break; |
case 10: ret = "Marras"; break; |
234 |
case 11: ret = "Joulu"; break; |
case 11: ret = "Joulu"; break; |
235 |
|
} |
236 |
|
return ret + "kuu"; |
237 |
} |
} |
|
return ret + "kuu"; |
|
|
} |
|
238 |
|
|
239 |
|
|
240 |
public static void main(String [] arg) { |
public static void main(String [] arg) { |
241 |
TextCalendar c = new TextCalendar(); |
TextCalendar c = new TextCalendar(); |
242 |
|
} |
243 |
} |
} |
|
} |
|
244 |
|
|
245 |
|
|
246 |
|
|