1. 程式人生 > 其它 >Java Calendar類實現日期的操作

Java Calendar類實現日期的操作

根據年月獲取月份第一天

public static String getSDateOfMonth(String monthId) {
    String SDate;
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int y = Integer.parseInt(monthId.substring(0, 4));
    int m = Integer.parseInt(monthId.substring(4, 6));
    if (y == year && m == month) {
      if (month < 10) {// 月份少於10
        SDate = "" + year + "-0" + month + "-01";
      } else {
        SDate = "" + year + month + "-01";
      }
    } else {// 不是當前月
      if (m < 10) {// 月份少於10
        SDate = "" + y + "-0" + m + "-01";
      } else {
        SDate = "" + y + m + "-01";
      }
    }
    return SDate;
  };

根據年月獲取月份最後一天

public static String getLastDayOfMonth(String monthId) {
    int year = Integer.parseInt(monthId.substring(0, 4));
    int month = Integer.parseInt(monthId.substring(4, 6));
    Calendar cal = Calendar.getInstance();
    // 設定年份
    cal.set(Calendar.YEAR, year);
    // 設定月份
    cal.set(Calendar.MONTH, month - 1);
    // 獲取某月最大天數
    int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    // 設定日曆中月份的最大天數
    cal.set(Calendar.DAY_OF_MONTH, lastDay);
    // 格式化日期
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String lastDayOfMonth = sdf.format(cal.getTime());

    return lastDayOfMonth;
  };

判斷是否是當前年月

public static boolean isCurrentMonth(String month) {
    Calendar cal = Calendar.getInstance();
    int currentYear = cal.get(Calendar.YEAR);
    int currentMonth = cal.get(Calendar.MONTH) + 1;
    int y = Integer.parseInt(month.substring(0, 4));
    int m = Integer.parseInt(month.substring(4, 6));
    if (y == currentYear && m == currentMonth) {
      return true;
    }else{
      return false;
    }
  };
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
         
        //獲取前月的第一天 
        Calendar   cal_1=Calendar.getInstance();//獲取當前日期  
        cal_1.add(Calendar.MONTH, -1); 
        cal_1.set(Calendar.DAY_OF_MONTH,1);//設定為1號,當前日期既為本月第一天  
        firstDay = format.format(cal_1.getTime()); 
        System.out.println("-----1------firstDay:"+firstDay);



        //獲取前月的最後一天 
        Calendar cale = Calendar.getInstance();    
        cale.set(Calendar.DAY_OF_MONTH,0);//設定為1號,當前日期既為本月第一天  
        lastDay = format.format(cale.getTime()); 
        System.out.println("-----2------lastDay:"+lastDay); 
         
         
        //獲取當前月第一天: 
        Calendar c = Calendar.getInstance();     
        c.add(Calendar.MONTH, 0); 
        c.set(Calendar.DAY_OF_MONTH,1);//設定為1號,當前日期既為本月第一天  
        String first = format.format(c.getTime()); 
        System.out.println("===============first:"+first); 
         
        //獲取當前月最後一天 
        Calendar ca = Calendar.getInstance();     
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));   
        String last = format.format(ca.getTime()); 
        System.out.println("===============last:"+last);