java的calendar类常见日期处理

包含功能:
1.获取当前月的上一个月
2.得到当前月的第一天,返回2016-06-01这样的字符串
3.获取当前年的第一个月,返回2016-01这样的数据
4.获取当前年已过的所有季度的集合(季度包含的月份未完则也算入)
5.获取昨天
6.获取指定时间范围内的所有日期
7.获取指定时间范围内的所有月份
8.获取指定日期范围的所有周的周一到周日,格式为周一~周日,如2016-09-19为周一,2016-09-25为周天,返回结果为[2016-09-19~2016-09-25]
9.获取当前日期的前index天(以当前日期开始计算) index = -6表示当天日期的前7天 index = -29表示当前日期的前30天
10.获取当天
11.获取最近3个月开始日期(包含当前月所在月)
12.获取指定日期为星期几
13.获取指定日期所在月的第一天
14.获取指定日期所在月的最后一天
15.获取指定日期所在周的周一

16.获取指定日期所在周的周日

17.获取当前时间到月末的秒数


[java]  view plain  copy
  1. /** 
  2.      * 得到当前月的上一个月,返回2016-06这样的字符串 
  3.      */  
  4.     public static String getLastMonth() {  
  5.         Calendar cal = Calendar.getInstance();  
  6.         cal.add(Calendar.MONTH, -1);  
  7.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");  
  8.         String date = format.format(cal.getTime());  
  9.         return date;  
  10.     }  
  11.   
  12.     /** 
  13.      * 得到当前月的第一天,返回2016-06-01这样的字符串 
  14.      *  
  15.      * @return 
  16.      */  
  17.     public static String getCurrentMonthFirstDay() {  
  18.         Calendar c = Calendar.getInstance();  
  19.         c.add(Calendar.MONTH, 0);  
  20.         c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天  
  21.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  22.         String first = format.format(c.getTime());  
  23.         return first;  
  24.     }  
  25.   
  26.     public static String getCurrentMonthTheLastDay() {  
  27.         Calendar c = Calendar.getInstance();  
  28.         c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));  
  29.         return dateToStr(c.getTime(), "yyyy-MM-dd");  
  30.     }  
  31.   
  32.     /** 
  33.      * 获取当前年的第一个月,返回2016-01这样的数据 
  34.      *  
  35.      * @return 
  36.      */  
  37.     public static String getCurrentYearFirstMonth() {  
  38.         Calendar c = Calendar.getInstance();  
  39.         c.add(Calendar.YEAR, 0);  
  40.         c.set(Calendar.DAY_OF_YEAR, 1);  
  41.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");  
  42.         String first = format.format(c.getTime());  
  43.         return first;  
  44.     }  
  45.   
  46.     /** 
  47.      * 获取当前年已过的所有季度的集合(季度包含的月份未完则也算入)<String> 
  48.      */  
  49.     public static List<String> getAllQuarterOfThisYear() {  
  50.         List<String> quarters = new ArrayList<String>();  
  51.         Calendar cal = Calendar.getInstance();  
  52.         int year = cal.get(Calendar.YEAR);  
  53.         int month = cal.get(Calendar.MONTH) + 1;  
  54.         if (month > 9) {  
  55.             quarters.add(year + "-" + 1);  
  56.             quarters.add(year + "-" + 2);  
  57.             quarters.add(year + "-" + 3);  
  58.             quarters.add(year + "-" + 4);  
  59.             return quarters;  
  60.         }  
  61.         if (month > 6 && month <= 9) {  
  62.             quarters.add(year + "-" + 1);  
  63.             quarters.add(year + "-" + 2);  
  64.             quarters.add(year + "-" + 3);  
  65.             return quarters;  
  66.         }  
  67.         if (month > 3 && month <= 6) {  
  68.             quarters.add(year + "-" + 1);  
  69.             quarters.add(year + "-" + 2);  
  70.             return quarters;  
  71.         }  
  72.         if (month >= 1 && month <= 3) {  
  73.             quarters.add(year + "-" + 1);  
  74.             return quarters;  
  75.         }  
  76.         return null;  
  77.     }  
  78.   
  79.     /** 
  80.      * 获取昨天 
  81.      *  
  82.      * @return 
  83.      */  
  84.     public static String getYesterDay() {  
  85.         Calendar cal = Calendar.getInstance();  
  86.         cal.add(Calendar.DATE, -1);  
  87.         String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());  
  88.         return yesterday;  
  89.     }  
  90.   
  91.     /** 
  92.      * 获取指定时间范围内的所有日期 
  93.      *  
  94.      * @param startDate 
  95.      *            (format="2016-06-01") 
  96.      * @param endDate 
  97.      *            (format="2016-07-12") 
  98.      * @return 
  99.      * @throws ParseException 
  100.      */  
  101.     public static List<String> getDaysBetween(String startDate, String endDate) {  
  102.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  103.         List<String> result = new ArrayList<String>();  
  104.         result.add(startDate);  
  105.         try {  
  106.             Calendar calBegin = Calendar.getInstance();  
  107.             // 使用给定的 Date 设置此 Calendar 的时间  
  108.             Date begin = format.parse(startDate);  
  109.             calBegin.setTime(begin);  
  110.             Calendar calEnd = Calendar.getInstance();  
  111.             // 使用给定的 Date 设置此 Calendar 的时间  
  112.             Date end = format.parse(endDate);  
  113.             calEnd.setTime(end);  
  114.             // 测试此日期是否在指定日期之后  
  115.             while (end.after(calBegin.getTime())) {  
  116.                 // 根据日历的规则,为给定的日历字段添加或减去指定的时间量  
  117.                 calBegin.add(Calendar.DAY_OF_MONTH, 1);  
  118.                 result.add(format.format(calBegin.getTime()));  
  119.             }  
  120.         } catch (Exception e) {  
  121.             e.printStackTrace();  
  122.         }  
  123.         return result;  
  124.     }  
  125.   
  126.     /** 
  127.      * 获取指定时间范围内的所有月份 
  128.      *  
  129.      * @param startDate(format="2016-06") 
  130.      * @param endDate(format="2016-08") 
  131.      * @return 
  132.      * @throws ParseException 
  133.      */  
  134.     public static List<String> getMonthsBetween(String startDate, String endDate) {  
  135.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");  
  136.         List<String> result = new ArrayList<String>();  
  137.         try {  
  138.             Calendar calBegin = Calendar.getInstance();  
  139.             Calendar calEnd = Calendar.getInstance();  
  140.             calBegin.setTime(sdf.parse(startDate));  
  141.             calBegin.set(calBegin.get(Calendar.YEAR), calBegin.get(Calendar.MONTH), 1);  
  142.             calEnd.setTime(sdf.parse(endDate));  
  143.             calEnd.set(calEnd.get(Calendar.YEAR), calEnd.get(Calendar.MONTH), 2);  
  144.             Calendar curr = calBegin;  
  145.             while (curr.before(calEnd)) {  
  146.                 result.add(sdf.format(curr.getTime()));  
  147.                 curr.add(Calendar.MONTH, 1);  
  148.             }  
  149.         } catch (Exception e) {  
  150.             e.printStackTrace();  
  151.         }  
  152.         return result;  
  153.     }  
  154.   
  155.     /** 
  156.      * 获取指定日期范围的所有周的周一~周日,格式为周一~周日,如2016-09-19为周一,2016-09-25为周天,返回结果为[2016-09-19 
  157.      * ~2016-09-25] 
  158.      *  
  159.      * @param startDate 
  160.      *            起始时间(2016-09-19) 
  161.      * @param endDate 
  162.      *            结束时间(2016-09-25) 
  163.      * @return 
  164.      * @throws ParseException 
  165.      */  
  166.     public static List<String> getWeeksBetween(String startDate, String endDate) {  
  167.         // startDate不能大于endDate  
  168.         List<String> result = new ArrayList<String>();  
  169.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式  
  170.         try {  
  171.             Calendar calBegin = Calendar.getInstance();  
  172.             // 使用给定的 Date 设置此 Calendar 的时间  
  173.             Date begin = sdf.parse(startDate);  
  174.             calBegin.setTime(begin);  
  175.             int dayWeek = calBegin.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
  176.             if (1 == dayWeek) {  
  177.                 calBegin.add(Calendar.DAY_OF_MONTH, -1);  
  178.             }  
  179.             calBegin.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一  
  180.             Calendar calEnd = Calendar.getInstance();  
  181.             // 使用给定的 Date 设置此 Calendar 的时间  
  182.             Date end = sdf.parse(endDate);  
  183.             calEnd.setTime(end);  
  184.             // 测试此日期是否在指定日期之后  
  185.             while (end.after(calBegin.getTime())) {  
  186.                 // 根据日历的规则,为给定的日历字段添加或减去指定的时间量  
  187.                 calBegin.add(Calendar.DAY_OF_MONTH, 1);  
  188.                 int day = calBegin.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
  189.                 calBegin.add(Calendar.DATE, calBegin.getFirstDayOfWeek() - day);// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值  
  190.                 // 获取周一  
  191.                 String monday = sdf.format(calBegin.getTime());  
  192.                 calBegin.add(Calendar.DATE, 6);  
  193.                 // 获取周日  
  194.                 String sunday = sdf.format(calBegin.getTime());  
  195.                 result.add(monday + "~" + sunday);  
  196.             }  
  197.         } catch (Exception e) {  
  198.             e.printStackTrace();  
  199.         }  
  200.         return result;  
  201.     }  
  202.   
  203.     /** 
  204.      * 获取当前日期的前index天(以当前日期开始计算) index = -6表示当天日期的前7天 index = -29表示当前日期的前30天 
  205.      *  
  206.      * @return 
  207.      */  
  208.     public static String getLastIndexDayStr(int index) {  
  209.         Date now = new Date();  
  210.         Calendar c = Calendar.getInstance();  
  211.         c.setTime(now);  
  212.         c.add(Calendar.DAY_OF_MONTH, index);  
  213.         return dateToStr(c.getTime(), "yyyy-MM-dd");  
  214.     }  
  215.   
  216.     /** 
  217.      * 获取当天 
  218.      */  
  219.     public static String getTodayStr() {  
  220.         Date now = new Date();  
  221.         return dateToStr(now, "yyyy-MM-dd");  
  222.     }  
  223.   
  224.     /** 
  225.      * 获取最近3个月开始日期(包含当前月所有月) 
  226.      *  
  227.      * @return 
  228.      */  
  229.     public static String getLast3MonthsStartDateStr() {  
  230.         Date now = new Date();  
  231.         Calendar c = Calendar.getInstance();  
  232.         c.setTime(now);  
  233.         c.add(Calendar.MONTH, -2);  
  234.         return dateToStr(c.getTime(), "yyyy-MM") + "-01";  
  235.     }  
  236.   
  237.     /** 
  238.      * 获取星期几 
  239.      * @param dateStr 
  240.      * @return 
  241.      */  
  242.     public static String getWeekNameOfDate(String dateStr) {  
  243.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  244.         Calendar c = Calendar.getInstance();  
  245.         try {  
  246.             c.setTime(format.parse(dateStr));  
  247.         } catch (ParseException e) {  
  248.             e.printStackTrace();  
  249.         }  
  250.         int dayForWeek = 0;  
  251.         if (c.get(Calendar.DAY_OF_WEEK) == 1) {  
  252.             dayForWeek = 7;  
  253.         } else {  
  254.             dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;  
  255.         }  
  256.         String result = "";  
  257.         switch (dayForWeek) {  
  258.         case 1:  
  259.             result = "星期一";  
  260.             break;  
  261.         case 2:  
  262.             result = "星期二";  
  263.             break;  
  264.         case 3:  
  265.             result = "星期三";  
  266.             break;  
  267.         case 4:  
  268.             result = "星期四";  
  269.             break;  
  270.         case 5:  
  271.             result = "星期五";  
  272.             break;  
  273.         case 6:  
  274.             result = "星期六";  
  275.             break;  
  276.         case 7:  
  277.             result = "星期天";  
  278.             break;  
  279.         }  
  280.         return result;  
  281.     }  
  282.   
  283.     /** 
  284.      * 获取指定日期所在月的第一天 
  285.      *  
  286.      * @param date 
  287.      * @return yyyy-MM-dd 
  288.      */  
  289.     public static String getMonthStartDate(String date) {  
  290.         try {  
  291.             Calendar calendar = Calendar.getInstance();  
  292.             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  293.             calendar.setTime(format.parse(date));  
  294.             calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));  
  295.             return format.format(calendar.getTime());  
  296.         } catch (ParseException e) {  
  297.             e.printStackTrace();  
  298.         }  
  299.         return null;  
  300.     }  
  301.   
  302.     /** 
  303.      * 获取指定日期所在月的最后一天 
  304.      *  
  305.      * @param date 
  306.      * @return yyyy--MM-dd 
  307.      */  
  308.     public static String getMonthEndDate(String date) {  
  309.         try {  
  310.             Calendar calendar = Calendar.getInstance();  
  311.             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  312.             calendar.setTime(format.parse(date));  
  313.             calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));  
  314.             return format.format(calendar.getTime());  
  315.         } catch (ParseException e) {  
  316.             e.printStackTrace();  
  317.         }  
  318.         return null;  
  319.     }  
  320.   
  321.     /** 
  322.      * 获取指定日期所在周的周一 
  323.      * @param date 
  324.      * @return yyyy-MM-dd 
  325.      */  
  326.     public static String getMondayOfDate(String date) {  
  327.         try {  
  328.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式  
  329.             Calendar cal = Calendar.getInstance();  
  330.             cal.setTime(sdf.parse(date));  
  331.             int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
  332.             if (1 == dayWeek) {  
  333.                 cal.add(Calendar.DAY_OF_MONTH, -1);  
  334.             }  
  335.             cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一  
  336.             int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
  337.             cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);  
  338.             return sdf.format(cal.getTime());  
  339.         } catch (ParseException e) {  
  340.             e.printStackTrace();  
  341.         }  
  342.         return null;  
  343.     }  
  344.   
  345.     /** 
  346.      * 获取指定日期所在周的周日 
  347.      * @param date 
  348.      * @return yyyy-MM-dd 
  349.      */  
  350.     public static String getSundayOfDate(String date) {  
  351.         try {  
  352.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式  
  353.             Calendar cal = Calendar.getInstance();  
  354.             cal.setTime(sdf.parse(date));  
  355.             int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
  356.             if (1 == dayWeek) {  
  357.                 cal.add(Calendar.DAY_OF_MONTH, -1);  
  358.             }  
  359.             cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一  
  360.             int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
  361.             cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则,给当前日期减去星期几与一个星期第一天的差值   
  362.             cal.add(Calendar.DATE, 6);  
  363.             return sdf.format(cal.getTime());  
  364.         } catch (ParseException e) {  
  365.             e.printStackTrace();  
  366.         }  
  367.         return null;  
  368.     }  
[java]  view plain  copy
  1. **  
  2. <span style="white-space:pre">  </span> * 获取月末到当前时间的秒数  
  3. <span style="white-space:pre">  </span> */  
  4. <span style="white-space:pre">  </span>public static long getSecondsBetweenEndMothToNow() {  
  5. <span style="white-space:pre">      </span>Calendar c = Calendar.getInstance();  
  6. <span style="white-space:pre">      </span>c = Calendar.getInstance();    
  7.         c.add(Calendar.MONTH, 1);    
  8.         c.set(Calendar.DAY_OF_MONTH, 0);  
  9.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  10.         String lastday = format.format(c.getTime());  
  11.         lastday = lastday + " 23:59:59";  
  12.         Date last = null;  
  13.         long sec = 0;  
  14. <span style="white-space:pre">      </span>try {  
  15. <span style="white-space:pre">          </span>last = format.parse(lastday);  
  16. <span style="white-space:pre">          </span>long diff = last.getTime() - System.currentTimeMillis();  
  17. <span style="white-space:pre">          </span>sec = diff / 1000;  
  18. <span style="white-space:pre">      </span>} catch (ParseException e) {  
  19. <span style="white-space:pre">          </span>e.printStackTrace();  
  20. <span style="white-space:pre">      </span>}  
  21. <span style="white-space:pre">      </span>return sec;  
  22. <span style="white-space:pre">  </span>}  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值