日历封装类

  1. package  com.iwode.common;  
  2.   
  3. import  java.text.DateFormat;  
  4. import  java.text.ParsePosition;  
  5. import  java.text.SimpleDateFormat;  
  6. import  java.util.Calendar;  
  7. import  java.util.Date;  
  8. import  java.util.GregorianCalendar;  
  9.   
  10. /**  
  11.  * 常用日历操作辅助类  
  12.  * @author steven 2010-08-10  
  13.  * @email:qing.tan@iwode.com  
  14.  */   
  15. public   class  CalendarUtil {  
  16.   
  17.     // 用来全局控制 上一周,本周,下一周的周数变化   
  18.     private   int  weeks =  0 ;  
  19.     private   int  MaxDate; // 一月最大天数   
  20.     private   int  MaxYear; // 一年最大天数   
  21.     private   static  Calendar calendar = Calendar.getInstance(); //实例化日历   
  22.   
  23.     /**测试  
  24.      * @param args  
  25.      */   
  26.     public   static   void  main(String[] args) {  
  27.         CalendarUtil tt = new  CalendarUtil();  
  28.         System.out.println("获取当天日期:"  + tt.getNowTime( "yyyy-MM-dd" ));  
  29.         System.out.println("获取本周一日期:"  + tt.getMondayOFWeek());  
  30.         System.out.println("获取本周日的日期~:"  + tt.getCurrentWeekday());  
  31.         System.out.println("获取上周一日期:"  + tt.getPreviousWeekday());  
  32.         System.out.println("获取上周日日期:"  + tt.getPreviousWeekSunday());  
  33.         System.out.println("获取下周一日期:"  + tt.getNextMonday());  
  34.         System.out.println("获取下周日日期:"  + tt.getNextSunday());  
  35.         System.out.println("获得相应周的周六的日期:"  + tt.getNowTime( "yyyy-MM-dd" ));  
  36.         System.out.println("获取本月第一天日期:"  + tt.getFirstDayOfMonth());  
  37.         System.out.println("获取本月最后一天日期:"  + tt.getDefaultDay());  
  38.         System.out.println("获取上月第一天日期:"  + tt.getPreviousMonthFirst());  
  39.         System.out.println("获取上月最后一天的日期:"  + tt.getPreviousMonthEnd());  
  40.         System.out.println("获取下月第一天日期:"  + tt.getNextMonthFirst());  
  41.         System.out.println("获取下月最后一天日期:"  + tt.getNextMonthEnd());  
  42.         System.out.println("获取本年的第一天日期:"  + tt.getCurrentYearFirst());  
  43.         System.out.println("获取本年最后一天日期:"  + tt.getCurrentYearEnd());  
  44.         System.out.println("获取去年的第一天日期:"  + tt.getPreviousYearFirst());  
  45.         System.out.println("获取去年的最后一天日期:"  + tt.getPreviousYearEnd());  
  46.         System.out.println("获取明年第一天日期:"  + tt.getNextYearFirst());  
  47.         System.out.println("获取明年最后一天日期:"  + tt.getNextYearEnd());  
  48.         System.out.println("获取本季度第一天:"  + tt.getThisSeasonFirstTime( 11 ));  
  49.         System.out.println("获取本季度最后一天:"  + tt.getThisSeasonFinallyTime( 11 ));  
  50.         System.out.println("获取两个日期之间间隔天数2008-12-1~2008-9.29:"   
  51.                 + CalendarUtil.getTwoDay("2008-12-1""2008-9-29" ));  
  52.         System.out.println("---------------->" );  
  53.         System.out.println("获取当前月的第几周:"  + tt.getWeekOfMonth());  
  54.         System.out.println("获取当前年份:"  + tt.getYear());  
  55.         System.out.println("获取当前月份:"  + tt.getMonth());  
  56.         System.out.println("获取今天在本年的第几天:"  + tt.getDayOfYear());  
  57.           
  58.         System.out.println("获得今天在本月的第几天(获得当前日):"  + tt.getDayOfMonth());  
  59.         System.out.println("获得今天在本周的第几天:"  + tt.getDayOfWeek());  
  60.         System.out.println("获得半年后的日期:"  + tt.convertDateToString(tt.getTimeYearNext()));  
  61.     }  
  62.   
  63.     /**  
  64.      * 获得当前年份  
  65.      *   
  66.      * @return  
  67.      */   
  68.     public   static   int  getYear() {  
  69.         return  calendar.get(Calendar.YEAR);  
  70.     }  
  71.   
  72.     /**  
  73.      * 获得当前月份  
  74.      *   
  75.      * @return  
  76.      */   
  77.     public   static   int  getMonth() {  
  78.         return  calendar.get(Calendar.MONTH) +  1 ;  
  79.     }  
  80.   
  81.     /**  
  82.      * 获得今天在本年的第几天  
  83.      *   
  84.      * @return  
  85.      */   
  86.     public   static   int  getDayOfYear() {  
  87.         return  calendar.get(Calendar.DAY_OF_YEAR);  
  88.     }  
  89.   
  90.     /**  
  91.      * 获得今天在本月的第几天(获得当前日)  
  92.      *   
  93.      * @return  
  94.      */   
  95.     public   static   int  getDayOfMonth() {  
  96.         return  calendar.get(Calendar.DAY_OF_MONTH);  
  97.     }  
  98.   
  99.     /**  
  100.      * 获得今天在本周的第几天  
  101.      *   
  102.      * @return  
  103.      */   
  104.     public   static   int  getDayOfWeek() {  
  105.         return  calendar.get(Calendar.DAY_OF_WEEK);  
  106.     }  
  107.   
  108.     /**  
  109.      * 获得今天是这个月的第几周  
  110.      *   
  111.      * @return  
  112.      */   
  113.     public   static   int  getWeekOfMonth() {  
  114.         return  calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);  
  115.     }  
  116.   
  117.     /**  
  118.      * 获得半年后的日期  
  119.      *   
  120.      * @return  
  121.      */   
  122.     public   static  Date getTimeYearNext() {  
  123.         calendar.add(Calendar.DAY_OF_YEAR, 183 );  
  124.         return  calendar.getTime();  
  125.     }  
  126.       
  127.     public   static  String convertDateToString(Date dateTime){  
  128.         SimpleDateFormat df=new  SimpleDateFormat( "yyyy-MM-dd" );  
  129.         return  df.format(dateTime);  
  130.     }  
  131.       
  132.     /**  
  133.      * 得到二个日期间的间隔天数  
  134.      */   
  135.     public   static  String getTwoDay(String sj1, String sj2) {  
  136.         SimpleDateFormat myFormatter = new  SimpleDateFormat( "yyyy-MM-dd" );  
  137.         long  day =  0 ;  
  138.         try  {  
  139.             java.util.Date date = myFormatter.parse(sj1);  
  140.             java.util.Date mydate = myFormatter.parse(sj2);  
  141.             day = (date.getTime() - mydate.getTime()) / (24  *  60  *  60  *  1000 );  
  142.         } catch  (Exception e) {  
  143.             return   "" ;  
  144.         }  
  145.         return  day +  "" ;  
  146.     }  
  147.   
  148.     /**  
  149.      * 根据一个日期,返回是星期几的字符串  
  150.      *   
  151.      * @param sdate  
  152.      * @return  
  153.      */   
  154.     public   static  String getWeek(String sdate) {  
  155.         // 再转换为时间   
  156.         Date date = CalendarUtil.strToDate(sdate);  
  157.         Calendar c = Calendar.getInstance();  
  158.         c.setTime(date);  
  159.         // int hour=c.get(Calendar.DAY_OF_WEEK);   
  160.         // hour中存的就是星期几了,其范围 1~7   
  161.         // 1=星期日 7=星期六,其他类推   
  162.         return   new  SimpleDateFormat( "EEEE" ).format(c.getTime());  
  163.     }  
  164.   
  165.     /**  
  166.      * 将短时间格式字符串转换为时间 yyyy-MM-dd  
  167.      *   
  168.      * @param strDate  
  169.      * @return  
  170.      */   
  171.     public   static  Date strToDate(String strDate) {  
  172.         SimpleDateFormat formatter = new  SimpleDateFormat( "yyyy-MM-dd" );  
  173.         ParsePosition pos = new  ParsePosition( 0 );  
  174.         Date strtodate = formatter.parse(strDate, pos);  
  175.         return  strtodate;  
  176.     }  
  177.   
  178.     /**  
  179.      * 两个时间之间的天数  
  180.      *   
  181.      * @param date1  
  182.      * @param date2  
  183.      * @return  
  184.      */   
  185.     public   static   long  getDays(String date1, String date2) {  
  186.         if  (date1 ==  null  || date1.equals( "" ))  
  187.             return   0 ;  
  188.         if  (date2 ==  null  || date2.equals( "" ))  
  189.             return   0 ;  
  190.         // 转换为标准时间   
  191.         SimpleDateFormat myFormatter = new  SimpleDateFormat( "yyyy-MM-dd" );  
  192.         java.util.Date date = null ;  
  193.         java.util.Date mydate = null ;  
  194.         try  {  
  195.             date = myFormatter.parse(date1);  
  196.             mydate = myFormatter.parse(date2);  
  197.         } catch  (Exception e) {  
  198.         }  
  199.         long  day = (date.getTime() - mydate.getTime()) / ( 24  *  60  *  60  *  1000 );  
  200.         return  day;  
  201.     }  
  202.   
  203.     // 计算当月最后一天,返回字符串   
  204.     public  String getDefaultDay() {  
  205.         String str = "" ;  
  206.         SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd" );  
  207.   
  208.         Calendar lastDate = Calendar.getInstance();  
  209.         lastDate.set(Calendar.DATE, 1 ); // 设为当前月的1号   
  210.         lastDate.add(Calendar.MONTH, 1 ); // 加一个月,变为下月的1号   
  211.         lastDate.add(Calendar.DATE, -1 ); // 减去一天,变为当月最后一天   
  212.   
  213.         str = sdf.format(lastDate.getTime());  
  214.         return  str;  
  215.     }  
  216.   
  217.     // 上月第一天   
  218.     public  String getPreviousMonthFirst() {  
  219.         String str = "" ;  
  220.         SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd" );  
  221.   
  222.         Calendar lastDate = Calendar.getInstance();  
  223.         lastDate.set(Calendar.DATE, 1 ); // 设为当前月的1号   
  224.         lastDate.add(Calendar.MONTH, -1 ); // 减一个月,变为下月的1号   
  225.         // lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天   
  226.   
  227.         str = sdf.format(lastDate.getTime());  
  228.         return  str;  
  229.     }  
  230.   
  231.     // 获取当月第一天   
  232.     public  String getFirstDayOfMonth() {  
  233.         String str = "" ;  
  234.         SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd" );  
  235.   
  236.         Calendar lastDate = Calendar.getInstance();  
  237.         lastDate.set(Calendar.DATE, 1 ); // 设为当前月的1号   
  238.         str = sdf.format(lastDate.getTime());  
  239.         return  str;  
  240.     }  
  241.   
  242.     // 获得本周星期日的日期   
  243.     public  String getCurrentWeekday() {  
  244.         weeks = 0 ;  
  245.         int  mondayPlus =  this .getMondayPlus();  
  246.         GregorianCalendar currentDate = new  GregorianCalendar();  
  247.         currentDate.add(GregorianCalendar.DATE, mondayPlus + 6 );  
  248.         Date monday = currentDate.getTime();  
  249.   
  250.         DateFormat df = DateFormat.getDateInstance();  
  251.         String preMonday = df.format(monday);  
  252.         return  preMonday;  
  253.     }  
  254.   
  255.     // 获取当天时间   
  256.     public  String getNowTime(String dateformat) {  
  257.         Date now = new  Date();  
  258.         SimpleDateFormat dateFormat = new  SimpleDateFormat(dateformat); // 可以方便地修改日期格式   
  259.         String hehe = dateFormat.format(now);  
  260.         return  hehe;  
  261.     }  
  262.   
  263.     // 获得当前日期与本周日相差的天数   
  264.     private   int  getMondayPlus() {  
  265.         Calendar cd = Calendar.getInstance();  
  266.         // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......   
  267.         int  dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) -  1// 因为按中国礼拜一作为第一天所以这里减1   
  268.         if  (dayOfWeek ==  1 ) {  
  269.             return   0 ;  
  270.         } else  {  
  271.             return   1  - dayOfWeek;  
  272.         }  
  273.     }  
  274.   
  275.     // 获得本周一的日期   
  276.     public  String getMondayOFWeek() {  
  277.         weeks = 0 ;  
  278.         int  mondayPlus =  this .getMondayPlus();  
  279.         GregorianCalendar currentDate = new  GregorianCalendar();  
  280.         currentDate.add(GregorianCalendar.DATE, mondayPlus);  
  281.         Date monday = currentDate.getTime();  
  282.   
  283.         DateFormat df = DateFormat.getDateInstance();  
  284.         String preMonday = df.format(monday);  
  285.         return  preMonday;  
  286.     }  
  287.   
  288.     // 获得相应周的周六的日期   
  289.     public  String getSaturday() {  
  290.         int  mondayPlus =  this .getMondayPlus();  
  291.         GregorianCalendar currentDate = new  GregorianCalendar();  
  292.         currentDate.add(GregorianCalendar.DATE, mondayPlus + 7  * weeks +  6 );  
  293.         Date monday = currentDate.getTime();  
  294.         DateFormat df = DateFormat.getDateInstance();  
  295.         String preMonday = df.format(monday);  
  296.         return  preMonday;  
  297.     }  
  298.   
  299.     // 获得上周星期日的日期   
  300.     public  String getPreviousWeekSunday() {  
  301.         weeks = 0 ;  
  302.         weeks--;  
  303.         int  mondayPlus =  this .getMondayPlus();  
  304.         GregorianCalendar currentDate = new  GregorianCalendar();  
  305.         currentDate.add(GregorianCalendar.DATE, mondayPlus + weeks);  
  306.         Date monday = currentDate.getTime();  
  307.         DateFormat df = DateFormat.getDateInstance();  
  308.         String preMonday = df.format(monday);  
  309.         return  preMonday;  
  310.     }  
  311.   
  312.     // 获得上周星期一的日期   
  313.     public  String getPreviousWeekday() {  
  314.         weeks--;  
  315.         int  mondayPlus =  this .getMondayPlus();  
  316.         GregorianCalendar currentDate = new  GregorianCalendar();  
  317.         currentDate.add(GregorianCalendar.DATE, mondayPlus + 7  * weeks);  
  318.         Date monday = currentDate.getTime();  
  319.         DateFormat df = DateFormat.getDateInstance();  
  320.         String preMonday = df.format(monday);  
  321.         return  preMonday;  
  322.     }  
  323.   
  324.     // 获得下周星期一的日期   
  325.     public  String getNextMonday() {  
  326.         weeks++;  
  327.         int  mondayPlus =  this .getMondayPlus();  
  328.         GregorianCalendar currentDate = new  GregorianCalendar();  
  329.         currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 );  
  330.         Date monday = currentDate.getTime();  
  331.         DateFormat df = DateFormat.getDateInstance();  
  332.         String preMonday = df.format(monday);  
  333.         return  preMonday;  
  334.     }  
  335.   
  336.     // 获得下周星期日的日期   
  337.     public  String getNextSunday() {  
  338.   
  339.         int  mondayPlus =  this .getMondayPlus();  
  340.         GregorianCalendar currentDate = new  GregorianCalendar();  
  341.         currentDate.add(GregorianCalendar.DATE, mondayPlus + 7  +  6 );  
  342.         Date monday = currentDate.getTime();  
  343.         DateFormat df = DateFormat.getDateInstance();  
  344.         String preMonday = df.format(monday);  
  345.         return  preMonday;  
  346.     }  
  347.   
  348.     private   int  getMonthPlus() {  
  349.         Calendar cd = Calendar.getInstance();  
  350.         int  monthOfNumber = cd.get(Calendar.DAY_OF_MONTH);  
  351.         cd.set(Calendar.DATE, 1 ); // 把日期设置为当月第一天   
  352.         cd.roll(Calendar.DATE, -1 ); // 日期回滚一天,也就是最后一天   
  353.         MaxDate = cd.get(Calendar.DATE);  
  354.         if  (monthOfNumber ==  1 ) {  
  355.             return  -MaxDate;  
  356.         } else  {  
  357.             return   1  - monthOfNumber;  
  358.         }  
  359.     }  
  360.   
  361.     // 获得上月最后一天的日期   
  362.     public  String getPreviousMonthEnd() {  
  363.         String str = "" ;  
  364.         SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd" );  
  365.   
  366.         Calendar lastDate = Calendar.getInstance();  
  367.         lastDate.add(Calendar.MONTH, -1 ); // 减一个月   
  368.         lastDate.set(Calendar.DATE, 1 ); // 把日期设置为当月第一天   
  369.         lastDate.roll(Calendar.DATE, -1 ); // 日期回滚一天,也就是本月最后一天   
  370.         str = sdf.format(lastDate.getTime());  
  371.         return  str;  
  372.     }  
  373.   
  374.     // 获得下个月第一天的日期   
  375.     public  String getNextMonthFirst() {  
  376.         String str = "" ;  
  377.         SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd" );  
  378.   
  379.         Calendar lastDate = Calendar.getInstance();  
  380.         lastDate.add(Calendar.MONTH, 1 ); // 减一个月   
  381.         lastDate.set(Calendar.DATE, 1 ); // 把日期设置为当月第一天   
  382.         str = sdf.format(lastDate.getTime());  
  383.         return  str;  
  384.     }  
  385.   
  386.     // 获得下个月最后一天的日期   
  387.     public  String getNextMonthEnd() {  
  388.         String str = "" ;  
  389.         SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd" );  
  390.   
  391.         Calendar lastDate = Calendar.getInstance();  
  392.         lastDate.add(Calendar.MONTH, 1 ); // 加一个月   
  393.         lastDate.set(Calendar.DATE, 1 ); // 把日期设置为当月第一天   
  394.         lastDate.roll(Calendar.DATE, -1 ); // 日期回滚一天,也就是本月最后一天   
  395.         str = sdf.format(lastDate.getTime());  
  396.         return  str;  
  397.     }  
  398.   
  399.     // 获得明年最后一天的日期   
  400.     public  String getNextYearEnd() {  
  401.         String str = "" ;  
  402.         SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd" );  
  403.   
  404.         Calendar lastDate = Calendar.getInstance();  
  405.         lastDate.add(Calendar.YEAR, 1 ); // 加一个年   
  406.         lastDate.set(Calendar.DAY_OF_YEAR, 1 );  
  407.         lastDate.roll(Calendar.DAY_OF_YEAR, -1 );  
  408.         str = sdf.format(lastDate.getTime());  
  409.         return  str;  
  410.     }  
  411.   
  412.     // 获得明年第一天的日期   
  413.     public  String getNextYearFirst() {  
  414.         String str = "" ;  
  415.         SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd" );  
  416.   
  417.         Calendar lastDate = Calendar.getInstance();  
  418.         lastDate.add(Calendar.YEAR, 1 ); // 加一个年   
  419.         lastDate.set(Calendar.DAY_OF_YEAR, 1 );  
  420.         str = sdf.format(lastDate.getTime());  
  421.         return  str;  
  422.   
  423.     }  
  424.   
  425.     // 获得本年有多少天   
  426.     private   int  getMaxYear() {  
  427.         Calendar cd = Calendar.getInstance();  
  428.         cd.set(Calendar.DAY_OF_YEAR, 1 ); // 把日期设为当年第一天   
  429.         cd.roll(Calendar.DAY_OF_YEAR, -1 ); // 把日期回滚一天。   
  430.         int  MaxYear = cd.get(Calendar.DAY_OF_YEAR);  
  431.         return  MaxYear;  
  432.     }  
  433.   
  434.     private   int  getYearPlus() {  
  435.         Calendar cd = Calendar.getInstance();  
  436.         int  yearOfNumber = cd.get(Calendar.DAY_OF_YEAR); // 获得当天是一年中的第几天   
  437.         cd.set(Calendar.DAY_OF_YEAR, 1 ); // 把日期设为当年第一天   
  438.         cd.roll(Calendar.DAY_OF_YEAR, -1 ); // 把日期回滚一天。   
  439.         int  MaxYear = cd.get(Calendar.DAY_OF_YEAR);  
  440.         if  (yearOfNumber ==  1 ) {  
  441.             return  -MaxYear;  
  442.         } else  {  
  443.             return   1  - yearOfNumber;  
  444.         }  
  445.     }  
  446.   
  447.     // 获得本年第一天的日期   
  448.     public  String getCurrentYearFirst() {  
  449.         int  yearPlus =  this .getYearPlus();  
  450.         GregorianCalendar currentDate = new  GregorianCalendar();  
  451.         currentDate.add(GregorianCalendar.DATE, yearPlus);  
  452.         Date yearDay = currentDate.getTime();  
  453.         DateFormat df = DateFormat.getDateInstance();  
  454.         String preYearDay = df.format(yearDay);  
  455.         return  preYearDay;  
  456.     }  
  457.   
  458.     // 获得本年最后一天的日期 *   
  459.     public  String getCurrentYearEnd() {  
  460.         Date date = new  Date();  
  461.         SimpleDateFormat dateFormat = new  SimpleDateFormat( "yyyy" ); // 可以方便地修改日期格式   
  462.         String years = dateFormat.format(date);  
  463.         return  years +  "-12-31" ;  
  464.     }  
  465.   
  466.     // 获得上年第一天的日期 *   
  467.     public  String getPreviousYearFirst() {  
  468.         Date date = new  Date();  
  469.         SimpleDateFormat dateFormat = new  SimpleDateFormat( "yyyy" ); // 可以方便地修改日期格式   
  470.         String years = dateFormat.format(date);  
  471.         int  years_value = Integer.parseInt(years);  
  472.         years_value--;  
  473.         return  years_value +  "-1-1" ;  
  474.     }  
  475.   
  476.     // 获得上年最后一天的日期   
  477.     public  String getPreviousYearEnd() {  
  478.         weeks--;  
  479.         int  yearPlus =  this .getYearPlus();  
  480.         GregorianCalendar currentDate = new  GregorianCalendar();  
  481.         currentDate.add(GregorianCalendar.DATE, yearPlus + MaxYear * weeks  
  482.                 + (MaxYear - 1 ));  
  483.         Date yearDay = currentDate.getTime();  
  484.         DateFormat df = DateFormat.getDateInstance();  
  485.         String preYearDay = df.format(yearDay);  
  486.         return  preYearDay;  
  487.     }  
  488.   
  489.     // 获得本季度第一天   
  490.     public  String getThisSeasonFirstTime( int  month) {  
  491.         int  array[][] = { {  123  }, {  456  }, {  789  }, {  101112  } };  
  492.         int  season =  1 ;  
  493.         if  (month >=  1  && month <=  3 ) {  
  494.             season = 1 ;  
  495.         }  
  496.         if  (month >=  4  && month <=  6 ) {  
  497.             season = 2 ;  
  498.         }  
  499.         if  (month >=  7  && month <=  9 ) {  
  500.             season = 3 ;  
  501.         }  
  502.         if  (month >=  10  && month <=  12 ) {  
  503.             season = 4 ;  
  504.         }  
  505.         int  start_month = array[season -  1 ][ 0 ];  
  506.         int  end_month = array[season -  1 ][ 2 ];  
  507.   
  508.         Date date = new  Date();  
  509.         SimpleDateFormat dateFormat = new  SimpleDateFormat( "yyyy" ); // 可以方便地修改日期格式   
  510.         String years = dateFormat.format(date);  
  511.         int  years_value = Integer.parseInt(years);  
  512.   
  513.         int  start_days =  1 ; // years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);   
  514.         int  end_days = getLastDayOfMonth(years_value, end_month);  
  515.         String seasonDate = years_value + "-"  + start_month +  "-"  + start_days;  
  516.         return  seasonDate;  
  517.   
  518.     }  
  519.   
  520.     // 获得本季度最后一天   
  521.     public  String getThisSeasonFinallyTime( int  month) {  
  522.         int  array[][] = { {  123  }, {  456  }, {  789  }, {  101112  } };  
  523.         int  season =  1 ;  
  524.         if  (month >=  1  && month <=  3 ) {  
  525.             season = 1 ;  
  526.         }  
  527.         if  (month >=  4  && month <=  6 ) {  
  528.             season = 2 ;  
  529.         }  
  530.         if  (month >=  7  && month <=  9 ) {  
  531.             season = 3 ;  
  532.         }  
  533.         if  (month >=  10  && month <=  12 ) {  
  534.             season = 4 ;  
  535.         }  
  536.         int  start_month = array[season -  1 ][ 0 ];  
  537.         int  end_month = array[season -  1 ][ 2 ];  
  538.   
  539.         Date date = new  Date();  
  540.         SimpleDateFormat dateFormat = new  SimpleDateFormat( "yyyy" ); // 可以方便地修改日期格式   
  541.         String years = dateFormat.format(date);  
  542.         int  years_value = Integer.parseInt(years);  
  543.   
  544.         int  start_days =  1 ; // years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);   
  545.         int  end_days = getLastDayOfMonth(years_value, end_month);  
  546.         String seasonDate = years_value + "-"  + end_month +  "-"  + end_days;  
  547.         return  seasonDate;  
  548.   
  549.     }  
  550.   
  551.     /**  
  552.      * 获取某年某月的最后一天  
  553.      *   
  554.      * @param year  
  555.      *            年  
  556.      * @param month  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值