- 论坛徽章:
- 0
|
java日期处理日期工具类DateUtil备忘笔记2 - 301. public static String addYear(String date, int i) {
- 302. try {
- 303. GregorianCalendar gCal = new GregorianCalendar(
- 304. Integer.parseInt(date.substring(0, 4)),
- 305. Integer.parseInt(date.substring(5, 7)) - 1,
- 306. Integer.parseInt(date.substring(8, 10)));
- 307. gCal.add(GregorianCalendar.YEAR, i);
- 308. return sdf_date_format.format(gCal.getTime());
- 309. } catch (Exception e) {
- 310. logger.debug("DateUtil.addYear():" + e.toString());
- 311. return "";
- 312. }
- 313. }
- 314.
- 315. /**
- 316. * 返回某年某月中的最大天
- 317. * @author dylan_xu
- 318. * @date Mar 11, 2012
- 319. * @param year
- 320. * @param month
- 321. * @return
- 322. */
- 323. public static int getMaxDay(int iyear, int imonth) {
- 324. int day = 0;
- 325. try {
- 326. if (imonth == 1 || imonth == 3 || imonth == 5 || imonth == 7
- 327. || imonth == 8 || imonth == 10 || imonth == 12) {
- 328. day = 31;
- 329. } else if (imonth == 4 || imonth == 6 || imonth == 9 || imonth == 11) {
- 330. day = 30;
- 331. } else if ((0 == (iyear % 4)) && (0 != (iyear % 100)) || (0 == (iyear % 400))) {
- 332. day = 29;
- 333. } else {
- 334. day = 28;
- 335. }
- 336. return day;
- 337. } catch (Exception e) {
- 338. logger.debug("DateUtil.getMonthDay():" + e.toString());
- 339. return 1;
- 340. }
- 341. }
- 342.
- 343. /**
- 344. * 格式化日期
- 345. * @author dylan_xu
- 346. * @date Mar 11, 2012
- 347. * @param orgDate
- 348. * @param Type
- 349. * @param Span
- 350. * @return
- 351. */
- 352. @SuppressWarnings("static-access")
- 353. public String rollDate(String orgDate, int Type, int Span) {
- 354. try {
- 355. String temp = "";
- 356. int iyear, imonth, iday;
- 357. int iPos = 0;
- 358. char seperater = '-';
- 359. if (orgDate == null || orgDate.length() < 6) {
- 360. return "";
- 361. }
- 362.
- 363. iPos = orgDate.indexOf(seperater);
- 364. if (iPos > 0) {
- 365. iyear = Integer.parseInt(orgDate.substring(0, iPos));
- 366. temp = orgDate.substring(iPos + 1);
- 367. } else {
- 368. iyear = Integer.parseInt(orgDate.substring(0, 4));
- 369. temp = orgDate.substring(4);
- 370. }
- 371.
- 372. iPos = temp.indexOf(seperater);
- 373. if (iPos > 0) {
- 374. imonth = Integer.parseInt(temp.substring(0, iPos));
- 375. temp = temp.substring(iPos + 1);
- 376. } else {
- 377. imonth = Integer.parseInt(temp.substring(0, 2));
- 378. temp = temp.substring(2);
- 379. }
- 380.
- 381. imonth--;
- 382. if (imonth < 0 || imonth > 11) {
- 383. imonth = 0;
- 384. }
- 385.
- 386. iday = Integer.parseInt(temp);
- 387. if (iday < 1 || iday > 31)
- 388. iday = 1;
- 389.
- 390. Calendar orgcale = Calendar.getInstance();
- 391. orgcale.set(iyear, imonth, iday);
- 392. temp = this.rollDate(orgcale, Type, Span);
- 393. return temp;
- 394. } catch (Exception e) {
- 395. return "";
- 396. }
- 397. }
- 398.
- 399. public static String rollDate(Calendar cal, int Type, int Span) {
- 400. try {
- 401. String temp = "";
- 402. Calendar rolcale;
- 403. rolcale = cal;
- 404. rolcale.add(Type, Span);
- 405. temp = sdf_date_format.format(rolcale.getTime());
- 406. return temp;
- 407. } catch (Exception e) {
- 408. return "";
- 409. }
- 410. }
- 411.
- 412. /**
- 413. * 返回默认的日期格式
- 414. * @author dylan_xu
- 415. * @date Mar 11, 2012
- 416. * @return
- 417. */
- 418. public static synchronized String getDatePattern() {
- 419. defaultDatePattern = "yyyy-MM-dd";
- 420. return defaultDatePattern;
- 421. }
- 422.
- 423. /**
- 424. * 将指定日期按默认格式进行格式代化成字符串后输出如:yyyy-MM-dd
- 425. * @author dylan_xu
- 426. * @date Mar 11, 2012
- 427. * @param aDate
- 428. * @return
- 429. */
- 430. public static final String getDate(Date aDate) {
- 431. SimpleDateFormat df = null;
- 432. String returnValue = "";
- 433. if (aDate != null) {
- 434. df = new SimpleDateFormat(getDatePattern());
- 435. returnValue = df.format(aDate);
- 436. }
- 437. return (returnValue);
- 438. }
- 439.
- 440. /**
- 441. * 取得给定日期的时间字符串,格式为当前默认时间格式
- 442. * @author dylan_xu
- 443. * @date Mar 11, 2012
- 444. * @param theTime
- 445. * @return
- 446. */
- 447. public static String getTimeNow(Date theTime) {
- 448. return getDateTime(timePattern, theTime);
- 449. }
- 450.
- 451. /**
- 452. * 取得当前时间的Calendar日历对象
- 453. * @author dylan_xu
- 454. * @date Mar 11, 2012
- 455. * @return
- 456. * @throws ParseException
- 457. */
- 458. public Calendar getToday() throws ParseException {
- 459. Date today = new Date();
- 460. SimpleDateFormat df = new SimpleDateFormat(getDatePattern());
- 461. String todayAsString = df.format(today);
- 462. Calendar cal = new GregorianCalendar();
- 463. cal.setTime(convertStringToDate(todayAsString));
- 464. return cal;
- 465. }
- 466.
- 467. /**
- 468. * 将日期类转换成指定格式的字符串形式
- 469. * @author dylan_xu
- 470. * @date Mar 11, 2012
- 471. * @param aMask
- 472. * @param aDate
- 473. * @return
- 474. */
- 475. public static final String getDateTime(String aMask, Date aDate) {
- 476. SimpleDateFormat df = null;
- 477. String returnValue = "";
- 478.
- 479. if (aDate == null) {
- 480. logger.error("aDate is null!");
- 481. } else {
- 482. df = new SimpleDateFormat(aMask);
- 483. returnValue = df.format(aDate);
- 484. }
- 485. return (returnValue);
- 486. }
- 487.
- 488. /**
- 489. * 将指定的日期转换成默认格式的字符串形式
- 490. * @author dylan_xu
- 491. * @date Mar 11, 2012
- 492. * @param aDate
- 493. * @return
- 494. */
- 495. public static final String convertDateToString(Date aDate) {
- 496. return getDateTime(getDatePattern(), aDate);
- 497. }
- 498.
- 499. /**
- 500. * 将日期字符串按指定格式转换成日期类型
- 501. * @author dylan_xu
- 502. * @date Mar 11, 2012
- 503. * @param aMask 指定的日期格式,如:yyyy-MM-dd
- 504. * @param strDate 待转换的日期字符串
- 505. * @return
- 506. * @throws ParseException
- 507. */
- 508. public static final Date convertStringToDate(String aMask, String strDate)
- 509. throws ParseException {
- 510. SimpleDateFormat df = null;
- 511. Date date = null;
- 512. df = new SimpleDateFormat(aMask);
- 513.
- 514. if (logger.isDebugEnabled()) {
- 515. logger.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
- 516. }
- 517. try {
- 518. date = df.parse(strDate);
- 519. } catch (ParseException pe) {
- 520. logger.error("ParseException: " + pe);
- 521. throw pe;
- 522. }
- 523. return (date);
- 524. }
- 525.
- 526. /**
- 527. * 将日期字符串按默认格式转换成日期类型
- 528. * @author dylan_xu
- 529. * @date Mar 11, 2012
- 530. * @param strDate
- 531. * @return
- 532. * @throws ParseException
- 533. */
- 534. public static Date convertStringToDate(String strDate)
- 535. throws ParseException {
- 536. Date aDate = null;
- 537.
- 538. try {
- 539. if (logger.isDebugEnabled()) {
- 540. logger.debug("converting date with pattern: " + getDatePattern());
- 541. }
- 542. aDate = convertStringToDate(getDatePattern(), strDate);
- 543. } catch (ParseException pe) {
- 544. logger.error("Could not convert '" + strDate + "' to a date, throwing exception");
- 545. throw new ParseException(pe.getMessage(), pe.getErrorOffset());
- 546. }
- 547. return aDate;
- 548. }
- 549.
- 550. /**
- 551. * 返回一个JAVA简单类型的日期字符串
- 552. * @author dylan_xu
- 553. * @date Mar 11, 2012
- 554. * @return
- 555. */
- 556. public static String getSimpleDateFormat() {
- 557. SimpleDateFormat formatter = new SimpleDateFormat();
- 558. String NDateTime = formatter.format(new Date());
- 559. return NDateTime;
- 560. }
- 561.
- 562. /**
- 563. * 将指定字符串格式的日期与当前时间比较
- 564. * @author DYLAN
- 565. * @date Feb 17, 2012
- 566. * @param strDate 需要比较时间
- 567. * @return
- 568. * <p>
- 569. * int code
- 570. * <ul>
- 571. * <li>-1 当前时间 < 比较时间 </li>
- 572. * <li> 0 当前时间 = 比较时间 </li>
- 573. * <li>>=1当前时间 > 比较时间 </li>
- 574. * </ul>
- 575. * </p>
- 576. */
- 577. public static int compareToCurTime (String strDate) {
- 578. if (StringUtils.isBlank(strDate)) {
- 579. return -1;
- 580. }
- 581. Date curTime = cale.getTime();
- 582. String strCurTime = null;
- 583. try {
- 584. strCurTime = sdf_datetime_format.format(curTime);
- 585. } catch (Exception e) {
- 586. if (logger.isDebugEnabled()) {
- 587. logger.debug("[Could not format '" + strDate + "' to a date, throwing exception:" + e.getLocalizedMessage() + "]");
- 588. }
- 589. }
- 590. if (StringUtils.isNotBlank(strCurTime)) {
- 591. return strCurTime.compareTo(strDate);
- 592. }
- 593. return -1;
- 594. }
- 595.
- 596. /**
- 597. * 为查询日期添加最小时间
- 598. *
- 599. * @param 目标类型Date
- 600. * @param 转换参数Date
- 601. * @return
- 602. */
- 603. @SuppressWarnings("deprecation")
- 604. public static Date addStartTime(Date param) {
- 605. Date date = param;
- 606. try {
- 607. date.setHours(0);
- 608. date.setMinutes(0);
- 609. date.setSeconds(0);
- 610. return date;
- 611. } catch (Exception ex) {
- 612. return date;
- 613. }
- 614. }
- 615.
- 616. /**
- 617. * 为查询日期添加最大时间
- 618. *
- 619. * @param 目标类型Date
- 620. * @param 转换参数Date
- 621. * @return
- 622. */
- 623. @SuppressWarnings("deprecation")
- 624. public static Date addEndTime(Date param) {
- 625. Date date = param;
- 626. try {
- 627. date.setHours(23);
- 628. date.setMinutes(59);
- 629. date.setSeconds(0);
- 630. return date;
- 631. } catch (Exception ex) {
- 632. return date;
- 633. }
- 634. }
- 635.
- 636. /**
- 637. * 返回系统现在年份中指定月份的天数
- 638. *
- 639. * @param 月份month
- 640. * @return 指定月的总天数
- 641. */
- 642. @SuppressWarnings("deprecation")
- 643. public static String getMonthLastDay(int month) {
- 644. Date date = new Date();
- 645. int[][] day = { { 0, 30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
- 646. { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
- 647. int year = date.getYear() + 1900;
- 648. if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
- 649. return day[1][month] + "";
- 650. } else {
- 651. return day[0][month] + "";
- 652. }
- 653. }
- 654.
- 655. /**
- 656. * 返回指定年份中指定月份的天数
- 657. *
- 658. * @param 年份year
- 659. * @param 月份month
- 660. * @return 指定月的总天数
- 661. */
- 662. public static String getMonthLastDay(int year, int month) {
- 663. int[][] day = { { 0, 30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
- 664. { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
- 665. if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
- 666. return day[1][month] + "";
- 667. } else {
- 668. return day[0][month] + "";
- 669. }
- 670. }
- 671.
- 672. /**
- 673. * 判断是平年还是闰年
- 674. * @author dylan_xu
- 675. * @date Mar 11, 2012
- 676. * @param year
- 677. * @return
- 678. */
- 679. public static boolean isLeapyear(int year) {
- 680. if ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0) {
- 681. return true;
- 682. } else {
- 683. return false;
- 684. }
- 685. }
- 686.
- 687. /**
- 688. * 取得当前时间的日戳
- 689. * @author dylan_xu
- 690. * @date Mar 11, 2012
- 691. * @return
- 692. */
- 693. @SuppressWarnings("deprecation")
- 694. public static String getTimestamp() {
- 695. Date date = cale.getTime();
- 696. String timestamp = "" + (date.getYear() + 1900) + date.getMonth()
- 697. + date.getDate() + date.getMinutes() + date.getSeconds()
- 698. + date.getTime();
- 699. return timestamp;
- 700. }
- 701.
- 702. /**
- 703. * 取得指定时间的日戳
- 704. *
- 705. * @return
- 706. */
- 707. @SuppressWarnings("deprecation")
- 708. public static String getTimestamp(Date date) {
- 709. String timestamp = "" + (date.getYear() + 1900) + date.getMonth()
- 710. + date.getDate() + date.getMinutes() + date.getSeconds()
- 711. + date.getTime();
- 712. return timestamp;
- 713. }
- 714.
- 715. public static void main(String[] args) {
- 716. System.out.println(getYear() + "|" + getMonth() + "|" + getDate());
- 717. }
- 718.}
复制代码 |
|