Java Get an array of the day name for the current week.

What I'm looking for is to create an array of days of the week in java starting from yesterday and go up to six days per se

String daysWeek[] = { "Yesterday", "Today", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; 

The first two elements of the array that I want to return both yesterday and today.

At first it seemed like a simple task using

 currentDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK); String daysList[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; String daysWeek[] = {"Yesterday", "Today", daysList[currentDay], daysList[currentDay+1], ...}; 

The note above daysList[currentDay] will return tomorrow, since the daysList array starts with 0 ie, if currentDay = 3 , which today speaks on Tuesday, then it will be daysList[2] .

But my problem is that if currentDay is 7, which is Saturday today, then currentDay+1 , which will be tomorrow, will be the eighth element of an array that does not exist.

Is there any one of them in which I can get around my numbers if today is Wednesday or later, and then times currentDay + x > 7 , set currentDay back to 1?

This all happens in one method called getDaysList(currentDay) , which returns the daysWeek[] array.

+6
source share
4 answers

The use function for you here is the modulo operator ( % ).

Basically, what the modulo operator does is take up the remainder of the division, which is exactly what you want. (Remember that in fourth grade, when “9/2” was not 4.5, but 4 was 1 remainder? This is that part of the remainder.)

So, instead of:

 days[currentDay + x] 

Using:

 days[(currentDay + x) % 7] 

A quick example of the 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) 
+7
source

You can use the remainder operator ( % ):

 6 % 7 == 6 7 % 7 == 0 8 % 7 == 1 
+1
source

TL; DR

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

More details

Take a break from the idea of ​​an array index related to the day of the week.

In addition, it is usually better to use a modern collection object to collect your objects, rather than a simple array.

Also avoid the problematic Calendar class. Now java.time classes are being superseded .

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

The time zone is critical for determining the date. At any given moment, the date changes around the world by zone. For example, a few minutes after midnight in Paris, France is a new day, still "yesterday" in Montreal Quebec .

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

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

We know that yesterday’s result is yesterday , so there’s no need to calculate this date. If you need a date, call LocalDate::minusDays .

We can set each LocalDate for the name of our day by retrieving the DayOfWeek enumeration object. It is DayOfWeek to know your own name in any human language that you can specify through the Locale object. Therefore, you do not need to track an array of string name names. Tip. In OOP, try to think either (a) to let the objects be smart, take care of yourself, or (b) enlist the help of an auxiliary object, rather than using the main 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 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 supersede the nasty old legacy datetime classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , is advised to switch to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

+1
source
 import java.text.SimpleDateFormat; import java.util.*; class Date {</p><br> public static void main(String args[]) { </p><br> // Monday 01/27/2019 15:07:53 AM System.out.println("Current Day: "+new SimpleDateFormat("EEEE MM-dd-yyyy HH:mm:ss a").format(Calendar.getInstance().getTime()) ); } } 
-1
source

All Articles