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";
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值