0

I did a simple example in hackerrank.com which asks us to return the day of a given date. ex: If the date is 08 05 2015(month day year) it should return WEDNESDAY.

This is the code I wrote for this task

public static String getDay(String day, String month, String year) {
    String[] dates=new String[]{"SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"};
    Calendar cal=Calendar.getInstance();
    cal.set(Integer.valueOf(year),Integer.valueOf(month),Integer.valueOf(day));
    int date_of_week=cal.get(Calendar.DAY_OF_WEEK);
    return dates[date_of_week-1];
}

My code returns 'Saturday' for the given example which should be 'Wednesday'. For the current date which is 10 29 2017, it returns 'Wednesday'. Could anyone please help me to solve this issue?

Nipuna Dilhara
  • 424
  • 2
  • 6
  • 18
  • 3
    Do you really have to use `Calendar` rather than the more modern `java.time` API? `LocalDate` is considerably nicer than this. (Also, it feels very odd to accept parameters in the order day/month/year rather than year/month/day; and why accept them as strings rather than integers? If the calling code only has strings, it can perform the parsing itself.) – Jon Skeet Oct 29 '17 at 09:12
  • @JonSkeet Yes, I tried LocalDate. It's better than using Calendar. – Nipuna Dilhara Oct 29 '17 at 10:51

2 Answers2

9

Assuming you're using Java 8+, you could use LocalDate and something like

public static String getDay(String day, String month, String year) {
    return LocalDate.of(
            Integer.parseInt(year),
            Integer.parseInt(month),
            Integer.parseInt(day)
    ).getDayOfWeek().toString();
}

Also, note that you describe the method as taking month, day and year but you implemented it taking day, month and year (make sure you are calling it correctly). I tested the above with

public static void main(String[] args) throws Exception {
    System.out.println(getDay("05", "08", "2015"));
    System.out.println(getDay("29", "10", "2017"));
}

And I get (as expected)

WEDNESDAY
SUNDAY

If you can't use Java 8 (or just to fix your current solution), Calendar takes a month offset from 1 (Calendar#JANUARY is 0). So you would need (and prefer parseInt to valueOf, the first returns a primitive - the second an Integer instance) something like

public static String getDay(String day, String month, String year) {
    String[] dates = new String[] { "SUNDAY", "MONDAY", "TUESDAY", //
            "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
    Calendar cal = Calendar.getInstance();
    cal.set(Integer.parseInt(year), //
            Integer.parseInt(month) - 1, // <-- add -1
            Integer.parseInt(day));
    int date_of_week = cal.get(Calendar.DAY_OF_WEEK);
    return dates[date_of_week - 1];
}

which gives the same result as above.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

Month in Calendar is zero based in cal.set(int,int,int). If I call your method with getDay("29", "9", "2017") it returns Sunday. So call your method with one less month i.e. 9, or call it with the Calendar constant for the month (Calendar.OCTOBER), or do month+1 in the call to cal.set.

See this running demo: https://ideone.com/A6WGRJ. I also added a print of the date to confirm it prints the correct date.

Vasan
  • 4,810
  • 4
  • 20
  • 39