Java获取各种常用时间方法2

  1.       
  2.     // 获得下周星期一的日期  
  3.     public String getNextMonday() {  
  4.          weeks++;  
  5.         int mondayPlus = this.getMondayPlus();  
  6.          GregorianCalendar currentDate = new GregorianCalendar();  
  7.          currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);  
  8.          Date monday = currentDate.getTime();  
  9.          DateFormat df = DateFormat.getDateInstance();  
  10.          String preMonday = df.format(monday);  
  11.         return preMonday;  
  12.      }  
  13.       
  14. // 获得下周星期日的日期  
  15.     public String getNextSunday() {  
  16.           
  17.         int mondayPlus = this.getMondayPlus();  
  18.          GregorianCalendar currentDate = new GregorianCalendar();  
  19.          currentDate.add(GregorianCalendar.DATE, mondayPlus + 7+6);  
  20.          Date monday = currentDate.getTime();  
  21.          DateFormat df = DateFormat.getDateInstance();  
  22.          String preMonday = df.format(monday);  
  23.         return preMonday;  
  24.      }  
  25.       
  26.       
  27.       
  28.     private int getMonthPlus(){  
  29.          Calendar cd = Calendar.getInstance();  
  30.         int monthOfNumber = cd.get(Calendar.DAY_OF_MONTH);  
  31.          cd.set(Calendar.DATE, 1);//把日期设置为当月第一天   
  32.          cd.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天   
  33.          MaxDate=cd.get(Calendar.DATE);   
  34.         if(monthOfNumber == 1){  
  35.             return -MaxDate;  
  36.          }else{  
  37.             return 1-monthOfNumber;  
  38.          }  
  39.      }  
  40.       
  41.   //获得上月最后一天的日期  
  42.     public String getPreviousMonthEnd(){  
  43.          String str = "";  
  44.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
  45.   
  46.         Calendar lastDate = Calendar.getInstance();  
  47.        lastDate.add(Calendar.MONTH,-1);//减一个月  
  48.        lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天   
  49.        lastDate.roll(Calendar.DATE, -1);//日期回滚一天,也就是本月最后一天   
  50.         str=sdf.format(lastDate.getTime());  
  51.        return str;    
  52.      }  
  53.       
  54.   //获得下个月第一天的日期  
  55.     public String getNextMonthFirst(){  
  56.          String str = "";  
  57.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
  58.   
  59.         Calendar lastDate = Calendar.getInstance();  
  60.        lastDate.add(Calendar.MONTH,1);//减一个月  
  61.        lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天   
  62.         str=sdf.format(lastDate.getTime());  
  63.        return str;    
  64.      }  
  65.       
  66.   //获得下个月最后一天的日期  
  67.     public String getNextMonthEnd(){  
  68.          String str = "";  
  69.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
  70.   
  71.         Calendar lastDate = Calendar.getInstance();  
  72.        lastDate.add(Calendar.MONTH,1);//加一个月  
  73.        lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天   
  74.        lastDate.roll(Calendar.DATE, -1);//日期回滚一天,也就是本月最后一天   
  75.         str=sdf.format(lastDate.getTime());  
  76.        return str;    
  77.      }  
  78.       
  79.     //获得明年最后一天的日期  
  80.     public String getNextYearEnd(){  
  81.          String str = "";  
  82.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
  83.   
  84.        Calendar lastDate = Calendar.getInstance();  
  85.        lastDate.add(Calendar.YEAR,1);//加一个年  
  86.        lastDate.set(Calendar.DAY_OF_YEAR, 1);  
  87.       lastDate.roll(Calendar.DAY_OF_YEAR, -1);  
  88.         str=sdf.format(lastDate.getTime());  
  89.        return str;    
  90.      }  
  91.       
  92.   //获得明年第一天的日期  
  93.     public String getNextYearFirst(){  
  94.          String str = "";  
  95.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
  96.   
  97.        Calendar lastDate = Calendar.getInstance();  
  98.        lastDate.add(Calendar.YEAR,1);//加一个年  
  99.        lastDate.set(Calendar.DAY_OF_YEAR, 1);  
  100.             str=sdf.format(lastDate.getTime());  
  101.       return str;    
  102.           
  103.      }  
  104.       
  105.   //获得本年有多少天  
  106.     private int getMaxYear(){  
  107.          Calendar cd = Calendar.getInstance();  
  108.          cd.set(Calendar.DAY_OF_YEAR,1);//把日期设为当年第一天  
  109.          cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期回滚一天。  
  110.         int MaxYear = cd.get(Calendar.DAY_OF_YEAR);   
  111.         return MaxYear;  
  112.      }  
  113.       
  114.     private int getYearPlus(){  
  115.          Calendar cd = Calendar.getInstance();  
  116.         int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);//获得当天是一年中的第几天  
  117.          cd.set(Calendar.DAY_OF_YEAR,1);//把日期设为当年第一天  
  118.          cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期回滚一天。  
  119.         int MaxYear = cd.get(Calendar.DAY_OF_YEAR);  
  120.         if(yearOfNumber == 1){  
  121.             return -MaxYear;  
  122.          }else{  
  123.             return 1-yearOfNumber;  
  124.          }  
  125.      }  
  126.   //获得本年第一天的日期  
  127.     public String getCurrentYearFirst(){  
  128.         int yearPlus = this.getYearPlus();  
  129.          GregorianCalendar currentDate = new GregorianCalendar();  
  130.          currentDate.add(GregorianCalendar.DATE,yearPlus);  
  131.          Date yearDay = currentDate.getTime();  
  132.          DateFormat df = DateFormat.getDateInstance();  
  133.          String preYearDay = df.format(yearDay);  
  134.         return preYearDay;  
  135.      }  
  136.       
  137.       
  138.   //获得本年最后一天的日期 *  
  139.     public String getCurrentYearEnd(){  
  140.          Date date = new Date();  
  141.          SimpleDateFormat    dateFormat    =   new    SimpleDateFormat("yyyy");//可以方便地修改日期格式     
  142.          String   years   = dateFormat.format(date);     
  143.         return years+"-12-31";  
  144.      }  
  145.       
  146.       
  147.   //获得上年第一天的日期 *  
  148.     public String getPreviousYearFirst(){  
  149.          Date date = new Date();  
  150.          SimpleDateFormat    dateFormat    =   new    SimpleDateFormat("yyyy");//可以方便地修改日期格式     
  151.          String   years   = dateFormat.format(date); int years_value = Integer.parseInt(years);    
  152.          years_value--;  
  153.         return years_value+"-1-1";  
  154.      }  
  155.       
  156.   //获得上年最后一天的日期  
  157.     public String getPreviousYearEnd(){  
  158.          weeks--;  
  159.         int yearPlus = this.getYearPlus();  
  160.          GregorianCalendar currentDate = new GregorianCalendar();  
  161.          currentDate.add(GregorianCalendar.DATE,yearPlus+MaxYear*weeks+(MaxYear-1));  
  162.          Date yearDay = currentDate.getTime();  
  163.          DateFormat df = DateFormat.getDateInstance();  
  164.          String preYearDay = df.format(yearDay);  
  165.          getThisSeasonTime(11);  
  166.         return preYearDay;  
  167.      }  
  168.       
  169.   //获得本季度  
  170.     public String getThisSeasonTime(int month){  
  171.         int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};  
  172.         int season = 1;  
  173.         if(month>=1&&month<=3){  
  174.              season = 1;  
  175.          }  
  176.         if(month>=4&&month<=6){  
  177.              season = 2;  
  178.          }  
  179.         if(month>=7&&month<=9){  
  180.              season = 3;  
  181.          }  
  182.         if(month>=10&&month<=12){  
  183.              season = 4;  
  184.          }  
  185.         int start_month = array[season-1][0];  
  186.         int end_month = array[season-1][2];  
  187.           
  188.          Date date = new Date();  
  189.          SimpleDateFormat    dateFormat    =   new    SimpleDateFormat("yyyy");//可以方便地修改日期格式     
  190.          String   years   = dateFormat.format(date);     
  191.         int years_value = Integer.parseInt(years);  
  192.           
  193.         int start_days =1;//years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);  
  194.         int end_days = getLastDayOfMonth(years_value,end_month);  
  195.          String seasonDate = years_value+"-"+start_month+"-"+start_days+";"+years_value+"-"+end_month+"-"+end_days;  
  196.         return seasonDate;  
  197.           
  198.      }  
  199.       
  200.     /**
  201.       * 获取某年某月的最后一天
  202.       * @param year 年
  203.       * @param month 月
  204.       * @return 最后一天
  205.       */  
  206.    private int getLastDayOfMonth(int year, int month) {  
  207.          if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8  
  208.                     || month == 10 || month == 12) {  
  209.                return 31;  
  210.           }  
  211.          if (month == 4 || month == 6 || month == 9 || month == 11) {  
  212.                return 30;  
  213.           }  
  214.          if (month == 2) {  
  215.                if (isLeapYear(year)) {  
  216.                    return 29;  
  217.                 } else {  
  218.                    return 28;  
  219.                 }  
  220.           }  
  221.          return 0;  
  222.     }  
  223.    /**
  224.      * 是否闰年
  225.      * @param year 年
  226.      * @return
  227.      */  
  228.   public boolean isLeapYear(int year) {  
  229.         return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);  
  230.    }  
  231. }

posted on 2009-01-15 10:13  dhj  阅读(125)  评论(0编辑  收藏  举报

导航