How to Determine Day of Week by Passing Specific Date

How to determine day of week by passing specific date?

Yes. Depending on your exact case:

  • You can use java.util.Calendar:

    Calendar c = Calendar.getInstance();
    c.setTime(yourDate);
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
  • if you need the output to be Tue rather than 3 (Days of week are indexed starting at 1 for Sunday, see Calendar.SUNDAY), instead of going through a calendar, just reformat the string: new SimpleDateFormat("EE").format(date) (EE meaning "day of week, short version")

  • if you have your input as string, rather than Date, you should use SimpleDateFormat to parse it: new SimpleDateFormat("dd/M/yyyy").parse(dateString)

  • you can use joda-time's DateTime and call dateTime.dayOfWeek() and/or DateTimeFormat.

  • edit: since Java 8 you can now use java.time package instead of joda-time

Determine day of the week by passing specific date?

The month value starts at '0'. So '10' will be for 'November'. So when the value '11' is passed in your code, it gives the day for the month of December.

See the documentation.

How to get which day of week is a specific date?

If you are using a datetime.date object you can use the weekday() function. Converting to a date object inside your module should be trivial if you are passing in the day, month and year.

https://docs.python.org/3.7/library/datetime.html#datetime.date.weekday

Numeric representation

Example function:

import datetime

def weekday_from_date(day, month, year):
return datetime.date(day=day, month=month, year=year).weekday()

Example usage:

>>> weekday_from_date(day=1, month=1, year=1995)
6

String representation

Combined with the calendar module and the day_name array you can also get it displayed as a string easily.

https://docs.python.org/3.7/library/calendar.html#calendar.day_name

Example function:

import datetime
import calendar

def weekday_from_date(day, month, year):
return calendar.day_name[
datetime.date(day=day, month=month, year=year).weekday()
]

Example usage:

>>> weekday_from_date(day=1, month=1, year=1995)
'Sunday'

Unit Tests

Using pytest writing a series of super fast unit tests should be straightforward to prove it's correctness. You can add to this suite as you please.

import pytest

@pytest.mark.parametrize(
['day', 'month', 'year', 'expected_weekday'],
[
(1, 1, 1995, 6),
(2, 1, 1995, 0),
(3, 1, 1995, 1),
(6, 6, 1999, 6)
]
)
def test_weekday_from_date(day, month, year, expected_weekday):
assert weekday_from_date(day, month, year) == expected_weekday

Finding the day of week of a given date

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.

How to get the weekday of a Date?

You can get the day-integer like that:

Calendar c = Calendar.getInstance();
c.setTime(yourdate); // yourdate is an object of type Date

int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); // this will for example return 3 for tuesday

If you need the output to be "Tue" rather than 3, instead of going through a calendar, just reformat the string: new SimpleDateFormat("EE").format(date) (EE meaning "day of week, short version")

Taken from here: How to determine day of week by passing specific date?

getDay() method to return day of the week for a given date not works

Your return is off; you don't want cal.get in the first column of cal.getDisplayName. Currently, I get the month name with your code. Change that to

return cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());

And call it like

public static void main(String[] args) {
System.out.println(getDay("14", "8", "2017"));
}

And I get (as expected)

Monday

In new code, I would prefer the new classes in java.time (Java 8+), and a DateTimeFormatter - like,

public static String getDay(String day, String month, String year) {
int y = Integer.parseInt(year), m = Integer.parseInt(month), d = Integer.parseInt(day);
return java.time.format.DateTimeFormatter.ofPattern("EEEE")
.format(LocalDate.of(y, m, d));
}


Related Topics



Leave a reply



Submit