Calendar类的使用api获取时间

Calendar类的使用api介绍:


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

猜你喜欢

转载自blog.csdn.net/liudongdong19/article/details/80541250