Return day of the week from a date in the format dd / mm / yyy

4

I have this function that brings me the day of the week:

public static String getWeek(String date){ //ex 07/03/2017
    String dayWeek = "---";
    GregorianCalendar gc = new GregorianCalendar();
    try {
        gc.setTime(new SimpleDateFormat("MM/dd/yyyy", BRAZIL).parse(date));
        switch (gc.get(Calendar.DAY_OF_WEEK)) {
            case Calendar.SUNDAY:
                dayWeek = "DOM";
                break;
            case Calendar.MONDAY:
                dayWeek = "SEG";
                break;
            case Calendar.TUESDAY:
                dayWeek = "TER";
            break;
            case Calendar.WEDNESDAY:
                dayWeek = "QUA";
                break;
            case Calendar.THURSDAY:
                dayWeek = "QUI";
                break;
            case Calendar.FRIDAY:
                dayWeek = "SEX";
                break;
            case Calendar.SATURDAY:
                dayWeek = "SAB";

        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return dayWeek;
}

But it's bringing me the day of the previous week, ex: 03/03/2017 Tuesday it brings as Monday.

    
asked by anonymous 07.03.2017 / 04:59

2 answers

6

Your date formatting mask is wrong. You are using MM/dd/yyyy format when your string is given a dd/MM/yyyy format. Just change the mask:

public static String getWeek(String date){ //ex 07/03/2017
    String dayWeek = "---";
    GregorianCalendar gc = new GregorianCalendar();
    try {
        gc.setTime(new SimpleDateFormat("dd/MM/yyyy").parse(date));
        switch (gc.get(Calendar.DAY_OF_WEEK)) {
            case Calendar.SUNDAY:
                dayWeek = "DOM";
                break;
            case Calendar.MONDAY:
                dayWeek = "SEG";
                break;
            case Calendar.TUESDAY:
                dayWeek = "TER";
            break;
            case Calendar.WEDNESDAY:
                dayWeek = "QUA";
                break;
            case Calendar.THURSDAY:
                dayWeek = "QUI";
                break;
            case Calendar.FRIDAY:
                dayWeek = "SEX";
                break;
            case Calendar.SATURDAY:
                dayWeek = "SAB";

        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return dayWeek;
}

Just changed this and worked correctly, see: link

    
07.03.2017 / 11:22
2

Your mask is wrong, as Articuno replied.

You can also shorten your code to:

public static String getWeek(String date){ //ex 07/03/2017
        String dayWeek = "---";
        GregorianCalendar gc = new GregorianCalendar();
        try {
            gc.setTime(new SimpleDateFormat("dd/MM/yyyy", new Locale("pt", "BR")).parse(date));
           return new SimpleDateFormat("EEE", new Locale("pt", "BR")).format(gc.getTime()).toUpperCase();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dayWeek;
    }
    
11.09.2017 / 16:55