0

I have a DateTime widget with 3/9/2017. Based on the documentation for DateTime, I don't see a way to determine the day of the week. I'll eventually need a string parsed in this format "Wed Feb 22 14:57:34 UTC 2017" from the DateTime widget, but the first step is to get the day of the week. Is there a way to do this outside of making my own function? And if not, what would you recommend as the best approach for the function, since days of the week are not consistent to dates from year to year?

Let me know if you need any addition information.

Thank you!

Sarah
  • 145
  • 1
  • 14
  • 1
    `java.time.LocalDateTime` provides a [getDayOfWeek](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#getDayOfWeek--) method. – Mena Mar 09 '17 at 16:48
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 05 '18 at 01:35

2 Answers2

2

Use java.util.Calendar:

    Calendar c = Calendar.getInstance();
    c.setTime(yourDate);
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
  • if you need the output to be Tue rather than 3 (Days of week are indexed starting at 1), instead of going through a calendar, just reformat the string: new SimpleDateFormat("EE").format(date) (EE meaning "day of week, short version")

Documentation

Ocean15
  • 135
  • 9
  • 1
    Mmkay, yeah I think this will work. My brain had a fart and I originally didn't think this approach was viable, but I think this will be good for me. I'll accept the answer and close out the question as soon as the system says I can. Thank you! – Sarah Mar 09 '17 at 16:50
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 06 '18 at 04:29
0

tl;dr

LocalDate.of( 2017 , Month.MARCH , 9 )
    .getDayOfWeek()
    .getDisplayName( TextStyle.FULL , Locale.ITALY )

Or…

OffsetDateTime.now( ZoneOffset.UTC )
    .format( DateTimeFormatter.RFC_1123_DATE_TIME )

java.time

The modern approach uses the java.time classes that supplanted the troublesome old date-time classes.

The DayOfWeek enum defines seven objects, one for each day of the week, Monday-Sunday.

The LocalDate class represents a date-only value without time-of-day and without time zone.

DayOfWeek dow = LocalDate.now().getDayOfWeek() ;

Generate a string of the localized name.

String output = dow.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;  // Or Locale.US etc.

To generate your longer string for a moment, use DateTimeFormatter to specify a custom pattern, use a built-in pattern, or automatically localize.

String output = OffsetDateTime.now( ZoneOffset.UTC ).format( DateTimeFormatter.RFC_1123_DATE_TIME ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154