1 import java.text.SimpleDateFormat; 2 import java.util.ArrayList; 3 import java.util.Calendar; 4 import java.util.Date; 5 import java.util.List; 6 7 /**時間 日期轉換類 8 * @author DELL 9 * 10 */ 11 public class UtilsData { 12 13 14 /** 15 * 通用類 16 * 17 * @author root 18 */ 19 20 private static SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd"); 21 private static SimpleDateFormat datetime = new SimpleDateFormat( 22 "yyyy-MM-dd HH:mm:ss"); 23 public static Date getNowDate(){ 24 return new Date(); 25 } 26 // ****************************當前時間相關**************************** 27 /** 28 * 得到以 yyyy-MM-dd 爲形式的當前時間字符串 29 * 30 * @return String 31 */ 32 public static String getCurrentTimeByDay() { 33 String time = date.format(new Date(System.currentTimeMillis())); 34 return time; 35 } 36 37 /** 38 * 得到以 yyyy-MM-dd HH:mm:ss 爲形式的當前時間字符串 39 * 40 * @return String 41 */ 42 public static String getCurrentTimeBySecond() { 43 String time = datetime.format(new Date(System.currentTimeMillis())); 44 return time; 45 } 46 47 /** 48 * 得到給定格式的當前時間字符串 49 * 50 * @param give 51 * String 給定的時間格式 52 * @return String 53 */ 54 public static String getCurrentTime(String give) { 55 SimpleDateFormat temp = new SimpleDateFormat(give); 56 return temp.format(new Date(System.currentTimeMillis())); 57 } 58 59 // ****************************String轉換爲Date**************************** 60 /** 61 * 將String轉化成date 62 * 63 * @throws ParseException 64 * */ 65 public static Date pStringToDate(String str, String sfgs) 66 throws Exception { 67 SimpleDateFormat sf = new SimpleDateFormat(sfgs); 68 return sf.parse(str); 69 } 70 71 /** 72 * 將String轉化成date 格式爲yyyy-MM-dd hh:mm:ss 73 * 74 * @throws ParseException 75 * */ 76 public static Date pStringToDate(String str) throws Exception { 77 return datetime.parse(str); 78 } 79 80 // ****************************Date轉換爲String**************************** 81 /** 82 * 轉換成日期格式的字符串 格式爲yyyy-MM-dd 83 * 84 * @param Object 85 * @return String 86 */ 87 public static String dateFormat(Date o) { 88 if (o == null) { 89 return ""; 90 } 91 return date.format(o); 92 } 93 94 /** 95 * 轉換成時間格式的字符串 格式爲yyyy-MM-dd hh:mm:ss 96 * 97 * @param Date 98 * @return String 99 */ 100 public static String dateTimeFormat(Date o) { 101 if (o == null) { 102 return ""; 103 } 104 return datetime.format(o); 105 } 106 107 /** 108 * 轉換成給定時間格式的字符串 109 * 110 * @param Date 111 * @param String 112 * @return String 113 */ 114 public static String getDateFormat(Date d, String format) { 115 return new SimpleDateFormat(format).format(d); 116 } 117 118 /** 119 * 日期格式化(yyyy年MM月dd日) 120 * 121 * @param Date 122 * @return String 123 * */ 124 public static String fDateCNYR(Date date) { 125 return getDateFormat(date, "yyyy年MM月dd日"); 126 } 127 128 /** 129 * 日期格式化(yyyy年MM月dd日 HH:mm) 130 * 131 * @param Date 132 * @return String 133 * */ 134 public static String fDateCNYRS(Date date) { 135 return getDateFormat(date, "yyyy年MM月dd日 HH點"); 136 } 137 138 /** 139 * 日期格式化(yyyy年MM月dd日 HH:mm) 140 * 141 * @param Date 142 * @return String 143 * */ 144 public static String fDateCNYRSF(Date date) { 145 return getDateFormat(date, "yyyy年MM月dd日 HH:mm"); 146 } 147 148 /** 149 * 日期格式化(yyyy年MM月dd日 HH:mm:ss) 150 * 151 * @param Date 152 * @return String 153 * */ 154 public static String fDateCNYRSFM(Date date) { 155 return getDateFormat(date, "yyyy年MM月dd日 HH:mm:ss"); 156 } 157 158 // ****************************時間格式的String轉換爲String**************************** 159 /** 160 * 根據給定的時間格式字符串截取給定格式的字符串 161 * 162 * @param d 163 * String 給定時間格式爲yyyy-MM-dd HH:mm:ss 164 * @param format 165 * String 給定的格式 166 * @return String 167 */ 168 public static String getDateFormat(String d, String format) 169 throws Exception { 170 Date date = datetime.parse(d); 171 return getDateFormat(date, format); 172 } 173 174 // ****************************時間格式的String轉換爲long**************************** 175 /** 176 * 經過字符串得到long型時間 177 * 178 * @param String 179 * @return long 180 */ 181 public static long getDateFromStr(String dateStr) { 182 long temp = 0L; 183 Date date = null; 184 try { 185 date = datetime.parse(dateStr); 186 } catch (Exception e) { 187 e.printStackTrace(); 188 return temp; 189 } 190 temp = date.getTime(); 191 return temp; 192 } 193 194 // ****************************Date轉換爲給定格式的Date**************************** 195 /** 196 * 日期格式化(2014-03-04) 197 * 198 * @param Date 199 * @return Date 200 * @throws ParseException 201 * */ 202 public static Date fDate(Date dat) throws Exception { 203 String dateStr = date.format(dat); 204 return date.parse(dateStr); 205 } 206 207 /** 208 * 經過開始時間和間隔得到結束時間。 209 * 210 * @param String 211 * @param int 212 * @return String 213 */ 214 public static String getEndTime(String start, int span) { 215 if (isNullOrNone(start) || span == 0) { 216 return null; 217 } 218 long temp = getDateFromStr(start); 219 temp += span * 60L * 1000L; 220 return datetime.format(new Date(temp)); 221 } 222 223 /** 224 * 格式化字符串,將2013-10-20 00:00:00.000000簡化爲2013-10-20 00:00:00 225 * 226 * @param String 227 * str 228 * @return String 229 * @throws ParseException 230 * */ 231 public static String getFormatStringDay(String str) throws Exception { 232 Date date = datetime.parse(str); 233 return datetime.format(date); 234 } 235 236 /** 237 * 判斷是否爲空 238 * 239 * @param String 240 * @return boolean 241 */ 242 public static boolean isNullOrNone(String src) { 243 if (null == src || "".equals(src)) { 244 return true; 245 } 246 return false; 247 } 248 249 /** 250 * 若是字符串長度大於25則截取前25個字符串後續改爲省略號 251 * 252 * @param String 253 * @return String 254 */ 255 public static String showCount(String str) { 256 if (str != null) { 257 if (str.length() > 25) { 258 str = str.substring(0, 25); 259 str = str + "..."; 260 } 261 } else { 262 str = ""; 263 } 264 return str; 265 } 266 267 /** 268 * 是否符合日期格式yyyy-MM-dd 269 * 270 * @param day 271 * String 日期字符串 272 * @return boolean 273 */ 274 public static boolean isFormatDay(String day) { 275 return day 276 .matches("(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)"); 277 } 278 279 /** 280 * 是否符合時間格式HH:mm:ss 281 * 282 * @param time 283 * String 時間字符串 284 * @return boolean 285 */ 286 public static boolean isFormatTime(String time) { 287 return time 288 .matches("(0[1-9]|1[0-9]|2[0-4]):(0[1-9]|[1-5][0-9]):(0[1-9]|[1-5][0-9])(\\.000000)?"); 289 } 290 291 /** 292 * 是否符合時間格式yyyy-MM-dd HH:mm:ss 293 * 294 * @param time 295 * String 時間字符串 296 * @return boolean 297 */ 298 public static boolean isFormat(String time) { 299 String[] temp = time.split(" "); 300 return isFormatDay(temp[0]) && isFormatTime(temp[1]); 301 } 302 303 /** 304 * 經過給定的年、月、周得到該周內的每一天日期 305 * 306 * @param year 307 * int 年 308 * @param month 309 * int 月 310 * @param week 311 * int 周 312 * @return List<Date> 七天的日期 313 */ 314 public static List<Date> getDayByWeek(int year, int month, int week) { 315 List<Date> list = new ArrayList<Date>(); 316 // 先滾動到該年. 317 Calendar c = Calendar.getInstance(); 318 c.set(Calendar.YEAR, year); 319 // 滾動到月: 320 c.set(Calendar.MONTH, month - 1); 321 // 滾動到周: 322 c.set(Calendar.WEEK_OF_MONTH, week); 323 // 獲得該周第一天: 324 for (int i = 0; i < 6; i++) { 325 c.set(Calendar.DAY_OF_WEEK, i + 2); 326 list.add(c.getTime()); 327 } 328 // 最後一天: 329 c.set(Calendar.WEEK_OF_MONTH, week + 1); 330 c.set(Calendar.DAY_OF_WEEK, 1); 331 list.add(c.getTime()); 332 return list; 333 } 334 335 /** 336 * 得到當前日期是本月的第幾周 337 * 338 * @return int 339 */ 340 public static int getCurWeekNoOfMonth() { 341 Date date = new Date(System.currentTimeMillis()); 342 Calendar calendar = Calendar.getInstance(); 343 calendar.setFirstDayOfWeek(Calendar.MONDAY); 344 calendar.setTime(date); 345 return calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH); 346 } 347 348 /** 349 * 得到當前日期是星期幾 350 * 351 * @return int 352 */ 353 public static int getCurWeekNo(String dat) { 354 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 355 Date date = null; 356 try { 357 date = format.parse(dat); 358 } catch (Exception e) { 359 e.printStackTrace(); 360 } 361 Calendar calendar = Calendar.getInstance(); 362 calendar.setFirstDayOfWeek(Calendar.MONDAY); 363 calendar.setTime(date); 364 return calendar.get(Calendar.DAY_OF_WEEK); 365 } 366 367 /** 368 * 得到當前的年份 369 * 370 * @return 371 */ 372 public static int getCurrentYear() { 373 Calendar calendar = Calendar.getInstance(); 374 return calendar.get(Calendar.YEAR); 375 } 376 377 /** 378 * 得到當前的月份 379 * 380 * @return 381 */ 382 public static int getCurrentMonth() { 383 Calendar calendar = Calendar.getInstance(); 384 return calendar.get(Calendar.MONTH) + 1; 385 } 386 387 /** 388 * 得到當前的日期天 389 * 390 * @return 391 */ 392 public static int getCurrentDay() { 393 Calendar calendar = Calendar.getInstance(); 394 return calendar.get(Calendar.DATE); 395 } 396 /** 397 * 獲取當月最後一天 398 * 399 * @param Date date 400 * @param String format 401 * @return String 402 * */ 403 public static String lastDayOfMoth(Date date, String format){ 404 Calendar cal = Calendar.getInstance(); 405 cal.setTime(date); 406 cal.set(Calendar.DAY_OF_MONTH,1); 407 cal.add(Calendar.MONTH,1); 408 cal.add(Calendar.DATE, -1); 409 date = cal.getTime();; 410 SimpleDateFormat sf = new SimpleDateFormat(format); 411 return sf.format(date); 412 } 413 /** 414 * 獲取當月最後一天 415 * 416 * @param Date date 417 * @param String format 418 * @return String 419 * */ 420 public static String firstDayOfMoth(Date date, String format){ 421 Calendar cal = Calendar.getInstance(); 422 cal.setTime(date); 423 cal.add(Calendar.DATE, 0); 424 date = cal.getTime();; 425 SimpleDateFormat sf = new SimpleDateFormat(format); 426 return sf.format(date); 427 } 428 //**************************************************************** 429 /** 430 * 轉換成字符串方法,其中若是是Integer格式的返回0,若是是Double格式的返回0.0 431 * 432 * @param Object 433 * @return String 434 */ 435 public static String toString(Object o) { 436 if (o == null) { 437 if (o instanceof Integer) { 438 return "0"; 439 } 440 if (o instanceof Double) { 441 return "0.0"; 442 } 443 return ""; 444 } else { 445 return o.toString(); 446 } 447 } 448 449 /** 450 * 清空字符串,若是爲則轉換成null 451 * 452 * @param String 453 * @return String 454 */ 455 public static String emptyString2Null(String src) { 456 if (src != null) { 457 if ("".equals(src)) { 458 src = null; 459 } 460 } 461 return src; 462 } 463 /** 464 * 轉化成可在hql中使用的字符串 465 * 1,2 轉爲 '1','2' 466 * */ 467 public static String formatIds(String ids){ 468 if(ids!=null&&ids!="") 469 { 470 String[] id = ids.split(","); 471 StringBuffer idsStr = new StringBuffer(); 472 for(String str : id){ 473 idsStr.append("'"+str+"',"); 474 } 475 return idsStr.toString().substring(0,idsStr.length()-1); 476 } 477 else 478 { 479 return ""; 480 } 481 } 482 /** 483 * 獲取當前日期前一天 484 * 485 * @param Date date 486 * @return Date 487 * */ 488 public static Date getSpecifiedDayBefore(Date date){ 489 Calendar c = Calendar.getInstance(); 490 c.setTime(date); 491 int day = c.get(Calendar.DATE); 492 c.set(Calendar.DATE, day-1); 493 date = c.getTime(); 494 return date; 495 } 496 /** 497 * 比較兩個日期的大小 498 * 499 * @param data1 500 * @param data2 501 * 502 * @return boolean 503 * 504 * @author zhangss 2016-5-18 13:47:16 505 * */ 506 public boolean bjDate(Date date1, Date date2){ 507 if (date1.getTime() > date2.getTime()) { 508 return true; 509 } 510 return false; 511 } 512 513 }