【问题标题】:How to get Current Week details as well as Quarter Details in android?如何在 android 中获取当前周详细信息以及季度详细信息?
【发布时间】:2014-04-04 12:32:15
【问题描述】:

我有当前日期,我想从中计算当前周以及 android 中当前月份的季度。 我怎样才能做到这一点? 我正在使用日历来获取当前日期。 请帮助我。 提前致谢。

【问题讨论】:

  • 到目前为止你有什么?
  • 查看这个帖子的答案,这是我的帖子,检查一下,希望你能从中得到他的想法,如果可行,请告诉我..stackoverflow.com/questions/21473696/…

标签: android date calendar


【解决方案1】:

这是我放在一起的 DateSelection 类的示例。我创建了一个 Activity,然后我将它作为我的 Activity 中的子类。

private class DateSelection{

    //---------------------------------------------------
    // Initial Date
    private Calendar currentDate    = null;

    // Two GregorianCalendar Objects to Return 
    private GregorianCalendar beginDate         = null;
    private GregorianCalendar endDate           = null;

    private int buttonId = -1;
    private int month = -1;
    private int year = -1;

    //---------------------------------------------------
    // Constructor
    public DateSelection(int buttonId){

        // Initialize the buttonId to the passed in value
        this.buttonId = buttonId;

        // Initialize the new GregorianCalendar Objects
        currentDate = new GregorianCalendar();

        // Set the Current Month
        month   = currentDate.get(Calendar.MONTH);

        // Set the Current Year
        year    = currentDate.get(Calendar.YEAR);

        beginDate   = new GregorianCalendar();
        endDate     = new GregorianCalendar();

        // Set the Current Date Selection 
        setCurrentDateSelection();
    }

    // Constructor : Creates a new Date Selection Object with Two Dates
    public DateSelection(GregorianCalendar beginDate, GregorianCalendar endDate){

        // Set the Being and End Dates for this object
        this.beginDate  = beginDate;
        this.endDate    = endDate;
    }

    // Sets the Current Date based on the Button Used
    public void setCurrentDateSelection(){
        // Switch on the buttons id
        switch(buttonId){
            // Current Month
            case 0: 
                getCurrentMonth();
                break;

            // Current Quarter
            case 1:
                getCurrentQuarter();
                break;

            // Current Year
            case 2: 
                getCurrentYear();
                break;
            case 3: 
                getPriorMonth();
                break;

            case 4: 
                getPriorQuarter();
                break;

            case 5: 
                getPriorYear();
                break;
            case 6:
                getMonthToDate();
                break;
            case 7:
                getQuarterToDate();
                break;
            case 8:
                getYearToDate();
                break;

            // Default Case
            default:
                break;
        }
    }

    // Returns the Current Month Range as a Date Selection Object
    public DateSelection getCurrentMonth(){

        // Determine the First and Last Day of the Current Month
        int firstOfMonth    = currentDate.getActualMinimum(Calendar.DAY_OF_MONTH);
        int lastOfMonth     = currentDate.getActualMaximum(Calendar.DAY_OF_MONTH);

        // Set the Year, Month and Date for the beginDate
        beginDate.set(Calendar.YEAR, year);
        beginDate.set(Calendar.MONTH, month);
        beginDate.set(Calendar.DAY_OF_MONTH, firstOfMonth);

        // Set the Year, Month and Date for the endDate
        endDate.set(Calendar.YEAR, year);
        endDate.set(Calendar.MONTH, month);
        endDate.set(Calendar.DAY_OF_MONTH, lastOfMonth);

        // Return the new DateSelection Object
        return new DateSelection(beginDate, endDate);

    }

    // Returns the Current Quarter Range as a DateSelection Object
    public DateSelection getCurrentQuarter(){

        // Determine the Quarter Begin and Quarter End Months
        int quarterBeginMonth = (((month - 1) / 3) * 3);
        int quarterEndMonth = quarterBeginMonth + 2;

        // Set the Month and Year for the Current Quarter
        beginDate.set(Calendar.MONTH, quarterBeginMonth);
        beginDate.set(Calendar.YEAR, year);

        // Determine First Day of the Quarter Begin Month
        int firstDayOfBeginMonth = beginDate.getActualMinimum(Calendar.DAY_OF_MONTH);
        // Set the Day for the Begin Date of the Current Quarter
        beginDate.set(Calendar.DAY_OF_MONTH, firstDayOfBeginMonth);

        // Set the Current Quarter Ending Month and Year
        endDate.set(Calendar.MONTH, quarterEndMonth);
        endDate.set(Calendar.YEAR, year);

        // Determine the Last Day of the Quarter End Month
        int lastDayOfEndMonth = endDate.getActualMaximum(Calendar.DAY_OF_MONTH);
        // Set the endDate Date of Month
        endDate.set(Calendar.DAY_OF_MONTH, lastDayOfEndMonth);

        // Return the new DateSelection Object
        return new DateSelection(beginDate, endDate);

    }

