Search code examples
javadayofweekjava.util.calendar

Calendar work in all the year except in July


I have an app that use a calendar and I need to take the day of week. The problem is that it takes the correct day of week in all the dates around the year, except in July.

I tried to add differents dates around all the year, but this problem only happens in July.

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(fechaaux);
Calendar c = Calendar.getInstance();
c.setTime(date);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

If fechaux is in a Monday of August this returns 6 (this is okay), but if I add a date in Monday on July this returns 7, when it should show 6. Why it happens?

Thanks!


Solution

  • If you can use the actual day of the week - consider using LocalDate (Java 8+) if possible:

    LocalDate ld = LocalDate.of(2019, 6, 27);
    System.out.println(ld.getDayOfWeek());
    

    Output:

    THURSDAY