This example will show how to get the day of week using java, java 8 date time api and joda date time.
@Test public void get_day_of_week_java () { Calendar cal = Calendar.getInstance(); cal.set(Calendar.MONTH, Calendar.APRIL); cal.set(Calendar.DAY_OF_MONTH, 4); cal.set(Calendar.YEAR, 2014); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); assertEquals(Calendar.FRIDAY, dayOfWeek); }
@Test public void get_day_of_week_java8 () { LocalDate date = LocalDate.of(2014, Month.APRIL, 4); DayOfWeek dayOfWeek = date.getDayOfWeek(); assertEquals(DayOfWeek.FRIDAY, dayOfWeek); // or int dayOfWeek2 = dayOfWeek.get(ChronoField.DAY_OF_WEEK); assertEquals(DayOfWeek.FRIDAY.getValue(), dayOfWeek2); // or LocalDateTime localDateTime = LocalDateTime.of(2014, Month.APRIL, 4, 5, 4); DayOfWeek dayOfWeek3 = localDateTime.getDayOfWeek(); assertEquals(DayOfWeek.FRIDAY, dayOfWeek3); }
@Test public void get_day_of_week_joda () { DateTime dateTime = new DateTime(2014, 4, 4, 0, 0); int dayOfWeek = dateTime.getDayOfWeek(); assertEquals(DateTimeConstants.FRIDAY, dayOfWeek); }