KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > misc > Calendar


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oña, 107 1º2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.misc;
34
35 import com.knowgate.debug.DebugFile;
36
37 /**
38  * <p>Calendar localization functions</p>
39  * @author Sergio Montoro Ten
40  * @version 2.0
41  */

42
43 public class Calendar {
44
45   private static String JavaDoc WeekDayNamesES[] = { null, "domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado" };
46   private static String JavaDoc WeekDayNamesEN[] = { null, "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
47   private static String JavaDoc WeekDayNamesIT[] = { null, "Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato" };
48   private static String JavaDoc WeekDayNamesFR[] = { null, "Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi" };
49   private static String JavaDoc WeekDayNamesDE[] = { null, "Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag" };
50   private static String JavaDoc WeekDayNamesPT[] = { null, "Domingo", "Segunda-feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado" };
51
52   private static String JavaDoc MonthNamesES[] = { "Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre" };
53   private static String JavaDoc MonthNamesEN[] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
54   private static String JavaDoc MonthNamesIT[] = { "Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre" };
55   private static String JavaDoc MonthNamesFR[] = { "Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre" };
56   private static String JavaDoc MonthNamesDE[] = { "Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember" };
57   private static String JavaDoc MonthNamesPT[] = { "Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro" };
58
59   private static String JavaDoc MonthNamesRFC[] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
60
61   //-----------------------------------------------------------
62

63   /**
64    * Get translated week day name
65    * @param MyWeekDay [1=Sunday .. 7=Saturday]
66    * @param sLangId 2 characters language identifier (currently only { "en","es","it","fr","de" and "pt" } are supported)
67    * @return Week day name
68    */

69   public static String JavaDoc WeekDayName(int MyWeekDay, String JavaDoc sLangId) {
70
71     if (DebugFile.trace) {
72       DebugFile.writeln("Begin WeekDayName(" + String.valueOf(MyWeekDay) + "," + sLangId + ")");
73       DebugFile.incIdent();
74     }
75
76     String JavaDoc sRetVal;
77
78     if (MyWeekDay<1 || MyWeekDay>7)
79       throw new java.lang.IllegalArgumentException JavaDoc("Calendar.WeekDayName 1st parameter (MyWeekDay) is " + String.valueOf(MyWeekDay) + " but must be in the range [1..7]");
80     else {
81       if (null==sLangId)
82         throw new java.lang.IllegalArgumentException JavaDoc("Calendar.WeekDayName 2nd parameter (Language Id.) is null but must be one of {es,en}");
83       else if (sLangId.equalsIgnoreCase("es"))
84         sRetVal = WeekDayNamesES[MyWeekDay];
85       else if (sLangId.equalsIgnoreCase("en"))
86         sRetVal = WeekDayNamesEN[MyWeekDay];
87       else if (sLangId.equalsIgnoreCase("fr"))
88         sRetVal = WeekDayNamesFR[MyWeekDay];
89       else if (sLangId.equalsIgnoreCase("de"))
90         sRetVal = WeekDayNamesDE[MyWeekDay];
91       else if (sLangId.equalsIgnoreCase("it"))
92         sRetVal = WeekDayNamesIT[MyWeekDay];
93       else if (sLangId.equalsIgnoreCase("pt"))
94         sRetVal = WeekDayNamesPT[MyWeekDay];
95       else
96         throw new java.lang.IllegalArgumentException JavaDoc("Calendar.WeekDayName 2nd parameter (Language Id.) must be one of {es,en}");
97     }
98
99     if (DebugFile.trace) {
100       DebugFile.decIdent();
101       DebugFile.writeln("End WeekDayName() : " + sRetVal);
102     }
103
104     return sRetVal;
105   } // WeekDay
106

107   //-----------------------------------------------------------
108

109   /**
110    * Get translated month name
111    * @param MyMonth [0=January .. 11=December]
112    * @param sLangId sLangId 2 characters language identifier (currently only { "en","es","it","fr","de" and "pt" } are supported)
113    * @return Month Name
114    */

115   public static String JavaDoc MonthName(int MyMonth, String JavaDoc sLangId) {
116
117     if (DebugFile.trace) {
118       DebugFile.writeln("Begin MonthName(" + String.valueOf(MyMonth) + "," + sLangId + ")");
119       DebugFile.incIdent();
120     }
121
122     String JavaDoc sRetVal;
123
124     if (MyMonth<0 || MyMonth>11)
125       throw new java.lang.IllegalArgumentException JavaDoc("Calendar.MonthName 1st parameter (MyMonth) is " + String.valueOf(MyMonth) + " but must be in the range [0..11]");
126     else {
127       if (null==sLangId)
128         throw new java.lang.IllegalArgumentException JavaDoc("Calendar.MonthName 2nd parameter (Language Id.) is null but must be one of {es,en}");
129       else if (sLangId.equalsIgnoreCase("es"))
130         sRetVal = MonthNamesES[MyMonth];
131       else if (sLangId.equalsIgnoreCase("en"))
132         sRetVal = MonthNamesEN[MyMonth];
133       else if (sLangId.equalsIgnoreCase("fr"))
134         sRetVal = MonthNamesFR[MyMonth];
135       else if (sLangId.equalsIgnoreCase("it"))
136         sRetVal = MonthNamesIT[MyMonth];
137       else if (sLangId.equalsIgnoreCase("de"))
138         sRetVal = MonthNamesDE[MyMonth];
139       else if (sLangId.equalsIgnoreCase("pt"))
140         sRetVal = MonthNamesPT[MyMonth];
141       else
142         throw new java.lang.IllegalArgumentException JavaDoc("Calendar.WeekDayName 2nd parameter (Language Id.) must be one of {es,en}");
143     }
144
145     if (DebugFile.trace) {
146       DebugFile.decIdent();
147       DebugFile.writeln("End MonthName() : " + sRetVal);
148     }
149
150     return sRetVal;
151   } // MonthName
152

153   //-----------------------------------------------------------
154

155   /**
156    * Get Month Last Day
157    * @param MyMonth [0=January .. 11=December]
158    * @param MyYear 4 digits year
159    * @return the last day of the month. Takes into account leap years
160    */

161   public static int LastDay(int MyMonth, int MyYear) {
162
163     if (MyMonth<0 || MyMonth>11)
164       throw new java.lang.IllegalArgumentException JavaDoc("Calendar.LastDay 1st parameter (MyMonth) is " + String.valueOf(MyMonth) + " but must be in the range [0..11]");
165
166     if (MyYear<1000 || MyYear>9999)
167       throw new java.lang.IllegalArgumentException JavaDoc("Calendar.LastDay 2nd parameter (MyYear) is " + String.valueOf(MyYear) + " but must be in the range [1000..9999]");
168
169     switch(MyMonth) {
170       case 0:
171       case 2:
172       case 4:
173       case 6:
174       case 7:
175       case 9:
176       case 11:
177         return 31;
178       case 3:
179       case 5:
180       case 8:
181       case 10:
182         return 30;
183       case 1:
184         return ( (MyYear%400==0) || ((MyYear%4==0) && (MyYear%100!=0)) ) ? 29 : 28;
185     } // end switch()
186
return 0;
187   } // LastDay()
188

189   //-----------------------------------------------------------
190
}
Popular Tags