免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1689 | 回复: 1
打印 上一主题 下一主题

java日期处理日期工具类DateUtil备忘笔记2 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-11 20:43 |只看该作者 |倒序浏览















java日期处理日期工具类DateUtil备忘笔记2
  1. 301.    public static String addYear(String date, int i) {  
  2. 302.        try {  
  3. 303.            GregorianCalendar gCal = new GregorianCalendar(  
  4. 304.                    Integer.parseInt(date.substring(0, 4)),   
  5. 305.                    Integer.parseInt(date.substring(5, 7)) - 1,   
  6. 306.                    Integer.parseInt(date.substring(8, 10)));  
  7. 307.            gCal.add(GregorianCalendar.YEAR, i);  
  8. 308.            return sdf_date_format.format(gCal.getTime());  
  9. 309.        } catch (Exception e) {  
  10. 310.            logger.debug("DateUtil.addYear():" + e.toString());  
  11. 311.            return "";  
  12. 312.        }  
  13. 313.    }  
  14. 314.  
  15. 315.    /**
  16. 316.     * 返回某年某月中的最大天
  17. 317.     * @author dylan_xu
  18. 318.     * @date Mar 11, 2012
  19. 319.     * @param year
  20. 320.     * @param month
  21. 321.     * @return
  22. 322.     */  
  23. 323.    public static int getMaxDay(int iyear, int imonth) {  
  24. 324.        int day = 0;  
  25. 325.        try {  
  26. 326.            if (imonth == 1 || imonth == 3 || imonth == 5 || imonth == 7  
  27. 327.                    || imonth == 8 || imonth == 10 || imonth == 12) {  
  28. 328.                day = 31;  
  29. 329.            } else if (imonth == 4 || imonth == 6 || imonth == 9 || imonth == 11) {  
  30. 330.                day = 30;  
  31. 331.            } else if ((0 == (iyear % 4)) && (0 != (iyear % 100)) || (0 == (iyear % 400))) {  
  32. 332.                day = 29;  
  33. 333.            } else {  
  34. 334.                day = 28;  
  35. 335.            }  
  36. 336.            return day;  
  37. 337.        } catch (Exception e) {  
  38. 338.            logger.debug("DateUtil.getMonthDay():" + e.toString());  
  39. 339.            return 1;  
  40. 340.        }  
  41. 341.    }  
  42. 342.  
  43. 343.    /**
  44. 344.     * 格式化日期
  45. 345.     * @author dylan_xu
  46. 346.     * @date Mar 11, 2012
  47. 347.     * @param orgDate
  48. 348.     * @param Type
  49. 349.     * @param Span
  50. 350.     * @return
  51. 351.     */  
  52. 352.    @SuppressWarnings("static-access")  
  53. 353.    public String rollDate(String orgDate, int Type, int Span) {  
  54. 354.        try {  
  55. 355.            String temp = "";  
  56. 356.            int iyear, imonth, iday;  
  57. 357.            int iPos = 0;  
  58. 358.            char seperater = '-';  
  59. 359.            if (orgDate == null || orgDate.length() < 6) {  
  60. 360.                return "";  
  61. 361.            }  
  62. 362.  
  63. 363.            iPos = orgDate.indexOf(seperater);  
  64. 364.            if (iPos > 0) {  
  65. 365.                iyear = Integer.parseInt(orgDate.substring(0, iPos));  
  66. 366.                temp = orgDate.substring(iPos + 1);  
  67. 367.            } else {  
  68. 368.                iyear = Integer.parseInt(orgDate.substring(0, 4));  
  69. 369.                temp = orgDate.substring(4);  
  70. 370.            }  
  71. 371.  
  72. 372.            iPos = temp.indexOf(seperater);  
  73. 373.            if (iPos > 0) {  
  74. 374.                imonth = Integer.parseInt(temp.substring(0, iPos));  
  75. 375.                temp = temp.substring(iPos + 1);  
  76. 376.            } else {  
  77. 377.                imonth = Integer.parseInt(temp.substring(0, 2));  
  78. 378.                temp = temp.substring(2);  
  79. 379.            }  
  80. 380.  
  81. 381.            imonth--;  
  82. 382.            if (imonth < 0 || imonth > 11) {  
  83. 383.                imonth = 0;  
  84. 384.            }  
  85. 385.  
  86. 386.            iday = Integer.parseInt(temp);  
  87. 387.            if (iday < 1 || iday > 31)  
  88. 388.                iday = 1;  
  89. 389.  
  90. 390.            Calendar orgcale = Calendar.getInstance();  
  91. 391.            orgcale.set(iyear, imonth, iday);  
  92. 392.            temp = this.rollDate(orgcale, Type, Span);  
  93. 393.            return temp;  
  94. 394.        } catch (Exception e) {  
  95. 395.            return "";  
  96. 396.        }  
  97. 397.    }  
  98. 398.  
  99. 399.    public static String rollDate(Calendar cal, int Type, int Span) {  
  100. 400.        try {  
  101. 401.            String temp = "";  
  102. 402.            Calendar rolcale;  
  103. 403.            rolcale = cal;  
  104. 404.            rolcale.add(Type, Span);  
  105. 405.            temp = sdf_date_format.format(rolcale.getTime());  
  106. 406.            return temp;  
  107. 407.        } catch (Exception e) {  
  108. 408.            return "";  
  109. 409.        }  
  110. 410.    }  
  111. 411.  
  112. 412.    /**
  113. 413.     * 返回默认的日期格式
  114. 414.     * @author dylan_xu
  115. 415.     * @date Mar 11, 2012
  116. 416.     * @return
  117. 417.     */  
  118. 418.    public static synchronized String getDatePattern() {  
  119. 419.        defaultDatePattern = "yyyy-MM-dd";  
  120. 420.        return defaultDatePattern;  
  121. 421.    }  
  122. 422.  
  123. 423.    /**
  124. 424.     * 将指定日期按默认格式进行格式代化成字符串后输出如:yyyy-MM-dd
  125. 425.     * @author dylan_xu
  126. 426.     * @date Mar 11, 2012
  127. 427.     * @param aDate
  128. 428.     * @return
  129. 429.     */  
  130. 430.    public static final String getDate(Date aDate) {  
  131. 431.        SimpleDateFormat df = null;  
  132. 432.        String returnValue = "";  
  133. 433.        if (aDate != null) {  
  134. 434.            df = new SimpleDateFormat(getDatePattern());  
  135. 435.            returnValue = df.format(aDate);  
  136. 436.        }  
  137. 437.        return (returnValue);  
  138. 438.    }  
  139. 439.  
  140. 440.    /**
  141. 441.     * 取得给定日期的时间字符串,格式为当前默认时间格式
  142. 442.     * @author dylan_xu
  143. 443.     * @date Mar 11, 2012
  144. 444.     * @param theTime
  145. 445.     * @return
  146. 446.     */  
  147. 447.    public static String getTimeNow(Date theTime) {  
  148. 448.        return getDateTime(timePattern, theTime);  
  149. 449.    }  
  150. 450.  
  151. 451.    /**
  152. 452.     * 取得当前时间的Calendar日历对象
  153. 453.     * @author dylan_xu
  154. 454.     * @date Mar 11, 2012
  155. 455.     * @return
  156. 456.     * @throws ParseException
  157. 457.     */  
  158. 458.    public Calendar getToday() throws ParseException {  
  159. 459.        Date today = new Date();  
  160. 460.        SimpleDateFormat df = new SimpleDateFormat(getDatePattern());  
  161. 461.        String todayAsString = df.format(today);  
  162. 462.        Calendar cal = new GregorianCalendar();  
  163. 463.        cal.setTime(convertStringToDate(todayAsString));  
  164. 464.        return cal;  
  165. 465.    }  
  166. 466.  
  167. 467.    /**
  168. 468.     * 将日期类转换成指定格式的字符串形式
  169. 469.     * @author dylan_xu
  170. 470.     * @date Mar 11, 2012
  171. 471.     * @param aMask
  172. 472.     * @param aDate
  173. 473.     * @return
  174. 474.     */  
  175. 475.    public static final String getDateTime(String aMask, Date aDate) {  
  176. 476.        SimpleDateFormat df = null;  
  177. 477.        String returnValue = "";  
  178. 478.  
  179. 479.        if (aDate == null) {  
  180. 480.            logger.error("aDate is null!");  
  181. 481.        } else {  
  182. 482.            df = new SimpleDateFormat(aMask);  
  183. 483.            returnValue = df.format(aDate);  
  184. 484.        }  
  185. 485.        return (returnValue);  
  186. 486.    }  
  187. 487.  
  188. 488.    /**
  189. 489.     * 将指定的日期转换成默认格式的字符串形式
  190. 490.     * @author dylan_xu
  191. 491.     * @date Mar 11, 2012
  192. 492.     * @param aDate
  193. 493.     * @return
  194. 494.     */  
  195. 495.    public static final String convertDateToString(Date aDate) {  
  196. 496.        return getDateTime(getDatePattern(), aDate);  
  197. 497.    }  
  198. 498.  
  199. 499.    /**
  200. 500.     * 将日期字符串按指定格式转换成日期类型
  201. 501.     * @author dylan_xu
  202. 502.     * @date Mar 11, 2012
  203. 503.     * @param aMask 指定的日期格式,如:yyyy-MM-dd
  204. 504.     * @param strDate 待转换的日期字符串
  205. 505.     * @return
  206. 506.     * @throws ParseException
  207. 507.     */  
  208. 508.    public static final Date convertStringToDate(String aMask, String strDate)  
  209. 509.            throws ParseException {  
  210. 510.        SimpleDateFormat df = null;  
  211. 511.        Date date = null;  
  212. 512.        df = new SimpleDateFormat(aMask);  
  213. 513.  
  214. 514.        if (logger.isDebugEnabled()) {  
  215. 515.            logger.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");  
  216. 516.        }  
  217. 517.        try {  
  218. 518.            date = df.parse(strDate);  
  219. 519.        } catch (ParseException pe) {  
  220. 520.            logger.error("ParseException: " + pe);  
  221. 521.            throw pe;  
  222. 522.        }  
  223. 523.        return (date);  
  224. 524.    }  
  225. 525.  
  226. 526.    /**
  227. 527.     * 将日期字符串按默认格式转换成日期类型
  228. 528.     * @author dylan_xu
  229. 529.     * @date Mar 11, 2012
  230. 530.     * @param strDate
  231. 531.     * @return
  232. 532.     * @throws ParseException
  233. 533.     */  
  234. 534.    public static Date convertStringToDate(String strDate)  
  235. 535.            throws ParseException {  
  236. 536.        Date aDate = null;  
  237. 537.  
  238. 538.        try {  
  239. 539.            if (logger.isDebugEnabled()) {  
  240. 540.                logger.debug("converting date with pattern: " + getDatePattern());  
  241. 541.            }  
  242. 542.            aDate = convertStringToDate(getDatePattern(), strDate);  
  243. 543.        } catch (ParseException pe) {  
  244. 544.            logger.error("Could not convert '" + strDate + "' to a date, throwing exception");  
  245. 545.            throw new ParseException(pe.getMessage(), pe.getErrorOffset());  
  246. 546.        }  
  247. 547.        return aDate;  
  248. 548.    }  
  249. 549.  
  250. 550.    /**
  251. 551.     * 返回一个JAVA简单类型的日期字符串
  252. 552.     * @author dylan_xu
  253. 553.     * @date Mar 11, 2012
  254. 554.     * @return
  255. 555.     */  
  256. 556.    public static String getSimpleDateFormat() {  
  257. 557.        SimpleDateFormat formatter = new SimpleDateFormat();  
  258. 558.        String NDateTime = formatter.format(new Date());  
  259. 559.        return NDateTime;  
  260. 560.    }  
  261. 561.      
  262. 562.    /**
  263. 563.     * 将指定字符串格式的日期与当前时间比较
  264. 564.     * @author DYLAN
  265. 565.     * @date Feb 17, 2012
  266. 566.     * @param strDate 需要比较时间
  267. 567.     * @return  
  268. 568.     *      <p>
  269. 569.     *      int code
  270. 570.     *      <ul>
  271. 571.     *      <li>-1 当前时间 < 比较时间 </li>
  272. 572.     *      <li> 0 当前时间 = 比较时间 </li>
  273. 573.     *      <li>>=1当前时间 > 比较时间 </li>
  274. 574.     *      </ul>
  275. 575.     *      </p>
  276. 576.     */  
  277. 577.    public static int compareToCurTime (String strDate) {  
  278. 578.        if (StringUtils.isBlank(strDate)) {  
  279. 579.            return -1;  
  280. 580.        }  
  281. 581.        Date curTime = cale.getTime();  
  282. 582.        String strCurTime = null;  
  283. 583.        try {  
  284. 584.            strCurTime = sdf_datetime_format.format(curTime);  
  285. 585.        } catch (Exception e) {  
  286. 586.            if (logger.isDebugEnabled()) {  
  287. 587.                logger.debug("[Could not format '" + strDate + "' to a date, throwing exception:" + e.getLocalizedMessage() + "]");  
  288. 588.            }  
  289. 589.        }  
  290. 590.        if (StringUtils.isNotBlank(strCurTime)) {  
  291. 591.            return strCurTime.compareTo(strDate);  
  292. 592.        }  
  293. 593.        return -1;  
  294. 594.    }  
  295. 595.      
  296. 596.    /**
  297. 597.     * 为查询日期添加最小时间
  298. 598.     *  
  299. 599.     * @param 目标类型Date
  300. 600.     * @param 转换参数Date
  301. 601.     * @return
  302. 602.     */  
  303. 603.    @SuppressWarnings("deprecation")  
  304. 604.    public static Date addStartTime(Date param) {  
  305. 605.        Date date = param;  
  306. 606.        try {  
  307. 607.            date.setHours(0);  
  308. 608.            date.setMinutes(0);  
  309. 609.            date.setSeconds(0);  
  310. 610.            return date;  
  311. 611.        } catch (Exception ex) {  
  312. 612.            return date;  
  313. 613.        }  
  314. 614.    }  
  315. 615.  
  316. 616.    /**
  317. 617.     * 为查询日期添加最大时间
  318. 618.     *  
  319. 619.     * @param 目标类型Date
  320. 620.     * @param 转换参数Date
  321. 621.     * @return
  322. 622.     */  
  323. 623.    @SuppressWarnings("deprecation")  
  324. 624.    public static Date addEndTime(Date param) {  
  325. 625.        Date date = param;  
  326. 626.        try {  
  327. 627.            date.setHours(23);  
  328. 628.            date.setMinutes(59);  
  329. 629.            date.setSeconds(0);  
  330. 630.            return date;  
  331. 631.        } catch (Exception ex) {  
  332. 632.            return date;  
  333. 633.        }  
  334. 634.    }  
  335. 635.  
  336. 636.    /**
  337. 637.     * 返回系统现在年份中指定月份的天数
  338. 638.     *  
  339. 639.     * @param 月份month
  340. 640.     * @return 指定月的总天数
  341. 641.     */  
  342. 642.    @SuppressWarnings("deprecation")  
  343. 643.    public static String getMonthLastDay(int month) {  
  344. 644.        Date date = new Date();  
  345. 645.        int[][] day = { { 0, 30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },  
  346. 646.                { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };  
  347. 647.        int year = date.getYear() + 1900;  
  348. 648.        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {  
  349. 649.            return day[1][month] + "";  
  350. 650.        } else {  
  351. 651.            return day[0][month] + "";  
  352. 652.        }  
  353. 653.    }  
  354. 654.  
  355. 655.    /**
  356. 656.     * 返回指定年份中指定月份的天数
  357. 657.     *  
  358. 658.     * @param 年份year
  359. 659.     * @param 月份month
  360. 660.     * @return 指定月的总天数
  361. 661.     */  
  362. 662.    public static String getMonthLastDay(int year, int month) {  
  363. 663.        int[][] day = { { 0, 30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },  
  364. 664.                { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };  
  365. 665.        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {  
  366. 666.            return day[1][month] + "";  
  367. 667.        } else {  
  368. 668.            return day[0][month] + "";  
  369. 669.        }  
  370. 670.    }  
  371. 671.  
  372. 672.    /**
  373. 673.     * 判断是平年还是闰年
  374. 674.     * @author dylan_xu
  375. 675.     * @date Mar 11, 2012
  376. 676.     * @param year
  377. 677.     * @return
  378. 678.     */   
  379. 679.    public static boolean isLeapyear(int year) {  
  380. 680.        if ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0) {  
  381. 681.            return true;  
  382. 682.        } else {  
  383. 683.            return false;  
  384. 684.        }  
  385. 685.    }  
  386. 686.  
  387. 687.    /**
  388. 688.     * 取得当前时间的日戳
  389. 689.     * @author dylan_xu
  390. 690.     * @date Mar 11, 2012
  391. 691.     * @return
  392. 692.     */  
  393. 693.    @SuppressWarnings("deprecation")  
  394. 694.    public static String getTimestamp() {  
  395. 695.        Date date = cale.getTime();  
  396. 696.        String timestamp = "" + (date.getYear() + 1900) + date.getMonth()  
  397. 697.                + date.getDate() + date.getMinutes() + date.getSeconds()  
  398. 698.                + date.getTime();  
  399. 699.        return timestamp;  
  400. 700.    }  
  401. 701.  
  402. 702.    /**
  403. 703.     * 取得指定时间的日戳
  404. 704.     *  
  405. 705.     * @return
  406. 706.     */  
  407. 707.    @SuppressWarnings("deprecation")  
  408. 708.    public static String getTimestamp(Date date) {  
  409. 709.        String timestamp = "" + (date.getYear() + 1900) + date.getMonth()  
  410. 710.                + date.getDate() + date.getMinutes() + date.getSeconds()  
  411. 711.                + date.getTime();  
  412. 712.        return timestamp;  
  413. 713.    }  
  414. 714.  
  415. 715.    public static void main(String[] args) {  
  416. 716.        System.out.println(getYear() + "|" + getMonth() + "|" + getDate());  
  417. 717.    }  
  418. 718.}  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-11 20:44 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP