Calendar

Calendar类是一个抽象类,不可以使用new关键字直接创建实例。
Calendar可以为在某一特定时刻和一组之间的转换的方法 calendar fields(日历域) 如YEAR , MONTH , DAY_OF_MONTH , HOUR 等等,以及用于操纵该日历字段,如获取的日期下个星期。

  1. import java.util.Calendar;
  2. /**
  3. * 使用默认时区和区域设置获取日历
  4. */
  5. public class TestCalendar {
  6. public static void main(String[] args) {
  7. Calendar ca = Calendar.getInstance();
  8. // get:返回给定日历字段的值。
  9. // YEAR:年份
  10. int year = ca.get(Calendar.YEAR);
  11. System.out.println(year);
  12. // MONTH:月份
  13. System.out.println(ca.get(Calendar.MONTH)+1);
  14. // DATE:本月的日期
  15. System.out.println(ca.get(Calendar.DATE));
  16. // DAY_OF_MONTH:一周中的日期
  17. System.out.println(ca.get(Calendar.DAY_OF_MONTH));
  18. // 一个月的第几周
  19. System.out.println(ca.get(Calendar.DAY_OF_WEEK_IN_MONTH));
  20. // 一个月的第几天
  21. System.out.println(ca.get(Calendar.DAY_OF_MONTH));
  22. // HOUR_OF_DAY:当天的小时数
  23. System.out.println(ca.get(Calendar.HOUR_OF_DAY));
  24. // MINUTE:分
  25. System.out.println(ca.get(Calendar.MINUTE));
  26. // SECOND:秒
  27. System.out.println(ca.get(Calendar.SECOND));
  28. // 注意:静态成员和静态方法不要使用名称来调用,必须使用名称来调用。
  29. System.out.println(ca.get(ca.SECOND));
  30. // set:设置日历字段的值
  31. // set(int field, nt value);
  32. // field -> 给定的日历字段
  33. // value -> 要为给定日历字段设置的值
  34. ca.set(Calendar.YEAR, 2020);
  35. Date time = ca.getTime();
  36. System.out.println(time);
  37. // set(int year, int month, int date, int hourOfDay, int minute, int second);
  38. // year -> 用于设置 YEAR日历字段的值。
  39. // month -> 用于设置MONTH日历字段的值。 月值为0。 例如,1月份为0。
  40. // date -> 用于设置 DAY_OF_MONTH日历字段的值。
  41. // hourOfDay -> 用于设置 HOUR_OF_DAY日历字段的值。
  42. // minute -> 用于设置 MINUTE日历字段的值。
  43. // second -> 用于设置 SECOND日历字段的值。
  44. }
  45. }

1.获取当月时间范围

  1. public static String getMonth(){
  2. SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
  3. Calendar c = Calendar.getInstance();
  4. c.add(Calendar.MONTH, 0);
  5. c.set(Calendar.DAY_OF_MONTH,1);
  6. String first = format.format(c.getTime());
  7. Calendar ca = Calendar.getInstance();
  8. ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
  9. String last = format.format(ca.getTime());
  10. return first+" and "+last;
  11. }

输出:

  1. public static void main(String[] args) {
  2. System.out.println(getMonth()); // 2019-01-01 and 2019-01-31
  3. }

2.获取去年当月时间范围

  1. public static String getMonthLastYear(){
  2. SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
  3. Calendar lastYearStart = Calendar.getInstance();
  4. lastYearStart.add(Calendar.YEAR, -1);
  5. lastYearStart.add(Calendar.MONTH, 0);
  6. lastYearStart.set(Calendar.DAY_OF_MONTH,1);
  7. String firstDayLastYear = format.format(lastYearStart.getTime());
  8. Calendar lastYearEnd = Calendar.getInstance();
  9. lastYearEnd.add(Calendar.YEAR, -1);
  10. lastYearEnd.add(Calendar.MONTH, 0);
  11. lastYearEnd.set(Calendar.DAY_OF_MONTH, lastYearEnd.getActualMaximum(Calendar.DAY_OF_MONTH));
  12. String endDayLastYear = format.format(lastYearEnd.getTime());
  13. return firstDayLastYear+" and "+endDayLastYear;
  14. }

