Reputation: 189
I want to get the day of the week (Monday, Tuesday, ..) from this function that receives as parameters 3 int that are the year the month and day, I have tried both Calendar and GregorianCalendar and it still gives me error, for example if I pass the date today (2017,11,04) it gives me result of the day of the week number 5, Thursday being today Tuesday I leave you the code
String diaSemana (int dia, int mes, int ano)
{
String letraD="";
/*Calendar c = Calendar.getInstance();
c.set(ano, mes, dia, 0, 0, 0);
nD=c.get(Calendar.DAY_OF_WEEK);*/
TimeZone timezone = TimeZone.getDefault();
Calendar calendar = new GregorianCalendar(timezone);
calendar.set(ano, mes, dia);
int nD=calendar.get(Calendar.DAY_OF_WEEK);
Log.i("result","diaSemana: "+nD+" dia:"+dia+" mes:"+mes+ "año:" +ano);
switch (nD){
case 0: letraD = "D";
break;
case 1: letraD = "L";
break;
case 2: letraD = "M";
break;
case 3: letraD = "X";
break;
case 4: letraD = "J";
break;
case 5: letraD = "V";
break;
case 6: letraD = "S";
break;
}
return letraD;
}
Log.i shows this message:
diaSemana:5 dia:11 mes:4 año:2017
Upvotes: 0
Views: 128
Reputation: 86203
Update: The java.time classes did come to Android 26+ and are built in there. For earlier Android (25-), the latest tooling provide most of the java.time functionality via “API desugaring”.
I know you tagged your question android and I know the LocalDate
class that I use in my solution is not built-in on Android yet (it will come). To use this on Android, get ThreeTenABP, see How to use ThreeTenABP in Android Project. To use it on Java 8 or later, just go ahead, everything is built-in.
static Locale español = Locale.forLanguageTag("es");
static String diaSemana (int dia, int mes, int ano)
{
return LocalDate.of(ano, mes, dia)
.getDayOfWeek()
.getDisplayName(TextStyle.NARROW, español);
}
Could it be this simple? Let’s test it with the days of the first whole week of next month:
for (int dia = 2; dia <= 8; dia++) {
System.out.format(español, "%-2s%2d%n", diaSemana(dia, 7, 2017), dia);
}
This prints
D 2
L 3
M 4
X 5
J 6
V 7
S 8
Even though Wednesday is miércoles in Spanish, I believe the letters printed fully agree with what you asked for.
For non-Android programmers the Calendar
class and its subclass GregorianCalendar
are long outdated. Their better and more programmer-friendly replacements came out with Java 8 in 2014. Fortunately they have also been backported to Java 6 and 7 and specifically to Android. For starters, the newer classes have no funny surprises about month numbers, and you need not care about the numbering of days of the week.
Upvotes: 3
Reputation: 28228
Try this:
Calendar calendar = GregorianCalendar.getInstance();
Otherwise the calendar variables aren't initalized and are set to their defaults. Meaning you get the wrong date.
However, in getting the field, you do it right.
Note though: Day Of week starts on sunday. If you press CTRL+Left click on DAY_OF_WEEK you see the details.
/**
* Field number for <code>get</code> and <code>set</code> indicating the day
* of the week. This field takes values <code>SUNDAY</code>,
* <code>MONDAY</code>, <code>TUESDAY</code>, <code>WEDNESDAY</code>,
* <code>THURSDAY</code>, <code>FRIDAY</code>, and <code>SATURDAY</code>.
*
* @see #SUNDAY
* @see #MONDAY
* @see #TUESDAY
* @see #WEDNESDAY
* @see #THURSDAY
* @see #FRIDAY
* @see #SATURDAY
*/
All though, if you use GregorianCalendar it may start with monday. But what you have to note is that the first day is not 0, the first day of the week starts with 1. This is what it says on sunday:
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Sunday.
*/
public final static int SUNDAY = 1;
Monday is 2, and so on.
However, that may vary based on what the device has set as the first day of the week. Add some logtags and see what it is after you change how you get the calendar (see the first line of code in this answer)
Upvotes: 0
Reputation: 400
The problem is that months start with 0: January = 0, February = 1 etc. Use this:
calendar.set(ano, mes - 1, dia);
Upvotes: 1