-1

How to find, let's say, what day of the week is June 1st in Java?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
drugsrbad
  • 19
  • 2
  • 4
  • Do you have to use `Date` and `Calendar` and what have you tried so far? – deHaar Jun 23 '21 at 08:02
  • i perfer to use calendar – drugsrbad Jun 23 '21 at 09:51
  • 1
    Please stop! You should definitely prefer **not** to use `Calendar`. That class is poorly designed and long outdated. Instead use `LocalDate` and `DayOfWeek`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 23 '21 at 10:34
  • `YearMonth.now(ZoneId.systemDefault()).atDay(1).getDayOfWeek()`. Running in this month it yields `TUESDAY`. – Ole V.V. Jun 23 '21 at 10:37
  • Welcome to Stack Overflow. Please learn that you are supposed to search before asking a question here and when you post a question, tell us what your search brought up and specify how it fell short of solving your problem. It’s for your own sake since (1) you often find a better answer faster that way (2) it allows us to give preciser and more focused answers. It also tends to prevent or at least reduce the number of downvotes. – Ole V.V. Jun 23 '21 at 10:42
  • Can you combine the answers from the follwing two questions? [Get first date of current month in java](https://stackoverflow.com/questions/14241836/get-first-date-of-current-month-in-java) and [How to determine day of week by passing specific date?](https://stackoverflow.com/questions/5270272/how-to-determine-day-of-week-by-passing-specific-date) I recommend [this answer](https://stackoverflow.com/a/26265701/5772882) and [this one](https://stackoverflow.com/a/26265701/5772882), both by Basil Bourque using java.time. – Ole V.V. Jun 23 '21 at 13:11

3 Answers3

2

Using java.util.Date (legacy time/date classes):

Date date=new Date(); // today
Date date = new Date(2021, 06, 23); // custom
Calendar c = Calendar.getInstance();
c.setTime(date);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); // 3
String dayWeekText = new SimpleDateFormat("EEEE").format(date); //TUESDAY

Using java.time.LocalDate:

LocalDate localDate = LocalDate.of(2021, 06, 23); // custom
LocalDate localDate = LocalDate.now(); // today
java.time.DayOfWeek dayOfWeek = localDate.getDayOfWeek();
dayOfWeek.getValue(); // 4
dayOfWeek.toString(); // WEDNESDAY
LenglBoy
  • 297
  • 2
  • 8
  • What if I wanted a month to be a variable. i.e. Return the day of the week of the first day of the current month. For example if I ran this function on june, this return the day of 1st june, when I run this function on July, this returns the day of 1st july – drugsrbad Jun 23 '21 at 10:08
  • 1
    @drugsrbad Feel free to use the custom/overloaded constructors and add the information. You can use it like: `LocalDate.of(2021, month, 01);` where day is fixed to be 1, the month can be any value by a variable and year is fixed. If you want the year to be dynamic use: `LocalDate.of(LocalDate.now().getYear(), month, 01);` If it should always take just the 1st date of current mont it would be: `LocalDate.of(LocalDate.now().getYear(), LocalDate.now().getMonth(), 01);` – LenglBoy Jun 23 '21 at 10:17
  • 2
    Even if the reader insists on using the poorly designed and long outdated `Date` class (which is not indicated in the question and is not recommended), you should stay **far** away from the three-arg `Date` constructor. It has been deprecated for 24 years because it works unreliably across time zones. – Ole V.V. Jun 23 '21 at 10:39
1

For the case you want the day of week as String, you can use code from this example:

public static void main(String[] args) {
    // provide the date (alternatively, parse it from a String)
    LocalDate firstOfJune2021 = LocalDate.of(2021, 6, 1);
    // then get the day of week by means of a DateTimeFormatter
    String dayOfWeek = firstOfJune2021.format(
                            DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH)
                       );
    // as an alternative, you can get it by chain-calling some methods
    String altDayOfWeek = firstOfJune2021.getDayOfWeek()
                                         .getDisplayName(TextStyle.FULL, Locale.ENGLISH);
    // print the results
    System.out.println(firstOfJune2021 + " was a " + dayOfWeek);
    System.out.println(firstOfJune2021 + " was a " + altDayOfWeek);
}

This code produces the following output:

2021-06-01 was a Tuesday
2021-06-01 was a Tuesday

There's an enum DayOfWeek, which is actually the result type of LocalDate.getDayOfWeek().
It is not clear from your question if you want to use a value like an int, the enum or a String representation...

deHaar
  • 17,687
  • 10
  • 38
  • 51
0

It depends on which version of Java and which types you are using. A simple way to determine the day of the week since Java 8 using LocalDate would look as follows:

LocalDate date = LocalDate.of(2021, 6, 1);
System.out.println(date.getDayOfWeek().getValue());

The returning values are numbered from 1 (Monday) to 7 (Sunday).

A good overview of different approaches can be found in How to determine day of week on Baeldung.

Gregor Zurowski
  • 2,222
  • 2
  • 9
  • 15