當(dāng)天
private void getNowDate() {
Calendar calendar = Calendar.getInstance();
int cYear = calendar.get(Calendar.YEAR);
int cMonth = calendar.get(Calendar.MONTH) + 1;
int cDay = calendar.get(Calendar.DAY_OF_MONTH);
nowYear = String.format("%02d", cYear);
nowMonth = String.format("%02d", cMonth);
nowDay = String.format("%02d", cDay);
tvStartTime.setText(nowYear + "-" + nowMonth + "-" + nowDay);
tvEndTime.setText(nowYear + "-" + nowMonth + "-" + nowDay);
startYear = nowYear;
startMonth = nowMonth;
startDay = nowDay;
}
本周
public void getWeekDate() {
Calendar calendar = new GregorianCalendar();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(new Date());
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
Log.i(TAG, String.format("本周:年%d月%d日%d", year, month, day));
tvStartTime.setText(String.format("%02d", year) + "-" + String.format("%02d", month) + "-" + String.format("%02d", day));
tvEndTime.setText(nowYear + "-" + nowMonth + "-" + nowDay);
startYear = String.format("%02d", year);
startMonth = String.format("%02d", month);
startDay = String.format("%02d", day);
endYear = nowYear;
endMonth = nowMonth;
endDay = nowDay;
}
本月
public void getMonthDate() {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
Log.i(TAG, String.format("本月:年%d月%d日%d", year, month, day));
tvStartTime.setText(String.format("%02d", year) + "-" + String.format("%02d", month) + "-" + String.format("%02d", day));
tvEndTime.setText(nowYear + "-" + nowMonth + "-" + nowDay);
startYear = String.format("%02d", year);
startMonth = String.format("%02d", month);
startDay = String.format("%02d", day);
endYear = nowYear;
endMonth = nowMonth;
endDay = nowDay;
}