1. 程式人生 > >Android 昨日 本週內 本月內 本季度 本年度所取得範圍

Android 昨日 本週內 本月內 本季度 本年度所取得範圍

由於專案中需要一個日期範圍,因此寫下了昨日 本週內 本月內 本季度 本年度所取得範圍,
所有的一切是基於今天的日期。
	//獲取當天的時間
    public static String getCurrentDate() {
        Calendar now = Calendar.getInstance();
        int calendarMonth = now.get(Calendar.MONTH) + 1;
        String month = calendarMonth + "";
        if (calendarMonth < 10) {
            month = "0" + calendarMonth;
        }
        return now.get(Calendar.YEAR) + "-" + month + "-" + now.get(Calendar.DAY_OF_MONTH);
    }

  /**
     * 昨日
     */
    public static String getYesterday() {
        Calendar now = Calendar.getInstance();
        int calendarMonth = now.get(Calendar.MONTH) + 1;
        String month = calendarMonth + "";
        if (calendarMonth < 10) {
            month = "0" + calendarMonth;
        }
        return now.get(Calendar.YEAR) + "-" + month + "-" + (now.get(Calendar.DAY_OF_MONTH) - 1);
    }

    /**
     * 本週開始的第一天
     */
  public static String getThisWeekStart() {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return df.format(c.getTime());
    }

    /**
     * 本週開始的最後一天
     */
  public static String getThisWeekEnd() {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        c.add(Calendar.DAY_OF_WEEK, 6);
        return df.format(c.getTime());
    }

    /**
     * 本月開始的第一天
     */
   public static String getThisMonthStart() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//設定為1號,當前日期既為本月第一天
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault());
        return df.format(c.getTime());
    }
    /**
     * 本月的最後一天
     */
   public static String getThisMonthEnd() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault());
        return df.format(ca.getTime());
    }

    /**
     * 本季度的第一天
     */
    public static String getThisQuarterStart() {
        Calendar now = Calendar.getInstance();
        int calendarMonth = now.get(Calendar.MONTH) + 1;
        return getThisSeasonFirstTime(calendarMonth);
    }

    private static String getThisSeasonFirstTime(int month) {
        int array[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
        int season = 1;
        if (month >= 1 && month <= 3) {
            season = 1;
        }
        if (month >= 4 && month <= 6) {
            season = 2;
        }
        if (month >= 7 && month <= 9) {
            season = 3;
        }
        if (month >= 10 && month <= 12) {
            season = 4;
        }
        int start_month = array[season - 1][0];
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
        String years = dateFormat.format(date);
        int years_value = Integer.parseInt(years);

        String seasonDate = years_value + "-" + start_month + "-" + "01";
        return seasonDate;

    }

    /**
     * 本季度的最後一天
     */
    public static String getThisQuarterEnd() {
        Calendar now = Calendar.getInstance();
        int calendarMonth = now.get(Calendar.MONTH) + 1;
        return getThisSeasonFinallyTime(calendarMonth);
    }

    private static String getThisSeasonFinallyTime(int month) {
        int array[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
        int season = 1;
        if (month >= 1 && month <= 3) {
            season = 1;
        }
        if (month >= 4 && month <= 6) {
            season = 2;
        }
        if (month >= 7 && month <= 9) {
            season = 3;
        }
        if (month >= 10 && month <= 12) {
            season = 4;
        }
        int end_month = array[season - 1][2];

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
        String years = dateFormat.format(date);
        int years_value = Integer.parseInt(years);
        int end_days = getLastDayOfMonth(years_value, end_month);
        String seasonDate = years_value + "-" + end_month + "-" + end_days;
        return seasonDate;
    }

    private static int getLastDayOfMonth(int year, int month) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
                || month == 10 || month == 12) {
            return 31;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        }
        if (month == 2) {
            if (isLeapYear(year)) {
                return 29;
            } else {
                return 28;
            }
        }
        return 0;
    }

    private static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    /**
     * 本年度第一天
     */
    public static String getThisYearStart() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.YEAR) + "01" + "01";
    }

    /**
     * 本年度最後
     */
    public static String getThisYearEnd() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.YEAR) + "12" + "31";
    }