我正在尝试设置日历对象 - >今天到一个月的第一周的特定日子。
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
today.clear(Calendar.MINUTE);
today.clear(Calendar.SECOND);
today.clear(Calendar.MILLISECOND);
today.getTime();
today.set(Calendar.YEAR,year);
today.set(Calendar.MONTH, month);
today.set(Calendar.WEEK_OF_MONTH,today.getActualMinimum(Calendar.WEEK_OF_MONTH));
today.set(Calendar.DAY_OF_WEEK, getDay()+1);
Log.d(TAG, "Test : "+getName()+", time : "+ today.getTime()+" ,year : "+year+" ,month : "+ month);
问题是,日历正在返回当前日期的当天日期。
答案 0 :(得分:3)
使用以下代码:
public static String getEightWeeksDateString() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-01 00:00:00");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 0);
return dateFormat.format(cal.getTime());
}
以字符串格式返回日期,然后你应该把它转换成你想要的
答案 1 :(得分:0)
Calendar c = Calendar.getInstance(); // this takes current date
c.set(Calendar.DAY_OF_MONTH, 1);
答案 2 :(得分:0)
班级日历中有一个DAY_OF_MONTH字段,如果您想要完成目标,则需要更改该字段。查看documentation。你只需要使用set方法:
today.set(Calendar.DAY_OF_MONTH, 1);
答案 3 :(得分:0)
您可以使用以下代码。它会将当天的日期返回到当天的第一周。
public String getDayFromFirstWeek() {
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault());
Calendar date = Calendar.getInstance(Locale.getDefault());
date.set(Calendar.DAY_OF_MONTH, 1);
date.add(Calendar.DAY_OF_YEAR, (date.get(Calendar.DAY_OF_WEEK)+ Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1) % 7);
return formatter.format(date.getTime());
}
答案 4 :(得分:-2)
以下代码有助于在特定月份内创建重复事件。
Calendar today = Calendar.getInstance();
today.clear();
today.set(Calendar.DAY_OF_MONTH, 1);
today.set(Calendar.YEAR,year);
today.set(Calendar.MONTH, month-1);
today.set(Calendar.DAY_OF_MONTH, 1);
today.add(Calendar.DAY_OF_YEAR,0);
today.getTime();
long id = (getId()*10000)+(year%100)*100;
for(int i = 0; i<today.getActualMaximum(Calendar.WEEK_OF_MONTH);i++){
Calendar startTime = (Calendar) today.clone();
Calendar endTime = (Calendar) today.clone();
startTime.setTimeInMillis(start.getTime());
startTime.set(Calendar.YEAR, year);
startTime.set(Calendar.MONTH,month-1);
startTime.add(Calendar.DAY_OF_YEAR,7*i);
startTime.set(Calendar.DAY_OF_WEEK,getDay()+1);
startTime.getTime();
if(startTime.get(Calendar.MONTH) == month) //break the loop if enters next month.
break;
endTime.setTimeInMillis(end.getTime());
endTime.set(Calendar.YEAR, year);
endTime.set(Calendar.MONTH,month-1);
endTime.add(Calendar.DAY_OF_YEAR,7*i);
endTime.set(Calendar.DAY_OF_WEEK,getDay()+1);
endTime.getTime();
// Set values into event.
WeekViewEvent weekViewEvent = new WeekViewEvent();
weekViewEvent.setName(getName());
weekViewEvent.setId(id+startTime.get(Calendar.WEEK_OF_YEAR));
weekViewEvent.setStartTime(startTime);
weekViewEvent.setEndTime(endTime);
weekViewEvent.setColor(Color.GREEN);
weekViewEvent.setEventBy(getEventBy());
Log.d(TAG, "weekly event recieved : "+getName()+", id : "+weekViewEvent.getId()+", time : "+ startTime.getTime() +" - "+ endTime.getTime());
// Append WeekViewEvent to the list.
list.add(weekViewEvent);
}