2

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]);
}
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
Tungken
  • 1,917
  • 1
  • 15
  • 18
  • The 2018 solutionis to use [`LocalDate.datesUntil()`](https://docs.oracle.com/javase/9/docs/api/java/time/LocalDate.html#datesUntil-java.time.LocalDate-). – Ole V.V. Jul 13 '18 at 07:22

4 Answers4

4
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?

Community
  • 1
  • 1
code4jhon
  • 5,725
  • 9
  • 40
  • 60
1

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]);
}
Bobbake4
  • 24,509
  • 9
  • 59
  • 94
  • The code is not 100% correct. `Calendar[i]` is supposed to be an array of strings. Create a string like that `String[] calendarDays = new String[7];` After, you use `calendarDays[i] = format.format(calendar.getTime());` – André Luiz Reis Jul 12 '18 at 20:34
1

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
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1
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]);
}
André Luiz Reis
  • 2,273
  • 21
  • 14
  • Code only answers are seldom useful. And IMHO in 2018 an answer using the long outdated and poorly designed `Calendar` class and the notoriously troublesome `SimpleDateFormat` should at the very least contain a note about this. – Ole V.V. Jul 13 '18 at 07:25
  • Also you don’t seem to answer the question (and I quote and emphasize) “i want get next 7 days starts **from the current day**.” – Ole V.V. Jul 13 '18 at 08:00