Reputation: 999
this is probably a very unique question because I have searched on Google for a while & could not find a solution. It is also interesting.
What I need bascailly is a number, which you can get the day name by it.
For example our number is 7, we need to go through a formula to find out it's day name, but for 7 it's basic, all you do is just get "saturday" in the switch statement.
Basically it's simple to get the name, you just done:
switch (dayNumber) {
case 1: return "Sunday"; etc...
But my question is pretty complicated I think, and I am not sure if logically there is a possible solution for it.
I want to get the index number of the week, by the number.
examples:
Why do I need this:
Well I had a question recently in a java course, not related to this, but it has something to do with looping through the month days, so I wanted to be creative & even get the name of the specific day, in a quick way using Mathematics functions and formulas.
So example on how I want to get the name:
for (int i = 1; i <= 31; i++) {
System.out.println(getDayByInteger(i));
}
public static String getDayByInteger(int day) {
int dayNumber = 0; //TODO Formula..
switch (dayNumber) {
case 1:
return "Sunday";
case 2:
return "Monday";
case 3:
return "Tuesday";
case 4:
return "Wednesday";
case 5:
return "Thursday";
case 6:
return "Friday";
case 7:
return "Saturday";
default: return "N/A";
}
}
I don't mind if it only will support the number range 1-31, but my question focuses on if it's possible to make it work with any number, because you can always build a formula to work for a specific numbers range I think.
Is it even possible for any number? Or will it require checking statements?
Upvotes: 1
Views: 882
Reputation: 338584
int yourDayNumber = 9 ; // 1 = Sunday, 2 = Monday, … , 8 = Sunday, 9 = Monday, …
int isoNumber = ( ( ( yourDayNumber % 7 ) - 1 ) < 1 ) ? ( 7 + ( ( yourDayNumber % 7 ) - 1 ) ) : ( ( yourDayNumber % 7 ) - 1 );
String output = DayOfWeek.of( isoNumber ).getDisplayName( TextStyle.FULL , Locale.US ) // Or Locale.CANADA_FRENCH or Locale.TAIWAN etc.
The Answer by kai is correct for the math part, using % 7
. I consolidated that into a single obnoxious line above using a ternary function (see expanded code in last section).
For the other part, getting name of day, you can use new features built into Java using java.time classes.
DayOfWeek
enumThe DayOfWeek
enum provides a singleton instance for each of the sever days of the week.
The days are numbered according to the ISO 8601 standard, 1-7 for Monday-Sunday.
DayOfWeek dow = DayOfWeek.of( 1 ); // Monday is # 1.
The DayOfWeek::getDisplayName
method translates the name of the day into any human language specified by a Locale
such as Locale.US
or Locale.CANADA_FRENCH
.
String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ); // Or Locale.US or Locale.ITALY etc.
lundi
The purpose of such an enum is to use these objects instead of integers. Rather than pass around mere integer numbers, pass around objects such as DayOfWeek
. This makes your code more self-documenting, ensures a range of valid values, and provides type-safety.
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in 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.
If you must count Sunday as first day of week, adjust to get the ISO 8601 value.
By subtracting one from your code number, we get 1-5 for Monday-Friday. That is just what we want for ISO 8601 numbers. But for Saturday we get -1 and for Sunday we get 0. I take advantage of that fact by adding the negative one or zero to seven to get the desired ISO number of 6 for Saturday and 7 for Sunday. There may be a neater way of doing this, but this approach succeeds according to my limited testing.
int yourDayNumber = 8; // 1 = Sunday, 2 = Monday, … , 8 = Sunday, 9 = Monday, etc.
int modResult = ( yourDayNumber % 7 );
int isoNumber = ( modResult - 1 );
if ( isoNumber < 1 ) {
isoNumber = ( 7 + isoNumber );
} else { // Else modResult is 1-5
// No code needed here.
}
Dump to console.
System.out.println ( "yourDayNumber: " + yourDayNumber + " | isoNumber: " + isoNumber + " | DayOfWeek: " + DayOfWeek.of ( isoNumber ) );
Upvotes: 0
Reputation: 414
Not clear what you are really looking for, if you wish to figure out the day of the week for a specific date, you might check the following: How to find which day of the week it is - Java
Upvotes: 0
Reputation: 7804
Try this out:
public static String getDayByInteger(int day) {
int dayNumber = day % 7; // Formula..
switch (dayNumber) {
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
default:
return "N/A";
}
}
Upvotes: 0
Reputation: 3802
you can use reminder operator %
for this
public static String getDayByInteger(int day) {
int dayNumber = (day % 7) + 1;
switch (dayNumber) {
// case 1 to 7
}
}
Upvotes: 1
Reputation: 6887
You only have to use modulu and edit your cases:
switch (dayNumber%7) {
case 1:
return "Sunday";
case 2:
return "Monday";
case 3:
return "Tuesday";
case 4:
return "Wednesday";
case 5:
return "Thursday";
case 6:
return "Friday";
case 0: //change case 7 to case 0
return "Saturday";
default: return "N/A";
}
Upvotes: 3