0

I am trying to get the day of week from an arraylist that contains String of date in the simple format ("yyyy-MM-dd"). For example, I have the arraylist = {"2021-04-01", "2021-05-03", "2020-06-17"}. Is it possible to determine what day of week belong to each of these dates? So for the first String of "2021-04-01", I would like to turn it into "Thursday".

Thank you for your help!

  • @Meninx-メネンックス - Your link does not answer this question because this question has two requirements: (1) Parse a date string (2) Get the day name of the given date. The link you have posted does not answer any of these requirements because that is about how to parse the given date as `2011-02-MON`. – Arvind Kumar Avinash Jul 20 '21 at 17:05
  • Thank you for referring me to this post! For my case, then I would use: new SimpleDateFormat("EEEE").format(new Date()); But where do I put my String of date "2021-04-01"? Is it inside the new Date("2021-04-01"), like this? Thank you – Brian McCanaugh Jul 20 '21 at 17:10
  • 1
    @BrianMcCanaugh - The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. It is recommended to stop using them completely and switch to the [modern Date-Time API](https://www.oracle.com/technical-resources/articles/java/jf14-Date-Time.html). I would also like to understand what is that you have not understood from the given answer that you are looking for `SimpleDateFormat`. – Arvind Kumar Avinash Jul 20 '21 at 17:11
  • 1
    @Meninx-メネンックス - You may like to read [Handling Duplicate Questions](https://stackoverflow.blog/2009/04/29/handling-duplicate-questions/). – Arvind Kumar Avinash Jul 20 '21 at 17:12
  • @BrianMcCanaugh instead of `new Date()` you can use the `date` variable defined as this `Date date = new SimpleDateFormat("dd-MM-yyyy").parse("01-04-2021");` – Meninx - メネンックス Jul 20 '21 at 17:23

1 Answers1

4

Your date strings conform to the ISO 8601 standards. The modern Date-Time API is based on ISO 8601 and does not require using a DateTimeFormatter object explicitly as long as the Date-Time string conforms to the ISO 8601 standards.

Simply parse the strings using LocalDate#parse and use LocalDate#getDayOfWeek to get the day of the week e.g.

System.out.println(
    LocalDate.parse("2021-04-01")
             .getDayOfWeek()
             .getDisplayName(TextStyle.FULL, Locale.ENGLISH)
);

Demo with some more styles:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        Stream.of(
                "2021-04-01", 
                "2021-05-03", 
                "2020-06-17"
        ).forEach(s -> 
            System.out.printf(
                "%s, %s, %s%n", 
                LocalDate.parse(s).getDayOfWeek(),
                LocalDate.parse(s).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH),
                LocalDate.parse(s).getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.ENGLISH)
            )
        );
        
        System.out.println("+-+-+-+-+-+-+-+-+-+-+-+-+");
        
        // Non-Stream solution:
        // Also, showing only one style without using String#format i.e. %s
        List<String> strDateList = Arrays.asList(
                                        "2021-04-01", 
                                        "2021-05-03", 
                                        "2020-06-17"
                                    );
        
        for(String s: strDateList) {
            LocalDate date = LocalDate.parse(s);
            DayOfWeek dow = date.getDayOfWeek();
            System.out.println(dow.getDisplayName(TextStyle.FULL, Locale.ENGLISH));
        }
    }
}

Output:

THURSDAY, Thursday, Thu
MONDAY, Monday, Mon
WEDNESDAY, Wednesday, Wed
+-+-+-+-+-+-+-+-+-+-+-+-+
Thursday
Monday
Wednesday

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Thank you for your answer! If I already get the string from the array as String dayOne = "2021-04-01", can I still use this Stream.of(dayOne)? I am still trying to understand what does the %s, %s, %s%n is for. – Brian McCanaugh Jul 20 '21 at 17:19
  • @BrianMcCanaugh - I've added one more example in the answer that will help you understand how to get the day name. In order to understand `%s`, I suggest you check [this](https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html) and [this](https://dzone.com/articles/java-string-format-examples). However, that is not required to solve the problem; that is only for the formatting the output. – Arvind Kumar Avinash Jul 20 '21 at 17:23
  • @BrianMcCanaugh - Also, I have shown the output in three different ways in the Demo but you may need only one of them as per your requirement. Feel free to comment in case of any doubt/issue. – Arvind Kumar Avinash Jul 20 '21 at 21:12