Reputation: 15592
The simplest way:
String[] namesOfDays = new String[7] {
"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
};
This method does not use Locale. Therefore, if the system's language is not English, this method does not work properly.
Using Joda time, we can do like this:
String[] namesOfDays = new String[7];
LocalDate now = new LocalDate();
for (int i=0; i<7; i++) {
/* DateTimeConstants.MONDAY = 1, TUESDAY = 2, ..., SUNDAY = 7 */
namesOfDays[i] = now.withDayOfWeek((DateTimeConstants.SUNDAY + i - 1) % 7 + 1)
.dayOfWeek().getAsShortText();
}
However, this method uses today's date and calendar calculations, which are useless for the final purpose. Also, it is a little complicated.
Is there an easy way to get Strings like "Sun"
, "Mon"
, ..., "Sat"
with system's default locale?
Upvotes: 31
Views: 41468
Reputation: 16
public class daysOfWeek {
public static String days(String s, int k) {
String[] days = new String[] {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
int len = days.length;
int index = 0;
int result = 0;
for(int i=0; i<len; i++) {
if(days[i]==s) {
index =i;
k%=7;
result = (index +k)%7;
}
}
return days[result];
}
public static void main(String args[]) {
String S ="Tue"; int k =25;
String s =days(S,k);
System.out.println(s);
}
}
Upvotes: 0
Reputation: 464
I think my code is useful. You can change it for your purpose easily.
Result string array is "Sat", "Sun", "Mon", "Tue", "Wed", "Thu" and "Fri".
public String[] getNameOfDays(){
SimpleDateFormat sdf_day_of_week = new SimpleDateFormat("EEE", Locale.getDefault());
String[] nameOfDays = new String[7];
Calendar calendar = Calendar.getInstance();
for(int i=0; i<7; i++) {
calendar.set(Calendar.DAY_OF_WEEK, i);
nameOfDays[i] = sdf_day_of_week.format(calendar.getTime());
}
return nameOfDays;
}
Upvotes: 0
Reputation: 6484
Without 1.8, you can use DateFormatSymbols
(which also works with Android).
DateFormatSymbols.getWeekdays()
Returns: the weekday strings. Use Calendar.SUNDAY, Calendar.MONDAY, etc. to index the result array.
Upvotes: 0
Reputation: 340350
DayOfWeek.MONDAY.getDisplayName(
TextStyle.SHORT ,
Locale.getDefault()
)
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes. Much of java.time is back-ported to Android (see below).
DayOfWeek
The DayOfWeek
enum defines seven objects, one for each day-of-week. The class offers several handy methods including getDisplayName
to generate a string with localized day name.
To localize, specify:
TextStyle
to determine how long or abbreviated should the string be.Locale
to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.Example:
String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.SHORT , Locale.CANADA_FRENCH );
You can use the JVM’s current default time zone rather than specify one. But keep in mind the risk: The default zone can be changed at any moment during execution by any code in any thread of any app running within the JVM.
Locale locale = Locale.getDefault() ;
String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.SHORT , locale );
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?
Upvotes: 4
Reputation: 157487
If I have not misunderstood you
calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);
is what you are looking for. Here you can find the documentation,
Or you can also use, getShortWeekdays()
String[] namesOfDays = DateFormatSymbols.getInstance().getShortWeekdays()
Upvotes: 58
Reputation: 799
Please try this
public static String[] namesOfDays = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
System.out.println("Day := "+namesOfDays[day-1]);
Upvotes: -3
Reputation: 68725
Date now = new Date();
// EEE gives short day names, EEEE would be full length.
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US);
String asWeek = dateFormat.format(now);
You can create the date with your desired date and time. And achieve what you want.
Upvotes: 11