188
189 做成方法190
191 import java.util.*;192 import java.text.*;193 importjava.util.Calendar;194
195 public classVeDate { 311 /**
312 * 得到现在时间313 *314 *@return
315 */
316 public staticDate getNow() {317 Date currentTime = newDate();318 returncurrentTime;319 }320
321 /**
322 * 提取一个月中的最后一天323 *324 *@paramday325 *@return
326 */
327 public static Date getLastDate(longday) {328 Date date = newDate();329 long date_3_hm = date.getTime() – 3600000 * 34 *day;330 Date date_3_hm_date = newDate(date_3_hm);331 returndate_3_hm_date;332 }333
334 /**
335 * 得到现在时间336 *337 *@return字符串 yyyyMMdd HHmmss338 */
339 public staticString getStringToday() {340 Date currentTime = newDate();341 SimpleDateFormat formatter = new SimpleDateFormat(“yyyyMMdd HHmmss”);342 String dateString =formatter.format(currentTime);343 returndateString;344 }345
346 /**
347 * 得到现在小时348 */
349 public staticString getHour() {350 Date currentTime = newDate();351 SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);352 String dateString =formatter.format(currentTime);353 String hour;354 hour = dateString.substring(11, 13);355 returnhour;356 }357
358 /**
359 * 得到现在分钟360 *361 *@return
362 */
363 public staticString getTime() {364 Date currentTime = newDate();365 SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);366 String dateString =formatter.format(currentTime);367 String min;368 min = dateString.substring(14, 16);369 returnmin;370 }371
372 /**
373 * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。374 *375 *@paramsformat376 * yyyyMMddhhmmss377 *@return
378 */
379 public staticString getUserDate(String sformat) {380 Date currentTime = newDate();381 SimpleDateFormat formatter = newSimpleDateFormat(sformat);382 String dateString =formatter.format(currentTime);383 returndateString;384 }385
386 /**
387 * 二个小时时间间的差值,必须保证二个时间都是”HH:MM”的格式,返回字符型的分钟388 */
389 public staticString getTwoHour(String st1, String st2) {390 String[] kk = null;391 String[] jj = null;392 kk = st1.split(“:”);393 jj = st2.split(“:”);394 if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))395 return “0”;396 else{397 double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;398 double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;399 if ((y – u) > 0)400 return y – u + “”;401 else
402 return “0”;403 }404 }405
406 /**
407 * 得到二个日期间的间隔天数408 */
409 public staticString getTwoDay(String sj1, String sj2) {410 SimpleDateFormat myFormatter = new SimpleDateFormat(“yyyy-MM-dd”);411 long day = 0;412 try{413 java.util.Date date =myFormatter.parse(sj1);414 java.util.Date mydate =myFormatter.parse(sj2);415 day = (date.getTime() – mydate.getTime()) / (24 * 60 * 60 * 1000);416 } catch(Exception e) {417 return “”;418 }419 return day + “”;420 }421
422 /**
423 * 时间前推或后推分钟,其中JJ表示分钟.424 */
425 public staticString getPreTime(String sj1, String jj) {426 SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);427 String mydate1 = “”;428 try{429 Date date1 =format.parse(sj1);430 long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;431 date1.setTime(Time * 1000);432 mydate1 =format.format(date1);433 } catch(Exception e) {434 }435 returnmydate1;436 }437
438 /**
439 * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数440 */
441 public staticString getNextDay(String nowdate, String delay) {442 try{443 SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd”);444 String mdate = “”;445 Date d =strToDate(nowdate);446 long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;447 d.setTime(myTime * 1000);448 mdate =format.format(d);449 returnmdate;450 }catch(Exception e){451 return “”;452 }453 }454
455 /**
456 * 判断是否润年457 *458 *@paramddate459 *@return
460 */
461 public static booleanisLeapYear(String ddate) {462
463 /**
464 * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年465 * 3.能被4整除同时能被100整除则不是闰年466 */
467 Date d =strToDate(ddate);468 GregorianCalendar gc =(GregorianCalendar) Calendar.getInstance();469 gc.setTime(d);470 int year =gc.get(Calendar.YEAR);471 if ((year % 400) == 0)472 return true;473 else if ((year % 4) == 0) {474 if ((year % 100) == 0)475 return false;476 else
477 return true;478 } else
479 return false;480 }481
482 /**
483 * 返回美国时间格式 26 Apr 2006484 *485 *@paramstr486 *@return
487 */
488 public staticString getEDate(String str) {489 SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd”);490 ParsePosition pos = new ParsePosition(0);491 Date strtodate =formatter.parse(str, pos);492 String j =strtodate.toString();493 String[] k = j.split(” “);494 return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);495 }496
497 /**
498 * 获取一个月的最后一天499 *500 *@paramdat501 *@return
502 */
503 public static String getEndDateOfMonth(String dat) {//yyyy-MM-dd
504 String str = dat.substring(0, 8);505 String month = dat.substring(5, 7);506 int mon =Integer.parseInt(month);507 if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {508 str += “31”;509 } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {510 str += “30”;511 } else{512 if(isLeapYear(dat)) {513 str += “29”;514 } else{515 str += “28”;516 }517 }518 returnstr;519 }520
521 /**
522 * 判断二个时间是否在同一个周523 *524 *@paramdate1525 *@paramdate2526 *@return
527 */
528 public static booleanisSameWeekDates(Date date1, Date date2) {529 Calendar cal1 =Calendar.getInstance();530 Calendar cal2 =Calendar.getInstance();531 cal1.setTime(date1);532 cal2.setTime(date2);533 int subYear = cal1.get(Calendar.YEAR) -cal2.get(Calendar.YEAR);534 if (0 ==subYear) {535 if (cal1.get(Calendar.WEEK_OF_YEAR) ==cal2.get(Calendar.WEEK_OF_YEAR))536 return true;537 } else if (1 == subYear && 11 ==cal2.get(Calendar.MONTH)) {538 //如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
539 if (cal1.get(Calendar.WEEK_OF_YEAR) ==cal2.get(Calendar.WEEK_OF_YEAR))540 return true;541 } else if (-1 == subYear && 11 ==cal1.get(Calendar.MONTH)) {542 if (cal1.get(Calendar.WEEK_OF_YEAR) ==cal2.get(Calendar.WEEK_OF_YEAR))543 return true;544 }545 return false;546 }547
548 /**
549 * 产生周序列,即得到当前时间所在的年度是第几周550 *551 *@return
552 */
553 public staticString getSeqWeek() {554 Calendar c =Calendar.getInstance(Locale.CHINA);555 String week =Integer.toString(c.get(Calendar.WEEK_OF_YEAR));556 if (week.length() == 1)557 week = “0” +week;558 String year =Integer.toString(c.get(Calendar.YEAR));559 return year +week;560 }561
562 /**
563 * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号564 *565 *@paramsdate566 *@paramnum567 *@return
568 */
569 public staticString getWeek(String sdate, String num) {570 //再转换为时间
571 Date dd =VeDate.strToDate(sdate);572 Calendar c =Calendar.getInstance();573 c.setTime(dd);574 if (num.equals(“1”)) //返回星期一所在的日期
575 c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);576 else if (num.equals(“2”)) //返回星期二所在的日期
577 c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);578 else if (num.equals(“3”)) //返回星期三所在的日期
579 c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);580 else if (num.equals(“4”)) //返回星期四所在的日期
581 c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);582 else if (num.equals(“5”)) //返回星期五所在的日期
583 c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);584 else if (num.equals(“6”)) //返回星期六所在的日期
585 c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);586 else if (num.equals(“0”)) //返回星期日所在的日期
587 c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);588 return new SimpleDateFormat(“yyyy-MM-dd”).format(c.getTime());589 }590
591 /**
592 * 根据一个日期,返回是星期几的字符串593 *594 *@paramsdate595 *@return
596 */
597 public staticString getWeek(String sdate) {598 //再转换为时间
599 Date date =VeDate.strToDate(sdate);600 Calendar c =Calendar.getInstance();601 c.setTime(date);602 //int hour=c.get(Calendar.DAY_OF_WEEK);603 //hour中存的就是星期几了,其范围 1~7604 //1=星期日 7=星期六,其他类推
605 return new SimpleDateFormat(“EEEE”).format(c.getTime());606 }607 public staticString getWeekStr(String sdate){608 String str = “”;609 str =VeDate.getWeek(sdate);610 if(“1”.equals(str)){611 str = “星期日”;612 }else if(“2”.equals(str)){613 str = “星期一”;614 }else if(“3”.equals(str)){615 str = “星期二”;616 }else if(“4”.equals(str)){617 str = “星期三”;618 }else if(“5”.equals(str)){619 str = “星期四”;620 }else if(“6”.equals(str)){621 str = “星期五”;622 }else if(“7”.equals(str)){623 str = “星期六”;624 }625 returnstr;626 }627
628 /**
629 * 两个时间之间的天数630 *631 *@paramdate1632 *@paramdate2633 *@return
634 */
635 public static longgetDays(String date1, String date2) {636 if (date1 == null || date1.equals(“”))637 return 0;638 if (date2 == null || date2.equals(“”))639 return 0;640 //转换为标准时间
641 SimpleDateFormat myFormatter = new SimpleDateFormat(“yyyy-MM-dd”);642 java.util.Date date = null;643 java.util.Date mydate = null;644 try{645 date =myFormatter.parse(date1);646 mydate =myFormatter.parse(date2);647 } catch(Exception e) {648 }649 long day = (date.getTime() – mydate.getTime()) / (24 * 60 * 60 * 1000);650 returnday;651 }652
653 /**
654 * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间655 * 此函数返回该日历第一行星期日所在的日期656 *657 *@paramsdate658 *@return
659 */
660 public staticString getNowMonth(String sdate) {661 //取该时间所在月的一号
662 sdate = sdate.substring(0, 8) + “01”;663
664 //得到这个月的1号是星期几
665 Date date =VeDate.strToDate(sdate);666 Calendar c =Calendar.getInstance();667 c.setTime(date);668 int u =c.get(Calendar.DAY_OF_WEEK);669 String newday = VeDate.getNextDay(sdate, (1 – u) + “”);670 returnnewday;671 }672
673 /**
674 * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数675 *676 *@paramk677 * 表示是取几位随机数,可以自己定678 */
679
680 public static String getNo(intk) {681
682 return getUserDate(“yyyyMMddhhmmss”) +getRandom(k);683 }684
685 /**
686 * 返回一个随机数687 *688 *@parami689 *@return
690 */
691 public static String getRandom(inti) {692 Random jjj = newRandom();693 //int suiJiShu = jjj.nextInt(9);
694 if (i == 0)695 return “”;696 String jj = “”;697 for (int k = 0; k < i; k++) {698 jj = jj + jjj.nextInt(9);699 }700 returnjj;701 }702
703 /**
704 *705 *@paramargs706 */
707 public static booleanRightDate(String date) {708
709 SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);710 ;711 if (date == null)712 return false;713 if (date.length() > 10) {714 sdf = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);715 } else{716 sdf = new SimpleDateFormat(“yyyy-MM-dd”);717 }718 try{719 sdf.parse(date);720 } catch(ParseException pe) {721 return false;722 }723 return true;724 }725
726 /***************************************************************************727 * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1728 * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回729 **************************************************************************/
730 public staticString getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {731 Date currentTime = newDate();732 SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd”);733 String dateString =formatter.format(currentTime);734 String s_nd = dateString.substring(0, 4); //年份
735 String s_yf = dateString.substring(5, 7); //月份
736 String s_rq = dateString.substring(8, 10); //日期
737 String sreturn = “”;738 roc.util.MyChar mc = newroc.util.MyChar();739 if (sdate == null || sdate.equals(“”) || !mc.Isdate(sdate)) { //处理空值情况
740 if (nd.equals(“1”)) {741 sreturn =s_nd;742 //处理间隔符
743 if (format.equals(“1”))744 sreturn = sreturn + “年”;745 else if (format.equals(“2”))746 sreturn = sreturn + “-“;747 else if (format.equals(“3”))748 sreturn = sreturn + “/”;749 else if (format.equals(“5”))750 sreturn = sreturn + “.”;751 }752 //处理月份
753 if (yf.equals(“1”)) {754 sreturn = sreturn +s_yf;755 if (format.equals(“1”))756 sreturn = sreturn + “月”;757 else if (format.equals(“2”))758 sreturn = sreturn + “-“;759 else if (format.equals(“3”))760 sreturn = sreturn + “/”;761 else if (format.equals(“5”))762 sreturn = sreturn + “.”;763 }764 //处理日期
765 if (rq.equals(“1”)) {766 sreturn = sreturn +s_rq;767 if (format.equals(“1”))768 sreturn = sreturn + “日”;769 }770 } else{771 //不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式
772 sdate =roc.util.RocDate.getOKDate(sdate);773 s_nd = sdate.substring(0, 4); //年份
774 s_yf = sdate.substring(5, 7); //月份
775 s_rq = sdate.substring(8, 10); //日期
776 if (nd.equals(“1”)) {777 sreturn =s_nd;778 //处理间隔符
779 if (format.equals(“1”))780 sreturn = sreturn + “年”;781 else if (format.equals(“2”))782 sreturn = sreturn + “-“;783 else if (format.equals(“3”))784 sreturn = sreturn + “/”;785 else if (format.equals(“5”))786 sreturn = sreturn + “.”;787 }788 //处理月份
789 if (yf.equals(“1”)) {790 sreturn = sreturn +s_yf;791 if (format.equals(“1”))792 sreturn = sreturn + “月”;793 else if (format.equals(“2”))794 sreturn = sreturn + “-“;795 else if (format.equals(“3”))796 sreturn = sreturn + “/”;797 else if (format.equals(“5”))798 sreturn = sreturn + “.”;799 }800 //处理日期
801 if (rq.equals(“1”)) {802 sreturn = sreturn +s_rq;803 if (format.equals(“1”))804 sreturn = sreturn + “日”;805 }806 }807 returnsreturn;808 }809
810 public static String getNextMonthDay(String sdate, intm) {811 sdate =getOKDate(sdate);812 int year = Integer.parseInt(sdate.substring(0, 4));813 int month = Integer.parseInt(sdate.substring(5, 7));814 month = month +m;815 if (month < 0) {816 month = month + 12;817 year = year – 1;818 } else if (month > 12) {819 month = month – 12;820 year = year + 1;821 }822 String smonth = “”;823 if (month < 10)824 smonth = “0” +month;825 else
826 smonth = “” +month;827 return year + “-” + smonth + “-10”;828 }829
830 public staticString getOKDate(String sdate) {831 if (sdate == null || sdate.equals(“”))832 returngetStringDateShort();833
834 if (!VeStr.Isdate(sdate)) {835 sdate =getStringDateShort();836 }837 //将“/”转换为“-”
838 sdate = VeStr.Replace(sdate, “/”, “-“);839 //如果只有8位长度,则要进行转换
840 if (sdate.length() == 8)841 sdate = sdate.substring(0, 4) + “-” + sdate.substring(4, 6) + “-” + sdate.substring(6, 8);842 SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd”);843 ParsePosition pos = new ParsePosition(0);844 Date strtodate =formatter.parse(sdate, pos);845 String dateString =formatter.format(strtodate);846 returndateString;847 }848
849 public static void main(String[] args) throwsException {850 try{851 //System.out.print(Integer.valueOf(getTwoDay(“2006-11-03 12:22:10”, “2006-11-02 11:22:09”)));
852 } catch(Exception e) {853 throw newException();854 }855 //System.out.println(“sss”);
856 }