7

Below is my program and it is returning the wrong day name when I enter the related month, date and year.

What I am missing here?

My Program

import java.util.Calendar;
import java.util.Locale;
import java.util.Scanner;

public class TimeTest {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String month = in.next();
        String day = in.next();
        String year = in.next();

        System.out.println(getDay(day, month, year));
    }

    private static String getDay(String day, String month, String year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
        return calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());

    }
}      


Output:

09
24
2018
Wednesday       

It should be returning "Monday", as that is the current day.

Talendar
  • 1,841
  • 14
  • 23
user10400282
  • 606
  • 1
  • 6
  • 11
  • Possible duplicate of [Why is January month 0 in Java Calendar?](https://stackoverflow.com/questions/344380/why-is-january-month-0-in-java-calendar) – Ole V.V. Sep 24 '18 at 08:24
  • The `Calendar` class is long outdated and poorly designed. I recommend you forget about the trouble with it and instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Your users may prefer to type the date on one line as `2018-09-24` (or a perhaps better localized format for their locale). So you may use `String dateString = in.next();` and `LocalDate.parse(dateString).getDayOfWeek()`. If you prefer to input three numbers, use `LocalDate.of(year, month, day)`, no adding or subtracting 1. – Ole V.V. Sep 24 '18 at 08:31
  • Also related: [How can I take a user inputted date instead of the current date in the Calendar class in Java?](https://stackoverflow.com/questions/46554701/how-can-i-take-a-user-inputted-date-instead-of-the-current-date-in-the-calendar) – Ole V.V. Sep 24 '18 at 08:58

3 Answers3

7

You are passing 09 as Month assuming it's September but actually in term of Calendar class it's August because months start from 00(Jan), 01(Feb)... and so on in Calendar Class.

Hence in order to the get correct output you need to pass

08 // September Month not August
24
2018

Here is your running code

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
  • Close. [October is 9](https://docs.oracle.com/javase/10/docs/api/constant-values.html#java.util.Calendar.OCTOBER). And 24th October will be a Wednesday. – Ole V.V. Sep 24 '18 at 08:34
  • This is one of many reasons to **never use `Calendar`** class. See the modern solution in the [Answer by Ole V.V.](https://stackoverflow.com/a/52482072/642706) – Basil Bourque Sep 24 '18 at 22:19
1

Neeraj Jain has already in another answer explained why you got the unexpected output. I should like to contribute the modern way of obtaining your result.

First, your users may prefer to enter the date on one line in a format of their own locale, for example 9/24/18 in the USA or 24/09/2018 in French. It’s not more complicated (when you know how):

    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
                    .withLocale(Locale.getDefault(Locale.Category.FORMAT));
    String dateString = in.next();
    LocalDate date = LocalDate.parse(dateString, dateFormatter);
    DayOfWeek dow = date.getDayOfWeek();
    String dowString = dow.getDisplayName(
                           TextStyle.FULL_STANDALONE,
                           Locale.getDefault(Locale.Category.FORMAT)
                       );
    System.out.println(dowString);

Here’s a sample run of the program in the US locale:

9/25/18
Tuesday

If you or your users do prefer to enter three separate numbers as in your program in the question:

    Scanner in = new Scanner(System.in);
    int month = in.nextInt();
    int day = in.nextInt();
    int year = in.nextInt();
    LocalDate date = LocalDate.of(year, month, day);

Now we’ve got the LocalDate, the remainder is the same as before. A sample run:

9
27
2018
Thursday

Note that there’s no adding or subtracting of 1 anywhere because LocalDate numbers months the same way humans do. I am letting the scanner convert to integers, I find it a bit simpler than calling Integer.parseInt.

The Calendar class you were using is long outdated and poorly designed. I recommend you forget about the trouble with it and instead use LocalDate from java.time, the modern Java date and time API.

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Excellent answer. In your code, I suggest breaking out the `getDayOfWeek` to its own line with a declared `DayOfWeek` variable. This might make it easier to follow, with less “magic”. – Basil Bourque Sep 24 '18 at 17:59
1

By using java.time.LocalDate you can pass your 3 paramter (year, month, day) and get the dayOfWeek:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String month = in.next();
    String day = in.next();
    String year = in.next();

    System.out.println(getDay(day, month, year));
}

private static String getDay(String day, String month, String year) {
    return LocalDate.of(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day)).getDayOfWeek()
            .toString();
}
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
  • 1
    This code returns the name of the day-of-week in English in all-caps. Better to use [`DayOfWeek::getDisplayName`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/DayOfWeek.html#getDisplayName(java.time.format.TextStyle,java.util.Locale)) to automatically localize. See [Answer by Ole V.V.](https://stackoverflow.com/a/52482072/642706). – Basil Bourque Dec 05 '20 at 02:04