Android Calendar的运用

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