0

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.

1 Answers1

1

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Instead of ' Date date = new Date(); ' I have used ' java.util.Date date= jDateChooser1.getDate(); ' . Note I have Used java swing drag and drop . rest the code worked fine for me. – Meera Bisht Aug 01 '19 at 05:11
  • It's the good answer. A detail, if the date chooser doesn't give any meaningful time of day, I'd use `toLocalDate` to obtain a `LocalDate` rather than a `LocalDateTime`. – Ole V.V. Aug 01 '19 at 05:23
  • @meeraBisht Yeah, it was all just using it as demonstration ;) – MadProgrammer Aug 01 '19 at 05:32