输出:

  1. public static void main(String[] args) {
  2. System.out.println(getMonthLastYear());// 20180101 and 20180131
  3. }

3. 获取某年某月时间范围

  1. public static String getMonthAround(String date){
  2. SimpleDateFormat format=new SimpleDateFormat("yyyy-MM");
  3. SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM-dd");
  4. Date monthDate = null;
  5. try {
  6. monthDate = format.parse(date);
  7. } catch (ParseException e) {
  8. e.printStackTrace();
  9. }
  10. Calendar lastYearStart = Calendar.getInstance();
  11. lastYearStart.setTime(monthDate);
  12. lastYearStart.set(Calendar.DAY_OF_MONTH,1);
  13. String firstDayLastYear = format2.format(lastYearStart.getTime());
  14. Calendar lastYearEnd = Calendar.getInstance();
  15. lastYearEnd.setTime(monthDate);
  16. lastYearEnd.set(Calendar.DAY_OF_MONTH, lastYearEnd.getActualMaximum(Calendar.DAY_OF_MONTH));
  17. String endDayLastYear = format2.format(lastYearEnd.getTime());
  18. return firstDayLastYear+" and "+endDayLastYear;
  19. }

输出:

  1. public static void main(String[] args) {
  2. System.out.println(getMonthAround("2019-06"));// 2019-06-01 and 2019-06-30
  3. }

4.获取某日去年的时间,如果是闰年二月29号,前一年自动变成28号

  1. public static String getDateDayLastYear(String dateStr){
  2. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  3. Date date = null;
  4. try {
  5. date = format.parse(dateStr);
  6. } catch (ParseException e) {
  7. e.printStackTrace();
  8. }
  9. Calendar cal = Calendar.getInstance();
  10. cal.setTime(date);
  11. cal.add(Calendar.YEAR, -1);
  12. cal.add(Calendar.MONTH, 0);
  13. String lastDay = format.format(cal.getTime());
  14. return lastDay;
  15. }

输出:

  1. public static void main(String[] args) {
  2. System.out.println(getDateDayLastYear("2019-2-28"));// 2018-02-28
  3. }

5.计算两个日期之间所包含的天数

  1. public static int differentDays(String startDate,String endDate){
  2. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  3. Date date2 = null;
  4. Date date1 = null;
  5. try {
  6. date1 = format.parse(startDate);
  7. date2 = format.parse(endDate);
  8. } catch (ParseException e) {
  9. e.printStackTrace();
  10. }
  11. Calendar cal1 = Calendar.getInstance();
  12. cal1.setTime(date1);
  13. Calendar cal2 = Calendar.getInstance();
  14. cal2.setTime(date2);
  15. int day1= cal1.get(Calendar.DAY_OF_YEAR);
  16. int day2 = cal2.get(Calendar.DAY_OF_YEAR);
  17. int year1 = cal1.get(Calendar.YEAR);
  18. int year2 = cal2.get(Calendar.YEAR);
  19. if(year1 != year2) {
  20. int timeDistance = 0 ;
  21. for(int i = year1 ; i < year2 ; i ++) {
  22. //闰年
  23. if(i%4==0 && i%100!=0 || i%400==0){
  24. timeDistance += 366;
  25. } else{ //不是闰年
  26. timeDistance += 365;
  27. }
  28. }
  29. return timeDistance + (day2-day1)+1 ;
  30. } else{ //同年
  31. return day2-day1+1;
  32. }
  33. }

输出:

  1. public static void main(String[] args) {
  2. System.out.println(differentDays("2019-2-26","2019-2-28"));// 3
  3. }

6.获取上个月的第一天和最后一天

  1. public static String getLastMonth(){
  2. SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
  3. Calendar calendar=Calendar.getInstance();
  4. calendar.add(Calendar.MONTH, -1);
  5. calendar.set(Calendar.DAY_OF_MONTH, 1);
  6. Calendar calendar1=Calendar.getInstance();
  7. int month=calendar1.get(Calendar.MONTH);
  8. calendar1.set(Calendar.MONTH, month-1);
  9. calendar1.set(Calendar.DAY_OF_MONTH,
  10. calendar1.getActualMaximum(Calendar.DAY_OF_MONTH));
  11. String firstDay = format.format(calendar.getTime());
  12. String lastDay = format.format(calendar1.getTime());
  13. return firstDay+" and "+lastDay;
  14. }

