Search code examples
javamysqldayofweekjdatechooser

How to know the day of week of selected date from jdatechooser in java


I am using java. I have used JDateChooser to select a date. now I want to display the day (line Sunday, Monday or else day of week) of the selected date. I know how to add JDateChooser but don't know how to get the day of week of selected date. Please help.


Solution

  • JDateChooser will return an instance of java.util.Date, which is, frankly, less than useless now days.

    The trick here is to covert the Date value to a LocalDateTime value, for example...

    Date date = new Date();
    LocalDateTime ldt = date.toInstant()
            .atZone(ZoneId.systemDefault())
            .toLocalDateTime();
    

    Then you can simply query the dayOfWeek value...

    DayOfWeek dow = ldt.getDayOfWeek();
    System.out.println(dow.name());
    

    Now you can use the DayOfWeek#name value or, because it's a enum, generate your own output as you need