001: /*
002: * Calendar.java
003: *
004: *
005: * Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
006: * All rights reserved.
007: *
008: * This package is written by Lars Andersen <lars@rimfaxe.com>
009: * and licensed by Rimfaxe ApS.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by Rimfaxe ApS
026: (www.rimfaxe.com)"
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
031: * "Rimfaxe WebServer" must not be used to endorse or promote products
032: * derived from this software without prior written permission. For written
033: * permission, please contact info@rimfaxe.com
034: *
035: * 5. Products derived from this software may not be called "Rimfaxe"
036: * nor may "Rimfaxe" appear in their names without prior written
037: * permission of the Rimfaxe ApS.
038: *
039: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
040: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
043: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
044: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
045: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
046: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
047: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
048: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: *
051: */
052:
053: package com.rimfaxe.util;
054:
055: import java.util.*;
056:
057: /**
058: *
059: * @author Lars Andersen
060: * @version
061: */
062: public class Calendar extends GregorianCalendar {
063:
064: public static TimeZone TZ = TimeZone
065: .getTimeZone("Europe/Copenhagen");
066:
067: /** Creates new calendar */
068: public Calendar() {
069: super (com.rimfaxe.util.Calendar.TZ, Locale.ENGLISH);
070: }
071:
072: /** Creates new calendar */
073: public Calendar(int timezone) {
074: this .setTimeZone(new SimpleTimeZone(timezone, "CUSTOM"));
075: }
076:
077: /** Creates new calendar */
078: public Calendar(int timezone, long millis) {
079: this .setTimeZone(new SimpleTimeZone(timezone, "CUSTOM"));
080: this .setTimeInMillis(millis);
081: }
082:
083: /** Creates new calendar */
084: public Calendar(String in) {
085: System.out
086: .println("NOTE -> Not compatible with GCJ Calendar(String)");
087: this .setTime(java.sql.Timestamp.valueOf(in));
088: }
089:
090: /** Creates new calendar */
091: public Calendar(String in, String format) {
092: try {
093: if (format.equalsIgnoreCase("sql")) {
094:
095: StringTokenizer tkz = new StringTokenizer(in, " ",
096: false);
097: String datestr = tkz.nextToken();
098: String timestr = tkz.nextToken();
099:
100: tkz = new StringTokenizer(datestr, "-", false);
101: String y = tkz.nextToken();
102: String m = tkz.nextToken();
103: String d = tkz.nextToken();
104:
105: Integer year = new Integer(y);
106: this .set(YEAR, year.intValue());
107: Integer month = new Integer(m);
108: this .set(MONTH, month.intValue() - 1);
109: Integer day = new Integer(d);
110: this .set(this .DATE, day.intValue());
111:
112: tkz = new StringTokenizer(timestr, ":", false);
113: String hour_s = tkz.nextToken();
114: String minute_s = tkz.nextToken();
115: String second_s = tkz.nextToken();
116:
117: Integer hour = new Integer(hour_s);
118: this .set(HOUR_OF_DAY, hour.intValue());
119: Integer minute = new Integer(minute_s);
120: this .set(MINUTE, minute.intValue());
121: Integer second = new Integer(second_s);
122: this .set(SECOND, second.intValue());
123:
124: }
125: } catch (Exception e) {
126: // TODO
127: }
128: }
129:
130: public Calendar(java.sql.Timestamp ts) {
131: this .setTime(ts);
132: }
133:
134: public Calendar(java.sql.Date ts) {
135: this .setTime(ts);
136: }
137:
138: public Calendar(java.util.Date ts) {
139: this .setTime(ts);
140: }
141:
142: public static void setDefaultTimeZone(TimeZone tz) {
143: TZ = tz;
144: }
145:
146: public void setSqlTimestamp(java.sql.Timestamp ts) {
147: this .setTime(ts);
148: }
149:
150: public void setSqlDate(java.sql.Date ts) {
151: this .setTime(ts);
152: }
153:
154: public long getTimeInMillisSpecial() {
155: return this .getTimeInMillis();
156: }
157:
158: // It should look like this : Sun, 06 Nov 1994 08:49:37 GMT
159: public String getHttpDate() {
160: StringBuffer sb = new StringBuffer();
161: switch (this .get(this .DAY_OF_WEEK)) {
162: case Calendar.MONDAY:
163: sb.append("Mon");
164: break;
165: case Calendar.TUESDAY:
166: sb.append("Tue");
167: break;
168: case Calendar.WEDNESDAY:
169: sb.append("Wed");
170: break;
171: case Calendar.THURSDAY:
172: sb.append("Thu");
173: break;
174: case Calendar.FRIDAY:
175: sb.append("Fri");
176: break;
177: case Calendar.SATURDAY:
178: sb.append("Sat");
179: break;
180: case Calendar.SUNDAY:
181: sb.append("Sun");
182: break;
183: }
184: sb.append(", ");
185:
186: String year = "2000";
187: String day = "01";
188:
189: year = "" + this .get(this .YEAR);
190: int d = this .get(this .DATE);
191: if (d < 10)
192: day = "0" + d;
193: else
194: day = "" + d;
195:
196: sb.append(day + " ");
197:
198: switch (this .get(this .MONTH)) {
199: case Calendar.JANUARY:
200: sb.append("Jan");
201: break;
202: case Calendar.FEBRUARY:
203: sb.append("Feb");
204: break;
205: case Calendar.MARCH:
206: sb.append("Mar");
207: break;
208: case Calendar.APRIL:
209: sb.append("Apr");
210: break;
211: case Calendar.MAY:
212: sb.append("May");
213: break;
214: case Calendar.JUNE:
215: sb.append("Jun");
216: break;
217: case Calendar.JULY:
218: sb.append("Jul");
219: break;
220: case Calendar.AUGUST:
221: sb.append("Aug");
222: break;
223: case Calendar.SEPTEMBER:
224: sb.append("Sep");
225: break;
226: case Calendar.OCTOBER:
227: sb.append("Oct");
228: break;
229: case Calendar.NOVEMBER:
230: sb.append("Nov");
231: break;
232: case Calendar.DECEMBER:
233: sb.append("Dec");
234: break;
235: }
236: sb.append(" " + year + " " + getPrettyTime() + " GMT");
237:
238: return "" + sb;
239: }
240:
241: // It should look like this : Sun, 06-03-1994 08:49:37 GMT
242: public String getCookieDate() {
243: StringBuffer sb = new StringBuffer();
244: switch (this .get(this .DAY_OF_WEEK)) {
245: case Calendar.MONDAY:
246: sb.append("Mon");
247: break;
248: case Calendar.TUESDAY:
249: sb.append("Tue");
250: break;
251: case Calendar.WEDNESDAY:
252: sb.append("Wed");
253: break;
254: case Calendar.THURSDAY:
255: sb.append("Thu");
256: break;
257: case Calendar.FRIDAY:
258: sb.append("Fri");
259: break;
260: case Calendar.SATURDAY:
261: sb.append("Sat");
262: break;
263: case Calendar.SUNDAY:
264: sb.append("Sun");
265: break;
266: }
267: sb.append(", ");
268:
269: String year = "2000";
270: String day = "01";
271: String month = "01";
272: year = "" + this .get(this .YEAR);
273: int d = this .get(this .DATE);
274: if (d < 10)
275: day = "0" + d;
276: else
277: day = "" + d;
278: int m = this .get(this .MONTH);
279: if (m < 10)
280: month = "0" + m;
281: else
282: month = "" + m;
283:
284: sb.append(day + ":" + month + ":");
285:
286: sb.append("" + year + " " + getPrettyTime() + " GMT");
287:
288: return "" + sb;
289: }
290:
291: // It should look like this : Sun, 06/03 1994 08:49
292: public String getPrintDateUK() {
293: StringBuffer sb = new StringBuffer();
294: switch (this .get(this .DAY_OF_WEEK)) {
295: case Calendar.MONDAY:
296: sb.append("Mon");
297: break;
298: case Calendar.TUESDAY:
299: sb.append("Tue");
300: break;
301: case Calendar.WEDNESDAY:
302: sb.append("Wed");
303: break;
304: case Calendar.THURSDAY:
305: sb.append("Thu");
306: break;
307: case Calendar.FRIDAY:
308: sb.append("Fri");
309: break;
310: case Calendar.SATURDAY:
311: sb.append("Sat");
312: break;
313: case Calendar.SUNDAY:
314: sb.append("Sun");
315: break;
316: }
317: sb.append(", ");
318:
319: String year = "2000";
320: String day = "01";
321: String month = "01";
322: year = "" + this .get(this .YEAR);
323: int d = this .get(this .DATE);
324: if (d < 10)
325: day = "0" + d;
326: else
327: day = "" + d;
328: int m = this .get(this .MONTH) + 1;
329: if (m < 10)
330: month = "0" + m;
331: else
332: month = "" + m;
333:
334: sb.append(day + "/" + month + " ");
335:
336: sb.append("" + year + " " + getPrettyTime2());
337:
338: return "" + sb;
339: }
340:
341: public String getTimestamp() {
342: String year = "2000";
343: String month = "01";
344: String day = "01";
345:
346: year = "" + this .get(this .YEAR);
347: int m = this .get(this .MONTH) + 1;
348: if (m < 10)
349: month = "0" + m;
350: else
351: month = "" + m;
352: int d = this .get(this .DATE);
353: if (d < 10)
354: day = "0" + d;
355: else
356: day = "" + d;
357:
358: return new String("" + year + "-" + month + "-" + day + " "
359: + this .getPrettyTime());
360: }
361:
362: public String getSqlTimestamp() {
363: int month = this .get(this .MONTH) + 1;
364: return new String("TIMESTAMP'" + this .get(this .YEAR) + "-"
365: + month + "-" + this .get(this .DATE) + " "
366: + this .get(this .HOUR_OF_DAY) + ":"
367: + this .get(this .MINUTE) + ":" + this .get(this .SECOND)
368: + "'");
369: }
370:
371: public String getSqlDate() {
372: String year = "2000";
373: String month = "01";
374: String day = "01";
375:
376: year = "" + this .get(this .YEAR);
377: int m = this .get(this .MONTH) + 1;
378: if (m < 10)
379: month = "0" + m;
380: else
381: month = "" + m;
382: int d = this .get(this .DATE);
383: if (d < 10)
384: day = "0" + d;
385: else
386: day = "" + d;
387:
388: return new String("'" + year + "-" + month + "-" + day + "'");
389: }
390:
391: public String getSqlSmalldatetime() {
392: String year = "2000";
393: String month = "01";
394: String day = "01";
395:
396: year = "" + this .get(this .YEAR);
397: int m = this .get(this .MONTH) + 1;
398: if (m < 10)
399: month = "0" + m;
400: else
401: month = "" + m;
402: int d = this .get(this .DATE);
403: if (d < 10)
404: day = "0" + d;
405: else
406: day = "" + d;
407:
408: return new String("'" + year + "-" + month + "-" + day + " "
409: + this .getPrettyTime() + "'");
410: }
411:
412: public String getMonthName(int m) {
413: switch (m) {
414: case Calendar.JANUARY:
415: return "januar";
416: case Calendar.FEBRUARY:
417: return "februar";
418: case Calendar.MARCH:
419: return "marts";
420: case Calendar.APRIL:
421: return "april";
422: case Calendar.MAY:
423: return "maj";
424: case Calendar.JUNE:
425: return "juni";
426: case Calendar.JULY:
427: return "juli";
428: case Calendar.AUGUST:
429: return "august";
430: case Calendar.SEPTEMBER:
431: return "september";
432: case Calendar.OCTOBER:
433: return "oktober";
434: case Calendar.NOVEMBER:
435: return "november";
436: case Calendar.DECEMBER:
437: return "december";
438: }
439: return "januar";
440: }
441:
442: public int getMonthValue(String m) {
443: if (m.equalsIgnoreCase("januar")) {
444: return Calendar.JANUARY;
445: } else if (m.equalsIgnoreCase("februar")) {
446: return Calendar.FEBRUARY;
447: } else if (m.equalsIgnoreCase("marts")) {
448: return Calendar.MARCH;
449: } else if (m.equalsIgnoreCase("april")) {
450: return Calendar.APRIL;
451: } else if (m.equalsIgnoreCase("maj")) {
452: return Calendar.MAY;
453: } else if (m.equalsIgnoreCase("juni")) {
454: return Calendar.JUNE;
455: } else if (m.equalsIgnoreCase("juli")) {
456: return Calendar.JULY;
457: } else if (m.equalsIgnoreCase("august")) {
458: return Calendar.AUGUST;
459: } else if (m.equalsIgnoreCase("september")) {
460: return Calendar.SEPTEMBER;
461: } else if (m.equalsIgnoreCase("oktober")) {
462: return Calendar.OCTOBER;
463: } else if (m.equalsIgnoreCase("november")) {
464: return Calendar.NOVEMBER;
465: } else if (m.equalsIgnoreCase("december")) {
466: return Calendar.DECEMBER;
467: } else {
468: return Calendar.JANUARY;
469: }
470: }
471:
472: public String getPrintDateDK() {
473: StringBuffer sb = new StringBuffer();
474:
475: sb.append("" + this .get(this .DATE));
476: sb.append(". ");
477: switch (this .get(this .MONTH)) {
478: case Calendar.JANUARY:
479: sb.append("januar ");
480: break;
481: case Calendar.FEBRUARY:
482: sb.append("februar ");
483: break;
484: case Calendar.MARCH:
485: sb.append("marts ");
486: break;
487: case Calendar.APRIL:
488: sb.append("april ");
489: break;
490: case Calendar.MAY:
491: sb.append("maj ");
492: break;
493: case Calendar.JUNE:
494: sb.append("juni ");
495: break;
496: case Calendar.JULY:
497: sb.append("juli ");
498: break;
499: case Calendar.AUGUST:
500: sb.append("august ");
501: break;
502: case Calendar.SEPTEMBER:
503: sb.append("september ");
504: break;
505: case Calendar.OCTOBER:
506: sb.append("oktober ");
507: break;
508: case Calendar.NOVEMBER:
509: sb.append("november ");
510: break;
511: case Calendar.DECEMBER:
512: sb.append("december ");
513: break;
514: }
515: sb.append(this .get(this .YEAR));
516: return sb.toString();
517: }
518:
519: public String getPrintDateSimpleDK() {
520: java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
521: "dd.MM.yy");
522: return sdf.format(this .getTime());
523:
524: }
525:
526: public String getWeek() {
527: StringBuffer buf = new StringBuffer();
528: int w = getWeekNumber();
529: if (w < 10)
530: buf.append("0" + w);
531: else
532: buf.append("" + w);
533: return buf.toString();
534: }
535:
536: public String getPrettyTime() {
537: StringBuffer buf = new StringBuffer();
538:
539: int hour = this .get(this .HOUR_OF_DAY);
540: int minute = this .get(this .MINUTE);
541: int second = this .get(this .SECOND);
542:
543: if (hour < 10)
544: buf.append("0" + hour);
545: else
546: buf.append("" + hour);
547:
548: buf.append(":");
549:
550: if (minute < 10)
551: buf.append("0" + minute);
552: else
553: buf.append("" + minute);
554:
555: buf.append(":");
556:
557: if (second < 10)
558: buf.append("0" + second);
559: else
560: buf.append("" + second);
561:
562: return buf.toString();
563: }
564:
565: public String getPrettyTime2() {
566: StringBuffer buf = new StringBuffer();
567:
568: int hour = this .get(this .HOUR_OF_DAY);
569: int minute = this .get(this .MINUTE);
570: //int second = this.get(this.SECOND);
571:
572: if (hour < 10)
573: buf.append("0" + hour);
574: else
575: buf.append("" + hour);
576:
577: buf.append(":");
578:
579: if (minute < 10)
580: buf.append("0" + minute);
581: else
582: buf.append("" + minute);
583:
584: //buf.append(":");
585:
586: //if (second<10)
587: // buf.append("0"+second);
588: //else
589: // buf.append(""+second);
590:
591: return buf.toString();
592: }
593:
594: public String getYear() {
595: int y = this .get(this .YEAR);
596: return "" + y;
597: }
598:
599: public String getMonth() {
600: StringBuffer buf = new StringBuffer();
601: int m = this .get(this .MONTH) + 1;
602: if (m < 10)
603: buf.append("0" + m);
604: else
605: buf.append("" + m);
606: return buf.toString();
607: }
608:
609: public String getDate() {
610: StringBuffer buf = new StringBuffer();
611: int d = this .get(this .DATE);
612: if (d < 10)
613: buf.append("0" + d);
614: else
615: buf.append("" + d);
616: return buf.toString();
617: }
618:
619: public int getWeekNumber() {
620: com.rimfaxe.util.Calendar x = new com.rimfaxe.util.Calendar();
621: x.set(x.YEAR, this .get(this .YEAR));
622: x.set(x.MONTH, this .get(this .MONTH));
623: x.set(x.DATE, this .get(this .DATE));
624:
625: if (x.get(x.DAY_OF_WEEK) == 1)
626: x.add(x.WEEK_OF_YEAR, -1);
627:
628: x.set(x.DAY_OF_WEEK, 5);
629:
630: int count = 1;
631: boolean sameyear = true;
632: int oldyear = x.get(x.YEAR);
633:
634: while (sameyear) {
635: x.add(x.WEEK_OF_YEAR, -1);
636: if (x.get(x.YEAR) == oldyear)
637: count++;
638: else
639: sameyear = false;
640: }
641:
642: return count;
643: }
644: }
|