    // Returns the Current Year Range as a DateSelection Object
    public DateSelection getCurrentYear(){

        // Determine the int values of the first and last months of the year
        int firstMonth = currentDate.getActualMinimum(Calendar.MONTH);
        int lastMonth = currentDate.getActualMaximum(Calendar.MONTH);

        // Set the Month for beginDate
        beginDate.set(Calendar.MONTH, firstMonth);

        // Determine the int value for the first Day of the Year
        int firstDayOfYear = beginDate.getActualMinimum(Calendar.DAY_OF_MONTH);
        // Set the Day for Begin Date to the first Day of the year
        beginDate.set(Calendar.DAY_OF_MONTH, firstDayOfYear);
        // Set the Month for the endDate to the Last month of the year
        endDate.set(Calendar.MONTH, lastMonth);

        // Determine the last day of the Year from the given month
        int lastDayOfYear = endDate.getActualMaximum(Calendar.DAY_OF_MONTH);

        // Set the Day of the endDate to the Last day of the year
        endDate.set(Calendar.DAY_OF_MONTH, lastDayOfYear);

        // Return the new DateSelection Object
        return new DateSelection(beginDate, endDate);
    }

    // Returns the Prior Month as a DateSelection Object
    public DateSelection getPriorMonth(){

        // Decrement the Month by 1 to get the prior month
        month -= 1;

        // Allow for underflow to the previous year
        if(month < 0)
        {   
            month = 11;
            year--;
        }

        // Determine the int value for the first month of the year
        int firstOfMonth    = currentDate.getActualMinimum(Calendar.DAY_OF_MONTH);

        // Set the Year, Month, and Day
        beginDate.set(Calendar.YEAR, year);
        beginDate.set(Calendar.MONTH, month);
        beginDate.set(Calendar.DAY_OF_MONTH, firstOfMonth);

        // Determine the int value for the last month of the year
        int lastOfMonth     = currentDate.getActualMaximum(Calendar.DAY_OF_MONTH);

        // Set the Year, Month, and Day
        endDate.set(Calendar.YEAR, year);
        endDate.set(Calendar.MONTH, month);
        endDate.set(Calendar.DAY_OF_MONTH, lastOfMonth);

        // Return the new DateSelection Object
        return new DateSelection(beginDate, endDate);
    }

    // Returns the Prior Quarter as a DateSelection Object
    public DateSelection getPriorQuarter(){

        // Determine the int values for the Begin and End Quarter Months
        int quarterBeginMonth = (((month - 1) / 3) * 3);
        int quarterEndMonth = quarterBeginMonth + 2;

        // Subtract 3 from the 
        quarterBeginMonth   -= 3;
        quarterEndMonth     -= 3;

        // Allow for underflow for the previous year
        if(quarterBeginMonth < 0)
        {   
            quarterBeginMonth += 12;
            quarterEndMonth += 12;

            year --;    
        }

        // Set the Begin Date's Month and Year
        beginDate.set(Calendar.MONTH, quarterBeginMonth);
        beginDate.set(Calendar.YEAR, year);

        // Get First Day of Quarter's Begin Month
        int firstDayOfBeginMonth = beginDate.getActualMinimum(Calendar.DAY_OF_MONTH);
        beginDate.set(Calendar.DAY_OF_MONTH, firstDayOfBeginMonth);

        // Set the End Date's Month and Year
        endDate.set(Calendar.MONTH, quarterEndMonth);
        endDate.set(Calendar.YEAR, year);

        // Determine the Last Day of the Quarter's End Month
        int lastDayOfEndMonth = endDate.getActualMaximum(Calendar.DAY_OF_MONTH);
        // Set the End Day for the End Month
        endDate.set(Calendar.DAY_OF_MONTH, lastDayOfEndMonth);

        // Return the new DateSelection Object
        return new DateSelection(beginDate, endDate);
    }

