0

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!

Abraham
  • 189
  • 1
  • 18
  • 1
    Could you please provide sample inputs? And MONDAY should be 2, btw. – Gyro Gearless Jun 27 '19 at 10:08
  • In addition to @GyroGearless comment, did you by any chance set first day of week to any other day? Ex.: cal.setFirstDayOfWeek(Calendar.MONDAY); – Dhruvil Vaghela Jun 27 '19 at 10:12
  • @GyroGearless sure! in a normal month it show Tuesday like 7 and Wednesday like 1. And in July it shows Monday like 7 and tuesday like 1. – Abraham Jun 27 '19 at 10:18
  • @DhruvilVaghela Really no. This is the only code that I use with the date. This is the strange... I dont know why it happens. – Abraham Jun 27 '19 at 10:19
  • 2
    I recommend you don’t use `SimpleDateFormat`, `Date` and `Calendar`. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead use `LocalDate`, `DateTimeFormatter` and `DayOfWeek`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 29 '19 at 09:48
  • I cannot reproduce. I tried `01/07/2019` (a Monday in July) and `05/08/2019` (a Monday in August). In both cases I got 2 as expected. – Ole V.V. Jun 29 '19 at 09:52
  • 2
    Same as what Ole V.V. says, but stronger: you **must** drop `Calendar`, `Date` and `SimpleDateFormat` classes immediately and start using the newer Java Date and Time API available in the `java.time` package. – MC Emperor Jun 29 '19 at 10:21
  • @Ole V. V. I finally get an "stable" result , the problem was that the "month" is searched in a Array (January,February,etc), and the Array index, was 1 less that the month. So the app have 1 month less, for example :" 1 January 2019"= "1/0/2019. But I finally solve it changing this array. Thanks! – Abraham Jun 30 '19 at 08:36
  • @MCEmperor Is much better use Java Date vefore than Java Calendar? The result is the same? – Abraham Jun 30 '19 at 08:37
  • Great you got it to work. For the answer to your question to @MCEmperor, also see the edit to my answer. – Ole V.V. Jun 30 '19 at 09:17
  • Abraham, would you consider your question answered? If you still have any doubt, please let us know, we’re still here to help. – Ole V.V. Jul 08 '19 at 10:49

3 Answers3

3

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

achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • Hi! But can I add the "date" object, to this? or Should I create a function that divides the year,month and day to use LocalDate ld = LocalDate.of(year, mont, day);? – Abraham Jun 27 '19 at 11:20
  • 2
    @Abraham Never use the `java.util.Date` or `java.sql.Date` classes. They are terribly flawed, designed by people who did not understand the complexities of date-time handling. – Basil Bourque Jun 29 '19 at 23:08
2

java.time

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/u");
    String fechaaux = "01/07/2019";
    LocalDate date = LocalDate.parse(fechaaux, formatter);
    DayOfWeek dayOfWeek = date.getDayOfWeek();

    System.out.println("dayOfWeek: " + dayOfWeek);

Output is:

dayOfWeek: MONDAY

You should avoid using SimpleDateFormat, Date and Calendar. While I don’t know what went wrong in your code, those classes are long outdated and poorly designed. Instead I recommend java.time, the modern Java date and time API. It is so much nicer to work with.

But can I add the "date" object, to this?

Sometimes we get an old-fashioned Date object from an API that we don’t want to upgrade just now. In that case a possible conversion is:

    ZonedDateTime zdt
            = oldfashionedDate.toInstant().atZone(ZoneId.systemDefault());
    DayOfWeek dayOfWeek = zdt.getDayOfWeek();

Edit:

Is much better use Java Date before than Java Calendar? The result is the same?

You should avoid both. Only if you are getting one of them from a legacy API that you cannot change, accept what you get and convert it to a modern type first thing. If you get a Date, use the conversion above. If you get a Calendar, you can be about certain that it is a GregorianCalendar. If so:

    ZonedDateTime zdt = ((GregorianCalendar) theCalendarYouGot).toZonedDateTime();

Now proceed as above.

Historically Calendar and GregorianCalendar were introduced as a (somewhat failed) attempt to make up for the design problems with Date.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

DAY_OF_WEEK is not the day of the week, it is number from 1 to 7 If it is on Sunday , It will return 1 and so on.

In this case , If it is on Monday It will return 2,

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
Date date = formatter.parse("01/07/2019"); 
Calendar c = Calendar.getInstance(); 
c.setTime(date); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
System.out.println(dayOfWeek);

Out Put:2
Rowi
  • 545
  • 3
  • 9
  • Hi! Thanks for your answer :) Should be 2? What am I doing wrong? I have this code on a jsp page, and always show me Tuesday like 7 and Wednesday like 1. – Abraham Jun 27 '19 at 10:15
  • Could it be a problem with date format? On my case I delete the "0" before day and month if is less than 10. Like this "01/07/2019/ to "1/7/2019" – Abraham Jun 27 '19 at 10:36
  • @Abraham, Yeah if Monday then It Should be 2 [1-7]=[Sunday - Saturday] Maybe, You may have set "setFirstDayOfWeek" to some other day. – Rowi Jun 28 '19 at 02:34
  • 1
    These terrible classes were supplanted years ago by the modern *java.time* classes defined by JSR 310. Suggesting their use in 2019 is poor advice. – Basil Bourque Jun 29 '19 at 23:10