简体   繁体   中英

Android / Java - Figuring out what the date will be

I'm creating an application that allows the person to select a day within the current week (Days 1/Sunday through 7/Saturday) and see whats on the agenda for that day.

The functionality I'm looking for is to check what today's date is and what day of the week it is. With this information It will show the current weekdays 1 through 7 and I want them to display the date of that day

Example: Today is Thursday 6/23/2011 (Day 5) I can find this information with the following code:

Calendar c = Calendar.getInstance();
int DayOfWeek = c.get(Calendar.DAY_OF_WEEK);

Calendar c = Calendar.getInstance();
int DayOfWeek = c.get(Calendar.DATE);

I'm trying to figure out what functionality or whatever it is to end up with the following result:

Sunday : 6/19/2011, Monday : 6/20/2011, Tuesday : 6/21/2011, Wednesday : 6/22/2011, Thursday : 6/23, 2011, Friday: 6/24/2011, Saturday : 6/25/2011

Select the day of week (as you have above) from the Calendar.

Back off that number of days, which is the "start" day. Then add six days, which will be the "end" day.

Calendar cal = new GregorianCalendar();
int day = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DAY_OF_MONTH, -day);
System.out.println(cal.toString());
cal.add(Calendar.DAY_OF_MONTH, 6);
System.out.println(cal.toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM