KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smile > utils > SmileDate


1 package smile.utils;
2
3 import java.util.*;
4 import java.text.*;
5
6 /**
7  * SmileDate is a date utility class used for MySQL
8  *
9  * Copyright 2002 Smile Les motoristes Internet http://www.smile.fr/ Contact
10  * cofax@smile.fr for further information
11  *
12  * @author Smile Les motoristes Internet
13  *
14  */

15 public class SmileDate {
16     public static final String JavaDoc RCS_ID = "$Header: /cvsroot/cofax/cofax/src/smile/utils/SmileDate.java,v 1.2.2.1 2006/12/11 16:33:18 fxrobin Exp $";
17
18     public static Calendar firstDayOfMonth() {
19         Date date = new Date();
20         Calendar cal = new GregorianCalendar();
21         cal.setTime(date);
22         cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_MONTH) + 1);
23         return cal;
24     }
25
26     public static Calendar lastDayOfMonth() {
27         Date date = new Date();
28         Calendar cal = new GregorianCalendar();
29         cal.setTime(date);
30         cal.add(Calendar.MONTH, 1);
31         cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_MONTH));
32         return cal;
33     }
34
35     public static int getMonth() {
36         Date date = new Date();
37         Calendar cal = new GregorianCalendar();
38         cal.setTime(date);
39         return Calendar.MONTH;
40     }
41
42     // --------------------------------------------------------------------------------
43
// Passage d'une date (DD/MM/YYYY) en nombre (YYYYMMDD)
44
// Paramètres : - Date ou nombre
45
// Retour : Nombre (String)
46
// --------------------------------------------------------------------------------
47
public static String JavaDoc dateToNum(String JavaDoc pstrValeur) throws ParseException {
48
49         Date date1 = null;
50         SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.FRENCH);
51         String JavaDoc strDateToNum = pstrValeur;
52
53         try {
54
55             // on convertit la chaine en date
56
date1 = sdf.parse(strDateToNum);
57             // on change le format de la date
58
sdf.applyPattern("yyyyMMdd");
59             strDateToNum = sdf.format(date1);
60
61         } catch (Exception JavaDoc e) {
62         }
63
64         finally {
65
66         }
67
68         return strDateToNum;
69
70     }
71
72     // --------------------------------------------------------------------------------
73
// Passage d'une date d'un format à un autre
74
// Paramètres : - Date ou nombre, format de la date à convertir, format
75
// final
76
// Retour : Nombre (String)
77
// --------------------------------------------------------------------------------
78
public static String JavaDoc changeFormat(String JavaDoc pstrValeur, String JavaDoc pFormatOrigine, String JavaDoc pFormatDestination) throws ParseException {
79
80         Date date1 = null;
81         SimpleDateFormat sdf = new SimpleDateFormat(pFormatOrigine, Locale.FRENCH);
82         String JavaDoc strDateToNum = pstrValeur;
83
84         try {
85
86             // on convertit la chaine en date
87
date1 = sdf.parse(strDateToNum);
88             // on change le format de la date
89
sdf.applyPattern(pFormatDestination);
90             strDateToNum = sdf.format(date1);
91
92         } catch (Exception JavaDoc e) {
93         }
94
95         finally {
96
97         }
98
99         return strDateToNum;
100
101     }
102
103     // --------------------------------------------------------------------------------
104
// Passage d'une date (DD/MM/YYYY) en String de type Date MySql (YYYY-MM-DD)
105
// Paramètres : - Date ou nombre
106
// Retour : Nombre (String)
107
// --------------------------------------------------------------------------------
108
public static String JavaDoc dateToMySql(String JavaDoc pstrValeur) throws ParseException {
109
110         Date date1 = null;
111         /**
112          * rem AM : dans l'application Cofax, le format de date est MM/dd/yyyy,
113          * et non pas dd/MM/yyyy
114          */

115
116         SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.FRENCH);
117         String JavaDoc strDateToNum = pstrValeur;
118
119         try {
120
121             // on convertit la chaine en date
122
date1 = sdf.parse(strDateToNum);
123             // on change le format de la date
124
sdf.applyPattern("yyyy-MM-dd");
125             strDateToNum = sdf.format(date1);
126
127         } catch (Exception JavaDoc e) {
128         }
129
130         finally {
131             // System.err.println("SmileDate.dateToMySQL : " + pstrValeur + " ->
132
// " + strDateToNum);
133

134         }
135
136         return strDateToNum;
137
138     }
139
140     // --------------------------------------------------------------------------------
141
// Passage d'un nombre (YYYYMMDD) en une date (DD/MM/YYYY)
142
// Paramètres : - Nombre
143
// Retour : Date (String)
144
// --------------------------------------------------------------------------------
145
public static String JavaDoc numToDate(String JavaDoc pstrValeur) throws ParseException {
146
147         Date date1 = null;
148         Calendar cal = new GregorianCalendar();
149         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.FRENCH);
150         String JavaDoc strnumToDate = pstrValeur;
151         String JavaDoc strTemp = pstrValeur;
152
153         try {
154
155             // Cas d'une date en numérique sur 6 caractères AAMMJJ
156
if (strnumToDate.length() == 6) {
157
158                 // Si le premier caractère est < 3 alors on ajoute le siècle
159
// "20" sinon on ajoute "19"
160
if (Integer.parseInt(strnumToDate.substring(0, 1)) < 3) {
161
162                     strTemp = "20" + strnumToDate;
163
164                 } else {
165
166                     strTemp = "19" + strnumToDate;
167
168                 }
169
170             }
171
172             // on converti la chaine en date
173
date1 = sdf.parse(strTemp);
174             // on change le format de la date
175
sdf.applyPattern("dd/MM/yyyy");
176             strnumToDate = sdf.format(date1);
177
178         } catch (Exception JavaDoc e) {
179         }
180
181         finally {
182
183         }
184
185         return strnumToDate;
186
187     }
188
189     // --------------------------------------------------------------------------------
190
// Différence entre 2 dates
191
// Paramètres : - Date 1
192
// - Date 2
193
// - Format à renvoyer : "D" (jour), "M" (mois), "yyyy" (année)
194
// Retour : Différence (integer)
195
// --------------------------------------------------------------------------------
196
public static int diffDate(Date date1, Date date2, String JavaDoc pformat) throws java.text.ParseException JavaDoc {
197
198         String JavaDoc FORMAT_DATE = "dd/MM/yyyy";
199         SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE, Locale.FRENCH);
200         int resultat = 0;
201         int var1 = 0;
202         int var2 = 0;
203         int annee1 = 0;
204         int annee2 = 0;
205         Date date3112;
206
207         // Calcul l'indice des dates au sein de leur année :
208
sdf.applyPattern(pformat);
209         var1 = Integer.parseInt(sdf.format(date1));
210         var2 = Integer.parseInt(sdf.format(date2));
211
212         // Prends en compte la difference d'année :
213
sdf.applyPattern("yyyy");
214         annee1 = Integer.parseInt(sdf.format(date1));
215         annee2 = Integer.parseInt(sdf.format(date2));
216
217         for (int i = annee1; i < annee2; i++) {
218
219             sdf.applyPattern(FORMAT_DATE);
220             date3112 = sdf.parse("31/12/" + i);
221
222             sdf.applyPattern(pformat);
223             resultat += Integer.parseInt(sdf.format(date3112));
224
225         }
226
227         resultat += var2 - var1;
228
229         return resultat;
230
231     }
232
233     // --------------------------------------------------------------------------------
234
// Date du jour sous forme de chaîne de caractères
235
// Paramètres :
236
// - Format à renvoyer : "D" (jour), "M" (mois), "yyyy" (année)
237
//
238
// Retour : String représentant la date du jour
239
// --------------------------------------------------------------------------------
240
public static String JavaDoc today(String JavaDoc pFormat) {
241
242         SimpleDateFormat sdf = new SimpleDateFormat(pFormat);
243         return sdf.format(new java.util.Date JavaDoc(System.currentTimeMillis()));
244
245     }
246
247     // --------------------------------------------------------------------------------
248
// Date du jour sous forme de chaîne de caractères au format "dd/MM/yyyy"
249
// Paramètres :
250
// - Format à renvoyer : "D" (jour), "M" (mois), "yyyy" (année)
251
//
252
// Retour : String représentant la date du jour au format "dd/MM/yyyy"
253
// --------------------------------------------------------------------------------
254
public static String JavaDoc today() {
255
256         SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
257         return sdf.format(new java.util.Date JavaDoc(System.currentTimeMillis()));
258     }
259
260     // --------------------------------------------------------------------------------
261
// Jour en cours sous forme de chaîne de caractères
262
// Paramètres :
263
//
264
// Retour : String représentant le jour du mois
265
// --------------------------------------------------------------------------------
266
public static String JavaDoc currentDay() {
267
268         SimpleDateFormat sdf = new SimpleDateFormat("dd");
269         return sdf.format(new java.util.Date JavaDoc(System.currentTimeMillis()));
270     }
271
272     // --------------------------------------------------------------------------------
273
// Mois en cours sous forme de chaîne de caractères
274
// Paramètres :
275
//
276
// Retour : String représentant le mois en cours
277
// --------------------------------------------------------------------------------
278
public static String JavaDoc currentMonth() {
279
280         SimpleDateFormat sdf = new SimpleDateFormat("MM");
281         return sdf.format(new java.util.Date JavaDoc(System.currentTimeMillis()));
282     }
283
284     // --------------------------------------------------------------------------------
285
// Année en cours sous forme de chaîne de caractères
286
// Paramètres :
287
//
288
// Retour : String représentant l'année en cours
289
// --------------------------------------------------------------------------------
290
public static String JavaDoc currentYear() {
291
292         SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
293         return sdf.format(new java.util.Date JavaDoc(System.currentTimeMillis()));
294     }
295
296 }
Popular Tags