Get next 7 days starts from the current day

橙三吉。 提交于 2019-12-11 03:46:29

问题


I got 7 days of week starts from Monday of week. But in my project, i want get next 7 days starts from the current day. Example : if Today is Monday 09/12/2013. List like below :

Monday, 09/12/2013
Tuesday, 10/12/2013
Wednesday, 11/12/2013
Thursday, 12/12/2013
Friday, 13/12/2013
Saturday, 14/12/2013
Sunday, 15/12/2013

Next: if Today is Tuesday 10/12/2013. List like below :

Tuesday, 10/12/2013
Wednesday, 11/12/2013
Thursday, 12/12/2013
Friday, 13/12/2013
Saturday, 14/12/2013
Sunday, 15/12/2013
Monday, 16/12/2013

My code get 7 days of week starts from monday

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Calendar date = Calendar.getInstance();
date.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
/*
* Get Date in 7 days
*/
for(int i = 0; i < 7;i++){
    Calendar[i] = format.format(date.getTime());
    date.add(Calendar.DATE  , 1);
    System.out.println("date :" + Calendar[i]);
}

回答1:


String dt = "2008-01-01";  // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 7);  // number of days to add
dt = sdf.format(c.getTime());  // dt is now the new date

took from this thread

How can I increment a date by one day in Java?




回答2:


You leave your code exactly how it is and just remove your set call. When you call getInstance for a calendar you will get back a calendar instance that is set to the current date and time.

Ex:

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Calendar date = Calendar.getInstance();

for(int i = 0; i < 7;i++){
    Calendar[i] = format.format(date.getTime());
    date.add(Calendar.DATE  , 1);
    System.out.println("date :" + Calendar[i]);
}



回答3:


Other answers are correct.

Joda-Time

For fun I did the same kind of code but using the Joda-Time 2.3 library.

First Moment Of The Day

Note the calls to withTimeAtStartOfDay(). When working with date-time values but focusing on the date portion, it is a good practice to set time to first moment of the day. Caution: Never set time to all zeros – that time may not exist as Daylight Savings Time (DST) can push the clock past midnight.

One reason to get first moment of the day is just for the heck of it, to avoid possible anomalies or issues with rolling over to another day. Another reason is to make the code self-documenting, showing our intent to focus on the date rather than the time.

Date Only

If you truly want only date, use Joda-Time's LocalDate class instead. But think twice. Often when people naïvely believe they want date only, they actually need date-time.

Example Code

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

DateTime today = new DateTime().withTimeAtStartOfDay();

// ISO 8601 format
for(int i=0; i<7; i++){
    System.out.println( today.plusDays( i ).withTimeAtStartOfDay() );
}

// User's default "short" format.
for(int i=0; i<7; i++){
    System.out.println( DateTimeFormat.shortDate().print( today.plusDays( i ) ) );
}

// Specific format demanded by the StackOverflow.com question.
DateTimeFormatter formatter = DateTimeFormat.forPattern( "dd/MM/yyyy" );
for(int i=0; i<7; i++){
    System.out.println( formatter.print( today.plusDays( i ) ) );
}

When run…

2013-12-08T00:00:00.000-08:00
2013-12-09T00:00:00.000-08:00
2013-12-10T00:00:00.000-08:00
2013-12-11T00:00:00.000-08:00
2013-12-12T00:00:00.000-08:00
2013-12-13T00:00:00.000-08:00
2013-12-14T00:00:00.000-08:00
12/8/13
12/9/13
12/10/13
12/11/13
12/12/13
12/13/13
12/14/13
08/12/2013
09/12/2013
10/12/2013
11/12/2013
12/12/2013
13/12/2013
14/12/2013



回答4:


SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String[] days = new String[7];
for(int i = 0; i < 7;i++){
    days[i] = format.format(calendar.getTime());
    calendar.add(Calendar.DATE  , 1);
    Log.d("Days" + i, "date :" + days[i]);
}


来源:https://stackoverflow.com/questions/20456793/get-next-7-days-starts-from-the-current-day

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!