Calendar工具类


[java]  view plain  copy
  1. import java.util.Calendar;  
  2. import java.text.DateFormat;    
  3. import java.text.ParsePosition;    
  4. import java.text.SimpleDateFormat;    
  5. import java.util.Calendar;    
  6. import java.util.Date;    
  7. import java.util.GregorianCalendar;    
  8.     
  9. /**  
  10.  * 常用日历操作辅助类  
  11.  *  
  12.  *  
  13.  */    
  14. public class CalendarUtil {    
  15.     
  16.     // 用来全局控制 上一周,本周,下一周的周数变化    
  17.     private int weeks = 0;    
  18.     private int MaxDate;// 一月最大天数    
  19.     private int MaxYear;// 一年最大天数    
  20.     private static Calendar calendar = Calendar.getInstance();//实例化日历    
  21.     
  22.     /**测试  
  23.      * @param args  
  24.      */    
  25.     public static void main(String[] args) {    
  26.           
  27.         CalendarUtil tt = new CalendarUtil();    
  28.           
  29.         System.out.println("获取当天日期:" + tt.getNowTime("yyyy-MM-dd"));    
  30.         System.out.println("获取本周一日期:" + tt.getMondayOFWeek());    
  31.         System.out.println("获取本周日的日期~:" + tt.getCurrentWeekday());    
  32.         System.out.println("获取上周一日期:" + tt.getPreviousWeekday());    
  33.         System.out.println("获取上周日日期:" + tt.getPreviousWeekSunday());    
  34.         System.out.println("获取下周一日期:" + tt.getNextMonday());    
  35.         System.out.println("获取下周日日期:" + tt.getNextSunday());    
  36.         System.out.println("获得相应周的周六的日期:" + tt.getNowTime("yyyy-MM-dd"));    
  37.         System.out.println("获取本月第一天日期:" + tt.getFirstDayOfMonth());    
  38.         System.out.println("获取本月最后一天日期:" + tt.getDefaultDay());    
  39.         System.out.println("获取上月第一天日期:" + tt.getPreviousMonthFirst());    
  40.         System.out.println("获取上月最后一天的日期:" + tt.getPreviousMonthEnd());    
  41.         System.out.println("获取下月第一天日期:" + tt.getNextMonthFirst());    
  42.         System.out.println("获取下月最后一天日期:" + tt.getNextMonthEnd());    
  43.         System.out.println("获取本年的第一天日期:" + tt.getCurrentYearFirst());    
  44.         System.out.println("获取本年最后一天日期:" + tt.getCurrentYearEnd());    
  45.         System.out.println("获取去年的第一天日期:" + tt.getPreviousYearFirst());    
  46.         System.out.println("获取去年的最后一天日期:" + tt.getPreviousYearEnd());    
  47.         System.out.println("获取明年第一天日期:" + tt.getNextYearFirst());    
  48.         System.out.println("获取明年最后一天日期:" + tt.getNextYearEnd());    
  49.         System.out.println("获取本季度第一天:" + tt.getThisSeasonFirstTime(11));    
  50.         System.out.println("获取本季度最后一天:" + tt.getThisSeasonFinallyTime(11));    
  51.         System.out.println("获取两个日期之间间隔天数2008-12-1~2008-9.29:"    
  52.                 + CalendarUtil.getTwoDay("2008-12-1""2008-9-29"));    
  53.         System.out.println("---------------->");    
  54.         System.out.println("获取当前月的第几周:" + tt.getWeekOfMonth());    
  55.         System.out.println("获取当前年份:" + tt.getYear());    
  56.         System.out.println("获取当前月份:" + tt.getMonth());    
  57.         System.out.println("获取今天在本年的第几天:" + tt.getDayOfYear());    
  58.             
  59.         System.out.println("获得今天在本月的第几天(获得当前日):" + tt.getDayOfMonth());    
  60.         System.out.println("获得今天在本周的第几天:" + tt.getDayOfWeek());    
  61.         System.out.println("获得半年后的日期:" + tt.convertDateToString(tt.getTimeYearNext()));    
  62.     }    
  63.     
  64.     /**  
  65.      * 获得当前年份  
  66.      *   
  67.      * @return  
  68.      */    
  69.     public static int getYear() {    
  70.         return calendar.get(Calendar.YEAR);    
  71.     }    
  72.     
  73.     /**  
  74.      * 获得当前月份  
  75.      *   
  76.      * @return  
  77.      */    
  78.     public static int getMonth() {    
  79.         return calendar.get(Calendar.MONTH) + 1;    
  80.     }    
  81.     
  82.     /**  
  83.      * 获得今天在本年的第几天  
  84.      *   
  85.      * @return  
  86.      */    
  87.     public static int getDayOfYear() {    
  88.         return calendar.get(Calendar.DAY_OF_YEAR);    
  89.     }    
  90.     
  91.     /**  
  92.      * 获得今天在本月的第几天(获得当前日)  
  93.      *   
  94.      * @return  
  95.      */    
  96.     public static int getDayOfMonth() {    
  97.         return calendar.get(Calendar.DAY_OF_MONTH);    
  98.     }    
  99.     
  100.     /**  
  101.      * 获得今天在本周的第几天  
  102.      *   
  103.      * @return  
  104.      */    
  105.     public static int getDayOfWeek() {    
  106.         return calendar.get(Calendar.DAY_OF_WEEK);    
  107.     }    
  108.     
  109.     /**  
  110.      * 获得今天是这个月的第几周  
  111.      *   
  112.      * @return  
  113.      */    
  114.     public static int getWeekOfMonth() {    
  115.         return calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);    
  116.     }    
  117.     
  118.     /**  
  119.      * 获得半年后的日期  
  120.      *   
  121.      * @return  
  122.      */    
  123.     public static Date getTimeYearNext() {    
  124.         calendar.add(Calendar.DAY_OF_YEAR, 183);    
  125.         return calendar.getTime();    
  126.     }    
  127.         
  128.     public static String convertDateToString(Date dateTime){    
  129.         SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");    
  130.         return df.format(dateTime);    
  131.     }    
  132.         
  133.     /**  
  134.      * 得到二个日期间的间隔天数  
  135.      */    
  136.     public static String getTwoDay(String sj1, String sj2) {    
  137.         SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");    
  138.         long day = 0;    
  139.         try {    
  140.             java.util.Date date = myFormatter.parse(sj1);    
  141.             java.util.Date mydate = myFormatter.parse(sj2);    
  142.             day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);    
  143.         } catch (Exception e) {    
  144.             return "";    
  145.         }    
  146.         return day + "";    
  147.     }    
  148.     
  149.     /**  
  150.      * 根据一个日期,返回是星期几的字符串  
  151.      *   
  152.      * @param sdate  
  153.      * @return  
  154.      */    
  155.     public static String getWeek(String sdate) {    
  156.         // 再转换为时间    
  157.         Date date = CalendarUtil.strToDate(sdate);    
  158.         Calendar c = Calendar.getInstance();    
  159.         c.setTime(date);    
  160.         // int hour=c.get(Calendar.DAY_OF_WEEK);    
  161.         // hour中存的就是星期几了,其范围 1~7    
  162.         // 1=星期日 7=星期六,其他类推    
  163.         return new SimpleDateFormat("EEEE").format(c.getTime());    
  164.     }    
  165.     
  166.     /**  
  167.      * 将短时间格式字符串转换为时间 yyyy-MM-dd  
  168.      *   
  169.      * @param strDate  
  170.      * @return  
  171.      */    
  172.     public static Date strToDate(String strDate) {    
  173.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");    
  174.         ParsePosition pos = new ParsePosition(0);    
  175.         Date strtodate = formatter.parse(strDate, pos);    
  176.         return strtodate;    
  177.     }    
  178.     
  179.     /**  
  180.      * 两个时间之间的天数  
  181.      *   
  182.      * @param date1  
  183.      * @param date2  
  184.      * @return  
  185.      */    
  186.     public static long getDays(String date1, String date2) {    
  187.         if (date1 == null || date1.equals(""))    
  188.             return 0;    
  189.         if (date2 == null || date2.equals(""))    
  190.             return 0;    
  191.         // 转换为标准时间    
  192.         SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");    
  193.         java.util.Date date = null;    
  194.         java.util.Date mydate = null;    
  195.         try {    
  196.             date = myFormatter.parse(date1);    
  197.             mydate = myFormatter.parse(date2);    
  198.         } catch (Exception e) {    
  199.         }    
  200.         long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);    
  201.         return day;    
  202.     }    
  203.     
  204.     // 计算当月最后一天,返回字符串    
  205.     public String getDefaultDay() {    
  206.         String str = "";    
  207.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
  208.     
  209.         Calendar lastDate = Calendar.getInstance();    
  210.         lastDate.set(Calendar.DATE, 1);// 设为当前月的1号    
  211.         lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号    
  212.         lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天    
  213.     
  214.         str = sdf.format(lastDate.getTime());    
  215.         return str;    
  216.     }    
  217.     
  218.     // 上月第一天    
  219.     public String getPreviousMonthFirst() {    
  220.         String str = "";    
  221.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
  222.     
  223.         Calendar lastDate = Calendar.getInstance();    
  224.         lastDate.set(Calendar.DATE, 1);// 设为当前月的1号    
  225.         lastDate.add(Calendar.MONTH, -1);// 减一个月,变为下月的1号    
  226.         // lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天    
  227.     
  228.         str = sdf.format(lastDate.getTime());    
  229.         return str;    
  230.     }    
  231.     
  232.     // 获取当月第一天    
  233.     public String getFirstDayOfMonth() {    
  234.         String str = "";    
  235.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
  236.     
  237.         Calendar lastDate = Calendar.getInstance();    
  238.         lastDate.set(Calendar.DATE, 1);// 设为当前月的1号    
  239.         str = sdf.format(lastDate.getTime());    
  240.         return str;    
  241.     }    
  242.     
  243.     // 获得本周星期日的日期    
  244.     public String getCurrentWeekday() {    
  245.         weeks = 0;    
  246.         int mondayPlus = this.getMondayPlus();    
  247.         GregorianCalendar currentDate = new GregorianCalendar();    
  248.         currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);    
  249.         Date monday = currentDate.getTime();    
  250.     
  251.         DateFormat df = DateFormat.getDateInstance();    
  252.         String preMonday = df.format(monday);    
  253.         return preMonday;    
  254.     }    
  255.     
  256.     // 获取当天时间    
  257.     public String getNowTime(String dateformat) {    
  258.         Date now = new Date();    
  259.         SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);// 可以方便地修改日期格式    
  260.         String hehe = dateFormat.format(now);    
  261.         return hehe;    
  262.     }    
  263.     
  264.     // 获得当前日期与本周日相差的天数    
  265.     private int getMondayPlus() {    
  266.         Calendar cd = Calendar.getInstance();    
  267.         // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......    
  268.         int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1// 因为按中国礼拜一作为第一天所以这里减1    
  269.         if (dayOfWeek == 1) {    
  270.             return 0;    
  271.         } else {    
  272.             return 1 - dayOfWeek;    
  273.         }    
  274.     }    
  275.     
  276.     // 获得本周一的日期    
  277.     public String getMondayOFWeek() {    
  278.         weeks = 0;    
  279.         int mondayPlus = this.getMondayPlus();    
  280.         GregorianCalendar currentDate = new GregorianCalendar();    
  281.         currentDate.add(GregorianCalendar.DATE, mondayPlus);    
  282.         Date monday = currentDate.getTime();    
  283.     
  284.         DateFormat df = DateFormat.getDateInstance();    
  285.         String preMonday = df.format(monday);    
  286.         return preMonday;    
  287.     }    
  288.     
  289.     // 获得相应周的周六的日期    
  290.     public String getSaturday() {    
  291.         int mondayPlus = this.getMondayPlus();    
  292.         GregorianCalendar currentDate = new GregorianCalendar();    
  293.         currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6);    
  294.         Date monday = currentDate.getTime();    
  295.         DateFormat df = DateFormat.getDateInstance();    
  296.         String preMonday = df.format(monday);    
  297.         return preMonday;    
  298.     }    
  299.     
  300.     // 获得上周星期日的日期    
  301.     public String getPreviousWeekSunday() {    
  302.         weeks = 0;    
  303.         weeks--;    
  304.         int mondayPlus = this.getMondayPlus();    
  305.         GregorianCalendar currentDate = new GregorianCalendar();    
  306.         currentDate.add(GregorianCalendar.DATE, mondayPlus + weeks);    
  307.         Date monday = currentDate.getTime();    
  308.         DateFormat df = DateFormat.getDateInstance();    
  309.         String preMonday = df.format(monday);    
  310.         return preMonday;    
  311.     }    
  312.     
  313.     // 获得上周星期一的日期    
  314.     public String getPreviousWeekday() {    
  315.         weeks--;    
  316.         int mondayPlus = this.getMondayPlus();    
  317.         GregorianCalendar currentDate = new GregorianCalendar();    
  318.         currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);    
  319.         Date monday = currentDate.getTime();    
  320.         DateFormat df = DateFormat.getDateInstance();    
  321.         String preMonday = df.format(monday);    
  322.         return preMonday;    
  323.     }    
  324.     
  325.     // 获得下周星期一的日期    
  326.     public String getNextMonday() {    
  327.         weeks++;    
  328.         int mondayPlus = this.getMondayPlus();    
  329.         GregorianCalendar currentDate = new GregorianCalendar();    
  330.         currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);    
  331.         Date monday = currentDate.getTime();    
  332.         DateFormat df = DateFormat.getDateInstance();    
  333.         String preMonday = df.format(monday);    
  334.         return preMonday;    
  335.     }    
  336.     
  337.     // 获得下周星期日的日期    
  338.     public String getNextSunday() {    
  339.     
  340.         int mondayPlus = this.getMondayPlus();    
  341.         GregorianCalendar currentDate = new GregorianCalendar();    
  342.         currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 + 6);    
  343.         Date monday = currentDate.getTime();    
  344.         DateFormat df = DateFormat.getDateInstance();    
  345.         String preMonday = df.format(monday);    
  346.         return preMonday;    
  347.     }    
  348.     
  349.     private int getMonthPlus() {    
  350.         Calendar cd = Calendar.getInstance();    
  351.         int monthOfNumber = cd.get(Calendar.DAY_OF_MONTH);    
  352.         cd.set(Calendar.DATE, 1);// 把日期设置为当月第一天    
  353.         cd.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天    
  354.         MaxDate = cd.get(Calendar.DATE);    
  355.         if (monthOfNumber == 1) {    
  356.             return -MaxDate;    
  357.         } else {    
  358.             return 1 - monthOfNumber;    
  359.         }    
  360.     }    
  361.     
  362.     // 获得上月最后一天的日期    
  363.     public String getPreviousMonthEnd() {    
  364.         String str = "";    
  365.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
  366.     
  367.         Calendar lastDate = Calendar.getInstance();    
  368.         lastDate.add(Calendar.MONTH, -1);// 减一个月    
  369.         lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天    
  370.         lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天    
  371.         str = sdf.format(lastDate.getTime());    
  372.         return str;    
  373.     }    
  374.     
  375.     // 获得下个月第一天的日期    
  376.     public String getNextMonthFirst() {    
  377.         String str = "";    
  378.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
  379.     
  380.         Calendar lastDate = Calendar.getInstance();    
  381.         lastDate.add(Calendar.MONTH, 1);// 减一个月    
  382.         lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天    
  383.         str = sdf.format(lastDate.getTime());    
  384.         return str;    
  385.     }    
  386.     
  387.     // 获得下个月最后一天的日期    
  388.     public String getNextMonthEnd() {    
  389.         String str = "";    
  390.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
  391.     
  392.         Calendar lastDate = Calendar.getInstance();    
  393.         lastDate.add(Calendar.MONTH, 1);// 加一个月    
  394.         lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天    
  395.         lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天    
  396.         str = sdf.format(lastDate.getTime());    
  397.         return str;    
  398.     }    
  399.     
  400.     // 获得明年最后一天的日期    
  401.     public String getNextYearEnd() {    
  402.         String str = "";    
  403.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
  404.     
  405.         Calendar lastDate = Calendar.getInstance();    
  406.         lastDate.add(Calendar.YEAR, 1);// 加一个年    
  407.         lastDate.set(Calendar.DAY_OF_YEAR, 1);    
  408.         lastDate.roll(Calendar.DAY_OF_YEAR, -1);    
  409.         str = sdf.format(lastDate.getTime());    
  410.         return str;    
  411.     }    
  412.     
  413.     // 获得明年第一天的日期    
  414.     public String getNextYearFirst() {    
  415.         String str = "";    
  416.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
  417.     
  418.         Calendar lastDate = Calendar.getInstance();    
  419.         lastDate.add(Calendar.YEAR, 1);// 加一个年    
  420.         lastDate.set(Calendar.DAY_OF_YEAR, 1);    
  421.         str = sdf.format(lastDate.getTime());    
  422.         return str;    
  423.     
  424.     }    
  425.     
  426.     // 获得本年有多少天    
  427.     private int getMaxYear() {    
  428.         Calendar cd = Calendar.getInstance();    
  429.         cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天    
  430.         cd.roll(Calendar.DAY_OF_YEAR, -1);// 把日期回滚一天。    
  431.         int MaxYear = cd.get(Calendar.DAY_OF_YEAR);    
  432.         return MaxYear;    
  433.     }    
  434.     
  435.     private int getYearPlus() {    
  436.         Calendar cd = Calendar.getInstance();    
  437.         int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);// 获得当天是一年中的第几天    
  438.         cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天    
  439.         cd.roll(Calendar.DAY_OF_YEAR, -1);// 把日期回滚一天。    
  440.         int MaxYear = cd.get(Calendar.DAY_OF_YEAR);    
  441.         if (yearOfNumber == 1) {    
  442.             return -MaxYear;    
  443.         } else {    
  444.             return 1 - yearOfNumber;    
  445.         }    
  446.     }    
  447.     
  448.     // 获得本年第一天的日期    
  449.     public String getCurrentYearFirst() {    
  450.         int yearPlus = this.getYearPlus();    
  451.         GregorianCalendar currentDate = new GregorianCalendar();    
  452.         currentDate.add(GregorianCalendar.DATE, yearPlus);    
  453.         Date yearDay = currentDate.getTime();    
  454.         DateFormat df = DateFormat.getDateInstance();    
  455.         String preYearDay = df.format(yearDay);    
  456.         return preYearDay;    
  457.     }    
  458.     
  459.     // 获得本年最后一天的日期 *    
  460.     public String getCurrentYearEnd() {    
  461.         Date date = new Date();    
  462.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式    
  463.         String years = dateFormat.format(date);    
  464.         return years + "-12-31";    
  465.     }    
  466.     
  467.     // 获得上年第一天的日期 *    
  468.     public String getPreviousYearFirst() {    
  469.         Date date = new Date();    
  470.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式    
  471.         String years = dateFormat.format(date);    
  472.         int years_value = Integer.parseInt(years);    
  473.         years_value--;    
  474.         return years_value + "-1-1";    
  475.     }    
  476.     
  477.     // 获得上年最后一天的日期    
  478.     public String getPreviousYearEnd() {    
  479.         weeks--;    
  480.         int yearPlus = this.getYearPlus();    
  481.         GregorianCalendar currentDate = new GregorianCalendar();    
  482.         currentDate.add(GregorianCalendar.DATE, yearPlus + MaxYear * weeks    
  483.                 + (MaxYear - 1));    
  484.         Date yearDay = currentDate.getTime();    
  485.         DateFormat df = DateFormat.getDateInstance();    
  486.         String preYearDay = df.format(yearDay);    
  487.         return preYearDay;    
  488.     }    
  489.     
  490.     // 获得本季度第一天    
  491.     public String getThisSeasonFirstTime(int month) {    
  492.         int array[][] = { { 123 }, { 456 }, { 789 }, { 101112 } };    
  493.         int season = 1;    
  494.         if (month >= 1 && month <= 3) {    
  495.             season = 1;    
  496.         }    
  497.         if (month >= 4 && month <= 6) {    
  498.             season = 2;    
  499.         }    
  500.         if (month >= 7 && month <= 9) {    
  501.             season = 3;    
  502.         }    
  503.         if (month >= 10 && month <= 12) {    
  504.             season = 4;    
  505.         }    
  506.         int start_month = array[season - 1][0];    
  507.         int end_month = array[season - 1][2];    
  508.     
  509.         Date date = new Date();    
  510.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式    
  511.         String years = dateFormat.format(date);    
  512.         int years_value = Integer.parseInt(years);    
  513.     
  514.         int start_days = 1;// years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);    
  515.         int end_days = getLastDayOfMonth(years_value, end_month);    
  516.         String seasonDate = years_value + "-" + start_month + "-" + start_days;    
  517.         return seasonDate;    
  518.     
  519.     }    
  520.     
  521.     // 获得本季度最后一天    
  522.     public String getThisSeasonFinallyTime(int month) {    
  523.         int array[][] = { { 123 }, { 456 }, { 789 }, { 101112 } };    
  524.         int season = 1;    
  525.         if (month >= 1 && month <= 3) {    
  526.             season = 1;    
  527.         }    
  528.         if (month >= 4 && month <= 6) {    
  529.             season = 2;    
  530.         }    
  531.         if (month >= 7 && month <= 9) {    
  532.             season = 3;    
  533.         }    
  534.         if (month >= 10 && month <= 12) {    
  535.             season = 4;    
  536.         }    
  537.         int start_month = array[season - 1][0];    
  538.         int end_month = array[season - 1][2];    
  539.     
  540.         Date date = new Date();    
  541.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式    
  542.         String years = dateFormat.format(date);    
  543.         int years_value = Integer.parseInt(years);    
  544.     
  545.         int start_days = 1;// years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);    
  546.         int end_days = getLastDayOfMonth(years_value, end_month);    
  547.         String seasonDate = years_value + "-" + end_month + "-" + end_days;    
  548.         return seasonDate;    
  549.     
  550.     }    
  551.     
  552.     /**  
  553.      * 获取某年某月的最后一天  
  554.      *   
  555.      * @param year  
  556.      *            年  
  557.      * @param month  
  558.      *            月  
  559.      * @return 最后一天  
  560.      */    
  561.     private int getLastDayOfMonth(int year, int month) {    
  562.         if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8    
  563.                 || month == 10 || month == 12) {    
  564.             return 31;    
  565.         }    
  566.         if (month == 4 || month == 6 || month == 9 || month == 11) {    
  567.             return 30;    
  568.         }    
  569.         if (month == 2) {    
  570.             if (isLeapYear(year)) {    
  571.                 return 29;    
  572.             } else {    
  573.                 return 28;    
  574.             }    
  575.         }    
  576.         return 0;    
  577.     }     
  578.   
  579. /*** 
  580.      *  
  581.      *获取所在月的第一天日期 
  582.      * */  
  583.     public static String getFirstDayOfMonth(String date) {  
  584.   
  585.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  586.   
  587.         try {  
  588.   
  589.             calendar.setTime(sdf.parse(date));  
  590.             calendar.set(Calendar.DATE, 1);  
  591.   
  592.         } catch (ParseException e) {  
  593.   
  594.             e.printStackTrace();  
  595.         }  
  596.   
  597.         return sdf.format(calendar.getTime());  
  598.     }  
  599.       
  600.     /*** 
  601.      * 获取所在月的最后一天时间 
  602.      *  
  603.      * */  
  604.     public static String getLastDayOfMonth(String date) {  
  605.   
  606.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  607.   
  608.         try {  
  609.   
  610.             calendar.setTime(sdf.parse(date));  
  611.             calendar.set(Calendar.DATE, 1);// 设为当前月的1号       
  612.             calendar.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号       
  613.             calendar.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天   
  614.   
  615.         } catch (ParseException e) {  
  616.   
  617.             e.printStackTrace();  
  618.         }  
  619.   
  620.         return sdf.format(calendar.getTime());  
  621.     }  
  622.       
  623.     /*** 
  624.      *  
  625.      * 上月第一天 
  626.      *  
  627.      * */  
  628.     public static String getPreviousMonthFirst(String date) {  
  629.   
  630.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  631.   
  632.         try {  
  633.   
  634.             calendar.setTime(sdf.parse(date));  
  635.             calendar.set(Calendar.DATE, 1);// 设为当前月的1号       
  636.             calendar.add(Calendar.MONTH, -1);// 减一个月,变为下月的1号   
  637.   
  638.         } catch (ParseException e) {  
  639.   
  640.             e.printStackTrace();  
  641.         }  
  642.   
  643.         return sdf.format(calendar.getTime());  
  644.     }  
  645.       
  646.     /*** 
  647.      *  
  648.      * 上月最后一天 
  649.      *  
  650.      * */  
  651.     public static String getPreviousMonthEnd(String date) {  
  652.   
  653.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  654.   
  655.         try {  
  656.   
  657.             calendar.setTime(sdf.parse(date));  
  658.             calendar.add(Calendar.MONTH, -1);// 减一个月       
  659.             calendar.set(Calendar.DATE, 1);// 把日期设置为当月第一天       
  660.             calendar.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天    
  661.   
  662.         } catch (ParseException e) {  
  663.   
  664.             e.printStackTrace();  
  665.         }  
  666.   
  667.         return sdf.format(calendar.getTime());  
  668.     }  
  669.       
  670.   
  671.     /*** 
  672.      * 获取所在年的第一天 
  673.      *  
  674.      * **/  
  675.     public static String getFirstDayOfYear(String date) {  
  676.   
  677.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  678.   
  679.         try {  
  680.   
  681.             calendar.setTime(sdf.parse(date));  
  682.             calendar.set(Calendar.DAY_OF_YEAR, 1);  
  683.           
  684.   
  685.         } catch (ParseException e) {  
  686.   
  687.             e.printStackTrace();  
  688.         }  
  689.   
  690.         return sdf.format(calendar.getTime());  
  691.     }  
  692.   
  693.     /** 
  694.      *  
  695.      * 当前时间去年的日期 
  696.      * */  
  697.     public static String getDayOfLastYear(String date) {  
  698.   
  699.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  700.   
  701.         try {  
  702.   
  703.             calendar.setTime(sdf.parse(date));  
  704.             calendar.add(Calendar.YEAR, -1);  
  705.   
  706.         } catch (ParseException e) {  
  707.   
  708.             e.printStackTrace();  
  709.         }  
  710.   
  711.         return sdf.format(calendar.getTime());  
  712.     }  
  713.       
  714.     /**** 
  715.      *  
  716.      * 系统当前时间 
  717.      * */  
  718.     public static String getCurrentDate (){  
  719.           
  720.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  721.   
  722.         return sdf.format(calendar.getTime());  
  723.     }  
  724.   
  725.   /** 
  726.    * 得到几天前的时间 
  727.    * @param d 
  728.    * @param day 
  729.    * @return 
  730.    */  
  731.   public static Date getDateBefore(Date d,int day){  
  732.    Calendar now =Calendar.getInstance();  
  733.    now.setTime(d);  
  734.    now.set(Calendar.DATE,now.get(Calendar.DATE)-day);  
  735.    return now.getTime();  
  736.   }  
  737.     
  738.   /** 
  739.    * 得到几天后的时间 
  740.    * @param d 
  741.    * @param day 
  742.    * @return 
  743.    */  
  744.   public static Date getDateAfter(Date d,int day){  
  745.    Calendar now =Calendar.getInstance();  
  746.    now.setTime(d);  
  747.    now.set(Calendar.DATE,now.get(Calendar.DATE)+day);  
  748.    return now.getTime();  
  749.   }  
[java]  view plain  copy
  1. /**** 
  2.      *  
  3.      *  
  4.      * 计算月份差 
  5.      *  
  6.      * **/  
  7.     public static int getMonthSpace(String firstTime ,String lastTime ){  
  8.           
  9.         SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");  
  10.           
  11.         Calendar c1 = Calendar.getInstance();  
  12.         Calendar c2 = Calendar.getInstance();  
  13.         try {  
  14.             c1.setTime(sdf.parse(lastTime));  
  15.             c2.setTime(sdf.parse(firstTime));  
  16.         } catch (ParseException e) {  
  17.             e.printStackTrace();          
  18.         }  
  19.           
  20.         int result =(c1.get(Calendar.YEAR)-c2.get(Calendar.YEAR))*12+(c1.get(Calendar.MONTH)-c2.get(Calendar.MONTH));  
  21.           
  22.           
  23.         return result ==0 ? 1 : result;  
  24.     }  


  • 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、付费专栏及课程。

余额充值