输出:

  1. public static void main(String[] args) {
  2. System.out.println(getLastMonth());// 2018-12-01 and 2018-12-31
  3. }

7.获取某个时间的当周时间范围

  1. public static String getWeek(String date){
  2. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  3. Date time = null;
  4. try {
  5. time = sdf.parse(date);
  6. } catch (ParseException e) {
  7. e.printStackTrace();
  8. }
  9. Calendar cal = Calendar.getInstance();
  10. cal.setTime(time);
  11. int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
  12. if (1 == dayWeek) {
  13. cal.add(Calendar.DAY_OF_MONTH, -1);
  14. }
  15. cal.setFirstDayOfWeek(Calendar.MONDAY);
  16. int day = cal.get(Calendar.DAY_OF_WEEK);
  17. cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
  18. String imptimeBegin = sdf.format(cal.getTime());
  19. cal.add(Calendar.DATE, 6);
  20. String imptimeEnd = sdf.format(cal.getTime());
  21. return imptimeBegin + " and " + imptimeEnd;
  22. }

输出:

  1. public static void main(String[] args) {
  2. System.out.println(getWeek("2019-01-23"));// 2019-01-21 and 2019-01-27
  3. }

8.指定日期加上天数后的日期

  1. // @param num 为增加的天数
  2. // @param newDate 创建时间
  3. public static String plusDay(int num, String newDate) {
  4. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  5. Date dt = null;
  6. try {
  7. dt = sdf.parse(newDate);
  8. } catch (ParseException e) {
  9. e.printStackTrace();
  10. }
  11. Calendar rightNow = Calendar.getInstance();
  12. rightNow.setTime(dt);
  13. rightNow.add(Calendar.DAY_OF_YEAR, num);
  14. Date dt1 = rightNow.getTime();
  15. String reStr = sdf.format(dt1);
  16. return reStr;
  17. }

输出:

  1. public static void main(String[] args) {
  2. System.out.println(plusDay(2,"2019-01-23"));//2019-01-25
  3. }

9.指定日期加上月数后的日期

  1. public static String plusMonth(int num, String newDate) {
  2. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  3. Date dt = null;
  4. try {
  5. dt = sdf.parse(newDate);
  6. } catch (ParseException e) {
  7. e.printStackTrace();
  8. }
  9. Calendar rightNow = Calendar.getInstance();
  10. rightNow.setTime(dt);
  11. rightNow.add(Calendar.MONTH,num);//日期加3个月
  12. Date dt1 = rightNow.getTime();
  13. String reStr = sdf.format(dt1);
  14. return reStr;
  15. }

输出:

  1. public static void main(String[] args) {
  2. System.out.println(plusMonth(12,"2004-02-29"));//2005-02-28
  3. }

10.获取上个月的今天

  1. public static String getFirstDate() {
  2. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  3. Calendar cl = Calendar.getInstance();
  4. cl.add(Calendar.MONTH, -1);
  5. Date dateFrom = cl.getTime();
  6. return sdf.format(dateFrom);
  7. }

输出:

  1. public static void main(String[] args) {
  2. System.out.println(getFirstDate());//2018-12-23
  3. }

11.获取某天的下一天

  1. public static String getNextDate(String newDate) {
  2. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  3. Date dt = null;
  4. try {
  5. dt = sdf.parse(newDate);
  6. } catch (ParseException e) {
  7. e.printStackTrace();
  8. }
  9. Calendar rightNow = Calendar.getInstance();
  10. rightNow.setTime(dt);
  11. rightNow.add(Calendar.DATE,1);//日期加1天
  12. Date dt1 = rightNow.getTime();
  13. String reStr = sdf.format(dt1);
  14. return reStr;
  15. }

输出:

  1. public static void main(String[] args) {
  2. System.out.println(getNextDate("2019-02-20"));//2019-02-21
  3. }