    // Returns the Prior Year Range as a DateSelection Object
    public DateSelection getPriorYear(){

        // Decrement the Year by 1 to get the Prior Year
        year -= 1;

        // Determine the int values for the Current years first and last months
        int firstMonth = currentDate.getActualMinimum(Calendar.MONTH);
        int lastMonth = currentDate.getActualMaximum(Calendar.MONTH);

        // Set the Begin date's Year and Month
        beginDate.set(Calendar.YEAR, year);
        beginDate.set(Calendar.MONTH, firstMonth);

        // Determine the first day of the first month of the previous year
        int firstDayOfYear = beginDate.getActualMinimum(Calendar.DAY_OF_MONTH);
        // Set the beginDate to the first day of the previous year
        beginDate.set(Calendar.DAY_OF_MONTH, firstDayOfYear);

        // Set the End date's Year and Month
        endDate.set(Calendar.YEAR, year);
        endDate.set(Calendar.MONTH, lastMonth);

        // Determine the last day of the last month of the previous year
        int lastDayOfYear = endDate.getActualMaximum(Calendar.DAY_OF_MONTH);
        // Set the endDate to the last day of the previous year
        endDate.set(Calendar.DAY_OF_MONTH, lastDayOfYear);

        // Return the new DateSelection Object
        return new DateSelection(beginDate, endDate);
    }       

    // Returns the Month to Date as a DateSelection Object
    public DateSelection getMonthToDate(){

        // Determine the int value for the first day of the Month
        int firstOfMonth = currentDate.getActualMinimum(Calendar.DAY_OF_MONTH);

        // Set the Year, Month, and Day for the Begin Date
        beginDate.set(Calendar.YEAR, year);
        beginDate.set(Calendar.MONTH, month);
        beginDate.set(Calendar.DAY_OF_MONTH, firstOfMonth);

        // Set the endDate to the Current Date
        endDate = (GregorianCalendar) currentDate;

        // Return the new DateSelection Object
        return new DateSelection(beginDate, endDate);
    }

    // Returns the Quarter to Date as a DateSelection Object
    public DateSelection getQuarterToDate(){

        // Determine the int value for the current Quarter's beginning month
        int quarterBeginMonth = (((month - 1) / 3) * 3);

        // Set the Month and Year for the beginMonth
        beginDate.set(Calendar.MONTH, quarterBeginMonth);
        beginDate.set(Calendar.YEAR, year);

        // Determine the First Day of Quarter's Begin Month
        int firstDayOfBeginMonth = beginDate.getActualMinimum(Calendar.DAY_OF_MONTH);
        // Set the day of the beginDate to the first day of the Quarter's Month
        beginDate.set(Calendar.DAY_OF_MONTH, firstDayOfBeginMonth);

        // Set the end Date to the Current Date
        endDate = (GregorianCalendar) currentDate;

        // Return the new DateSelection Object
        return new DateSelection(beginDate, endDate);
    }

    // Returns the Year to Date as a DateSelection Object
    public DateSelection getYearToDate(){

        // Determine the first Month of the Year
        int firstMonth = currentDate.getActualMinimum(Calendar.MONTH);

        beginDate.set(Calendar.MONTH, firstMonth);

        int firstDayOfYear = beginDate.getActualMinimum(Calendar.DAY_OF_MONTH);
        beginDate.set(Calendar.DAY_OF_MONTH, firstDayOfYear);

        // Set the end Date to the Current Date
        endDate = (GregorianCalendar) currentDate;

        // Return the new DateSelection Object
        return new DateSelection(beginDate, endDate);
    }

}

然后为了获取日期,我创建了一个 DateSelection Object 的类变量,然后使用按钮和 id 来控制返回的日期。

例如,今年我创建了一个按钮,添加了一个点击监听器,如下所示:

类变量:DateSelection m_dateSelection = null; 还有一个 int id private static final int BUTTON_CURRENT_MONTH = 0;

OnClickListener CurrentMonthListener = new OnClickListener(){

    @Override
    public void onClick(View view) {
        m_dateSelection = new DateSelection(BUTTON_CURRENT_MONTH);
        Toast.makeText(getBaseContext(), "Current Month is from "+ calendarToNumberString( m_dateSelection.beginDate) + " to "+ calendarToNumberString(m_dateSelection.endDate), Toast.LENGTH_LONG).show();

    }

};

然后在我的子类中你可以看到我如何使用 switch 语句来控制输出。

祝你好运

【讨论】:

  • 你可以从 get CurrentMonth 复制代码,使用 GregorianCalendar 查找当前日期,然后加 7 或查找当前日期,获取一周的第一天、一周的最后一天等。
猜你喜欢
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 2014-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 2021-01-20
相关资源
最近更新 更多