Reputation: 125
I have an int called dayCounter
that runs 1 to 7. And i have a String called day
. dayCounter
increments every 10 seconds using TimerTask
.
I use the following if statement inside the private void run()
method used for TimerTask
::
if (dayCounter == 1) {
day = "Monday";
}
I have an if statement like this for every day of the week. Is there a more efficient way which gets the same result. I was thinking maybe creating an array with holds String month and then increment it some way. But i wouldn't know how to go about it. Any ideas?
Upvotes: 2
Views: 354
Reputation: 14255
Probably the easiest way would be to use an array for the days:
public class Demo {
public static void main(String[] args) {
System.out.println(getDay(1));
System.out.println(getDay(7));
}
// Returns the corresponding day for the given dayCounter.
// Assumes that dayCounter goes from 1-7.
private static String getDay(int dayCounter) {
String[] days = new String[] { "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday" };
return days[dayCounter - 1];
}
}
If your counter goes above 7 (e.g. you are incrementing it by 1 in each timer step), you might want to be interested in this version, which basically loops through all available days:
public class Demo {
public static void main(String[] args) {
System.out.println(getDay(1));
System.out.println(getDay(7));
System.out.println(getDay(8));
System.out.println(getDay(13));
}
// Returns the corresponding day for the given dayCounter.
// Maps dayCounter to the range of 1-7, assumes that dayCounter
// goes from 1-Integer.MAX_VALUE.
private static String getDay(int dayCounter) {
String[] days = new String[] { "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday" };
return days[dayCounter % 7];
}
}
Or you could use DateFormatSymbols
to retrieve the weekdays and get them even (nearly) automatically translated for you:
import java.text.DateFormatSymbols;
public class Demo {
public static void main(String[] args) {
System.out.println(getDay(1));
System.out.println(getDay(7));
}
// Returns the corresponding day for the given dayCounter.
// Assumes that dayCounter goes from 1-7.
private static String getDay(int dayCounter) {
String[] days = DateFormatSymbols.getInstance().getWeekdays();
return days[dayCounter];
}
}
Or for Java 8 and later, the currently preferred way might be to make use of new java.time API (you can play around with the method arguments). This API includes a handy DayOfWeek
enum.
import java.time.DayOfWeek;
import java.time.format.TextStyle;
import java.util.Locale;
public class Demo {
public static void main(String[] args) {
System.out.println(getDay(1));
System.out.println(getDay(7));
}
// Returns the corresponding day for the given dayCounter.
// Assumes that dayCounter goes from 1-7.
private static String getDay(int dayCounter) {
return DayOfWeek.of(dayCounter)
.getDisplayName(TextStyle.FULL, Locale.getDefault());
}
}
And if you just want to replace the if
statements, go with the switch-case
as mentioned in Ryan's answer.
Upvotes: 3
Reputation: 718
You can have an array of strings, dayOfWeek, one for each day of the week. Then you use dayCounter as as index into the array. For example, if dayCounter = 1, then dayOfWeek[dayCounter] would be "Monday". This way you need only one statement to get the name of the day of the week.
Upvotes: 1
Reputation: 1974
Other then a if statement you can use a switch case with the counter
switch(dayCounter){
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
}
Although this is another way to do it they both create the same byte code and the only difference between these two options is the readability of the code.
Upvotes: 4