如何在Android中获取“当前周”详细信息以及“季度详细信息”?

用户名

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

安卓

这是我放在一起的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 idprivate 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语句来控制输出。

祝你好运

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在EmberJS中存储当前用户详细信息?

来自分类Dev

如何从班级中获取详细信息

来自分类Dev

如何在Swift中获取硬件详细信息?

来自分类Dev

如何在Jquery中获取选定的行详细信息

来自分类Dev

如何在TFS中获取构建详细信息

来自分类Dev

Liferay:如何在Servlet中获取当前的登录用户详细信息?

来自分类Dev

如何从Google API获取当前用户的详细信息

来自分类Dev

如何在Android中的AlertDialog中添加“单击以获取更多详细信息”可扩展字段

来自分类Dev

如何在Android中获取来电的网络(运营商)详细信息

来自分类Dev

如何在MPL Android中获取PayPal交易详细信息

来自分类Dev

如何在Android中获取来电的网络(运营商)详细信息

来自分类Dev

如何获取数组结果的详细信息

来自分类Dev

如何从PyYAML异常获取详细信息?

来自分类Dev

如何获取Windows文件详细信息?

来自分类Dev

如何获取Facebook Post详细信息?

来自分类Dev

如何获取Java文件的“详细信息”

来自分类Dev

获取JSP中的用户详细信息?

来自分类Dev

未在jsp中获取详细信息

来自分类Dev

如何在Windows上获取文件的详细信息

来自分类Dev

如何在Windows上获取文件的详细信息

来自分类Dev

获取意图详细信息

来自分类Dev

如何从android.database.SQLException获取错误详细信息?

来自分类Dev

从android中的联系人获取地址详细信息

来自分类Dev

以编程方式在Android中获取Facebook朋友的详细信息

来自分类Dev

如何从渲染的文档中获取蜂房模型详细信息?

来自分类Dev

如何通过Rails中的关联获取详细信息?

来自分类Dev

如何获取亚马逊MWS中的产品详细信息?

来自分类Dev

如何从Datomic的“事务”中获取详细信息/错误消息

来自分类Dev

如何获取轮班活动FullCalendar中约会的详细信息

Related 相关文章

  1. 1

    如何在EmberJS中存储当前用户详细信息?

  2. 2

    如何从班级中获取详细信息

  3. 3

    如何在Swift中获取硬件详细信息?

  4. 4

    如何在Jquery中获取选定的行详细信息

  5. 5

    如何在TFS中获取构建详细信息

  6. 6

    Liferay:如何在Servlet中获取当前的登录用户详细信息?

  7. 7

    如何从Google API获取当前用户的详细信息

  8. 8

    如何在Android中的AlertDialog中添加“单击以获取更多详细信息”可扩展字段

  9. 9

    如何在Android中获取来电的网络(运营商)详细信息

  10. 10

    如何在MPL Android中获取PayPal交易详细信息

  11. 11

    如何在Android中获取来电的网络(运营商)详细信息

  12. 12

    如何获取数组结果的详细信息

  13. 13

    如何从PyYAML异常获取详细信息?

  14. 14

    如何获取Windows文件详细信息?

  15. 15

    如何获取Facebook Post详细信息?

  16. 16

    如何获取Java文件的“详细信息”

  17. 17

    获取JSP中的用户详细信息?

  18. 18

    未在jsp中获取详细信息

  19. 19

    如何在Windows上获取文件的详细信息

  20. 20

    如何在Windows上获取文件的详细信息

  21. 21

    获取意图详细信息

  22. 22

    如何从android.database.SQLException获取错误详细信息?

  23. 23

    从android中的联系人获取地址详细信息

  24. 24

    以编程方式在Android中获取Facebook朋友的详细信息

  25. 25

    如何从渲染的文档中获取蜂房模型详细信息?

  26. 26

    如何通过Rails中的关联获取详细信息?

  27. 27

    如何获取亚马逊MWS中的产品详细信息?

  28. 28

    如何从Datomic的“事务”中获取详细信息/错误消息

  29. 29

    如何获取轮班活动FullCalendar中约会的详细信息

热门标签

归档