Day of the week from date in int format

1

I want to make a function that from three values in int (day month year) returns me that day of the week is, googling I have reached this function but I get an error, it does not hit with the day it is.

This is the code:

   String diaSemana (int dia, int mes, int ano)
   {
    String letraD="";
    int nD =-1;
    Calendar c = Calendar.getInstance();

    c.set(ano, mes, dia);
    nD=c.get(Calendar.DAY_OF_WEEK); 
    switch (nD){
        case 1: letraD = "D";
            break;
        case 2: letraD = "L";
            break;
        case 3: letraD = "M";
            break;
        case 4: letraD = "X";
            break;
        case 5: letraD = "J";
            break;
        case 6: letraD = "V";
            break;
        case 7: letraD = "S";
            break;
    }

    return letraD;
}

I leave here a log.i that I have to debug: Weekday: 4 2017,4,10 I should go out a week: 2

    
asked by midlab 10.04.2017 в 22:25
source

3 answers

1

I put the solution that I found I used GregorianCalendar finally and I had to subtract one a month (January is month 0). This is 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-1, dia);
        int nD=calendar.get(Calendar.DAY_OF_WEEK);
        Log.i("result","diaSemana: "+nD+" dia:"+dia+" mes:"+mes+ "año:" +ano);
        switch (nD){
            case 1: letraD = "D";
                break;
            case 2: letraD = "L";
                break;
            case 3: letraD = "M";
                break;
            case 4: letraD = "X";
                break;
            case 5: letraD = "J";
                break;
            case 6: letraD = "V";
                break;
            case 7: letraD = "S";
                break;
        }

        return letraD;
    }

I have also found this one that is more simple and intuitive:

 Calendar c = Calendar.getInstance();
 c.set(año,mes,dia) // vairables int
 int dia =  c.get(Calendar.DAY_OF_WEEK);
 if(dia==Calendar.SUNDAY){
   //Domingo
 }if(dia==Calendar.MONDAY){
   //Lunes
 }
if(dia==Calendar.TUESDAY){
   //Martes
 }
...
    
answered by 11.04.2017 / 12:20
source
1

How about You can do it like this.

public class TryDateFormats 
{
    public static void main(String[] args) throws ParseException 
   {       
        String month = "08";
        String day = "05";
        String year = "2015";
        String inputDateStr = String.format("%s/%s/%s", day, month, year);
        Date inputDate = new SimpleDateFormat("dd/MM/yyyy").parse(inputDateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(inputDate);
        String dayOfWeek = calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US).toUpperCase();
        System.out.println(dayOfWeek);
    }
}
    
answered by 10.04.2017 в 22:47
1

The 1 is Sunday and the month of January.

Calendar c = Calendar.getInstance ();

c.set(2017, 3, 1); //año, mes, día  


        int diaSemana = c.get(Calendar.DAY_OF_WEEK);
        if (diaSemana == 1) {
            Valor_dia = "Domingo";
        } else if (diaSemana == 2) {
            Valor_dia = "Lunes";
        } else if (diaSemana == 3) {
            Valor_dia = "Martes";
        } else if (diaSemana == 4) {
            Valor_dia = "Miercoles";
        } else if (diaSemana == 5) {
            Valor_dia = "Jueves";
        } else if (diaSemana == 6) {
            Valor_dia = "Viernes";
        } else if (diaSemana == 7) {
            Valor_dia = "Sabado";
        }
        return Valor_dia;
        }
    
answered by 11.04.2017 в 01:06