我有当前日期,我想要计算当前星期以及当前月份在Android中的季度。 我怎样才能做到这一点? 我正在使用日历来获取当前日期。 请帮帮我。 提前谢谢。
答案 0 :(得分:0)
这是我放在一起的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语句来控制输出。
祝你好运