Java - Get array of name of day for current week

A function of use to you here is the modulo (%) operator.

Basically, what the modulo operator does is take the remainder of the division, which is exactly what you want. (Remember back in fourth grade when "9 / 2" wasn't 4.5, but 4 remainder 1? This is that remainder part.)

So instead of having:

days[currentDay + x]

Use:

days[(currentDay + x) % 7]

Quick example on values returned by the modulo operator:

 0 % 7 = 0   (0 / 7 = 0 R0)
 1 % 7 = 1   (1 / 7 = 0 R1)
 6 % 7 = 6   (6 / 7 = 0 R6)
 7 % 7 = 0   (7 / 7 = 1 R0)
 8 % 7 = 1   (8 / 7 = 1 R1)
15 % 7 = 1   (15 / 7 = 2 R1)

tl;dr

LocalDate.now( ZoneId.of( "Pacific/Auckland" ) )
         .plusDays( 5 )
         .getDayOfWeek()
         .getDisplayName( TextStyle.FULL , Locale.US )

Details

Get your mind off the idea of array index relating to day-of-week.

Also, generally better to use a modern collection object to gather your objects rather than a mere array.

Also, avoid the troublesome Calendar class. Now supplanted by the java.time classes.

First get the current date. The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

We know the result for yesterday is yesterday so no need to calculate that date. If you do need the date, call LocalDate::minusDays.

We can ask each LocalDate for the name of its day by extracting a DayOfWeek enum object. That DayOfWeek know its own name in any human language that you can specify via a Locale object. So there is no need for you to track an array of template name-of-day strings. Tip: In OOP, try to think of either (a) letting objects be smart take care of themselves, or (b) enlisting the aid of a helper object, rather than using basic arrays to do all the work yourself.

List<String> dayNames = new ArrayList<>( 7 ) ;  // Initialize to seven days of a week.
dayNames.add( "yesterday" ) ;
dayNames.add( "today" ) ;
for( int i = 1 ; i <= 5 ; i ++ ) {
    LocalDate ld = today.plusDays( i ) ;
    String dayName = ld.getDayOfWeek().getDisplayName( TextStyle.FULL , Locale.US ) ;
    dayNames.add( dayName ) ;  // Add each of the five days remaining in a week.
}

Try this code live in IdeOne.com.

[yesterday, today, Tuesday, Wednesday, Thursday, Friday, Saturday]


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.