1 package smile.utils; 2 3 import java.util.*; 4 import java.text.*; 5 6 15 public class SmileDate { 16 public static final String 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 public static String dateToNum(String pstrValeur) throws ParseException { 48 49 Date date1 = null; 50 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.FRENCH); 51 String strDateToNum = pstrValeur; 52 53 try { 54 55 date1 = sdf.parse(strDateToNum); 57 sdf.applyPattern("yyyyMMdd"); 59 strDateToNum = sdf.format(date1); 60 61 } catch (Exception e) { 62 } 63 64 finally { 65 66 } 67 68 return strDateToNum; 69 70 } 71 72 public static String changeFormat(String pstrValeur, String pFormatOrigine, String pFormatDestination) throws ParseException { 79 80 Date date1 = null; 81 SimpleDateFormat sdf = new SimpleDateFormat(pFormatOrigine, Locale.FRENCH); 82 String strDateToNum = pstrValeur; 83 84 try { 85 86 date1 = sdf.parse(strDateToNum); 88 sdf.applyPattern(pFormatDestination); 90 strDateToNum = sdf.format(date1); 91 92 } catch (Exception e) { 93 } 94 95 finally { 96 97 } 98 99 return strDateToNum; 100 101 } 102 103 public static String dateToMySql(String pstrValeur) throws ParseException { 109 110 Date date1 = null; 111 115 116 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.FRENCH); 117 String strDateToNum = pstrValeur; 118 119 try { 120 121 date1 = sdf.parse(strDateToNum); 123 sdf.applyPattern("yyyy-MM-dd"); 125 strDateToNum = sdf.format(date1); 126 127 } catch (Exception e) { 128 } 129 130 finally { 131 134 } 135 136 return strDateToNum; 137 138 } 139 140 public static String numToDate(String pstrValeur) throws ParseException { 146 147 Date date1 = null; 148 Calendar cal = new GregorianCalendar(); 149 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.FRENCH); 150 String strnumToDate = pstrValeur; 151 String strTemp = pstrValeur; 152 153 try { 154 155 if (strnumToDate.length() == 6) { 157 158 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 date1 = sdf.parse(strTemp); 174 sdf.applyPattern("dd/MM/yyyy"); 176 strnumToDate = sdf.format(date1); 177 178 } catch (Exception e) { 179 } 180 181 finally { 182 183 } 184 185 return strnumToDate; 186 187 } 188 189 public static int diffDate(Date date1, Date date2, String pformat) throws java.text.ParseException { 197 198 String 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 sdf.applyPattern(pformat); 209 var1 = Integer.parseInt(sdf.format(date1)); 210 var2 = Integer.parseInt(sdf.format(date2)); 211 212 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 public static String today(String pFormat) { 241 242 SimpleDateFormat sdf = new SimpleDateFormat(pFormat); 243 return sdf.format(new java.util.Date (System.currentTimeMillis())); 244 245 } 246 247 public static String today() { 255 256 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 257 return sdf.format(new java.util.Date (System.currentTimeMillis())); 258 } 259 260 public static String currentDay() { 267 268 SimpleDateFormat sdf = new SimpleDateFormat("dd"); 269 return sdf.format(new java.util.Date (System.currentTimeMillis())); 270 } 271 272 public static String currentMonth() { 279 280 SimpleDateFormat sdf = new SimpleDateFormat("MM"); 281 return sdf.format(new java.util.Date (System.currentTimeMillis())); 282 } 283 284 public static String currentYear() { 291 292 SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); 293 return sdf.format(new java.util.Date (System.currentTimeMillis())); 294 } 295 296 } | Popular Tags |